Skip to content

Conversation

@jeremy
Copy link

@jeremy jeremy commented Nov 24, 2025

Fixes a race condition that causes SIGSEGV when a connection is closed while another thread is mid-query.

The library isn't meant to be thread-safe, but in case of mistaken concurrent use let's raise exceptions rather than crashing. Prefer both belt and suspenders when they're cheap and available.

Adds NULL deref guards to protect against connection close while awaiting IO (rb_wait_for_single_fd without GVL). Now raises Trilogy::ConnectionClosed instead of segfaulting.

Guards:

  • read_packet: check socket and buffer before read
  • trilogy_flush_writes: check socket before write
  • current_packet_type: check buffer before access
  • Ruby wrapper flush_writes: early exit on closed socket
  • Ruby read_query_response: check socket after wait returns
  • Ruby buffer_checkout/checkin: NULL pool fallback

/cc @jhawthorn

Fixes a race condition that causes SIGSEGV when a connection is closed
while another thread is mid-query.

The library isn't meant to be thread-safe, but in case of mistaken
concurrent use let's raise exceptions rather than crashing. Prefer both
belt and suspenders when they're cheap and available.

Adds NULL deref guards to protect against connection close while
awaiting IO (rb_wait_for_single_fd without GVL). Now raises
Trilogy::ConnectionClosed instead of segfaulting.

Guards:
* read_packet: check socket and buffer before read
* trilogy_flush_writes: check socket before write
* current_packet_type: check buffer before access
* Ruby wrapper flush_writes: early exit on closed socket
* Ruby read_query_response: check socket after wait returns
* Ruby buffer_checkout/checkin: NULL pool fallback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant