Prevent native selector waits with pending signal exceptions#204
Merged
Conversation
samuel-williams-shopify
force-pushed
the
fix-pending-interrupt-fallback
branch
2 times, most recently
from
July 20, 2026 23:53
76b9b61 to
2094ba5
Compare
samuel-williams-shopify
force-pushed
the
fix-pending-interrupt-fallback
branch
from
July 21, 2026 00:25
2094ba5 to
077e4e3
Compare
samuel-williams-shopify
force-pushed
the
fix-pending-interrupt-fallback
branch
from
July 21, 2026 00:26
077e4e3 to
79a7244
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
RB_NOGVL_PENDING_INTR_FAILwhen Ruby provides itThread.handle_interrupt(SignalException => :never)block immediately beforerb_thread_call_without_gvl2Thread.pending_interrupt?pre-check from URing, EPoll, and KQueueWhy this works
A signal exception can be queued under an outer
Thread.handle_interrupt(... => :never)mask after the selector checksThread.pending_interrupt?but before it releases the GVL. Ruby can then mark the queue as checked and clear the VM interrupt flag, sorb_thread_call_without_gvl2enters the native wait without an interrupt to observe and without an installed UBF to wake it.Pushing a
Thread.handle_interruptmask while the pending queue is non-empty resetspending_interrupt_queue_checkedand raises the VM interrupt flag. The fallback invokesrb_thread_call_without_gvl2directly from a C-backed block, leaving no Ruby safepoint between that refresh andunblock_function_set. Ruby then either observes the interrupt before blocking or installs the UBF while holdinginterrupt_lock, closing the transition race.The fallback uses
SignalException => :never: it prevents the native wait from being stranded without changing the caller's exception-delivery timing. It is compiled out whenRB_NOGVL_PENDING_INTR_FAILis available.Tests
Performance
On Ruby 4.0.5, 7 alternating 100,000-iteration KQueue runs with a positive near-zero timeout measured 27.152 us/wait for the baseline and 27.128 us/wait for the fallback (-0.09%, within noise). The compatibility path allocates one internal
T_IMEMOper genuinely blocking wait. Nonblocking/immediately-ready waits do not enter it, and Rubies withRB_NOGVL_PENDING_INTR_FAILretain the direct native path.