fix: deadlock using IRB integration on Rails applications#1175
Merged
ko1 merged 1 commit intoruby:masterfrom Apr 5, 2026
Merged
fix: deadlock using IRB integration on Rails applications#1175ko1 merged 1 commit intoruby:masterfrom
ko1 merged 1 commit intoruby:masterfrom
Conversation
When using `debugger` in a rails console using IRB integration, the second `continue` causes a fatal deadlock: "No live threads left. Deadlock?" The root cause is that `leave_subsession` pops `@subsession_stack` and sets `@tc = nil` before `thread_stopper.disable` takes full effect. In that window, the thread_stopper TracePoint fires on the main thread (back in IRB's eval loop), and since `@tc` is nil, the guard `tc == @tc` no longer protects it. The main thread is paused via `on_pause`, but the session server is already blocked on `@q_evt.pop` -- neither can wake the other. The fix for this is addiing `next unless in_subsession?` as the first guard in thread_stopper. Once `leave_subsession` has popped the stack, in_subsession? returns false and the TracePoint becomes a no-op even if it hasn't been disabled yet.
23958ba to
42691ec
Compare
Collaborator
|
Thank you! |
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.
Related issues
Maybe #1166
#1145
I think there are others, but I'm not entirely sure
Description
When using
debuggerin a Rails console with IRB integration (RUBY_DEBUG_IRB_CONSOLE=true), hitting a breakpoint causes a fatal deadlock:"No live threads left. Deadlock?"The root cause is a race condition in
thread_stopper:leave_subsessionpops@subsession_stackand sets@tc = nilbeforethread_stopper.disabletakes full effect. In that window, thethread_stopperTracePoint fires on the main thread (already back in IRB's eval loop), and since@tcisnil, the guardtc == @tcno longer protects it. The main thread is paused viaon_pause, but the session server is already blocked on@q_evt.pop-- neither can wake the other.The fix adds
next unless in_subsession?as the first guard inthread_stopper. Onceleave_subsessionhas popped the stack,in_subsession?returnsfalseand the TracePoint becomes a no-op even if it hasn't been disabled yet.How to reproduce
Tested with Rails 8.1.3 and Ruby 3.3.9, but the issue is not Rails-version-specific -- it occurs whenever background threads are running (e.g. ActiveRecord Pool Reaper), which triggers the race condition in
thread_stopper.Then modify the Gemfile:
Create a
script.rbfile:Then run:
With this fix applied, the second
continuereturns to the prompt normally.