fix: release shells mutex before spawn_subshell to prevent deadlock#342
Open
ibash-corpusant wants to merge 1 commit intoshell-pool:masterfrom
Open
fix: release shells mutex before spawn_subshell to prevent deadlock#342ibash-corpusant wants to merge 1 commit intoshell-pool:masterfrom
ibash-corpusant wants to merge 1 commit intoshell-pool:masterfrom
Conversation
select_shell_desc() previously held the global 'shells' mutex while calling spawn_subshell(), which calls wait_for_startup(). If wait_for_startup blocks (e.g. due to a version mismatch between client and daemon, or a shell that fails to start), the mutex is held forever, deadlocking the entire daemon. All operations (list, attach, detach, kill) that need the shells mutex will hang indefinitely. Fix: Refactor select_shell_desc() into three phases: 1. Hold the lock to determine whether to create/reuse a session 2. Release the lock, then call spawn_subshell (may block) 3. Re-acquire the lock briefly to insert the session and return refs Also adds a 30-second timeout to wait_for_startup() as defense-in-depth, and a regression test that proves list does not deadlock during a slow shell spawn.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Contributor
|
Please explain the extent to which you used AI to generate this PR. It will help me know how closely I have to read it while reviewing. |
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.
Problem
select_shell_desc()holds the globalshellsmutex while callingspawn_subshell(), which callswait_for_startup(). Ifwait_for_startupblocks (e.g. due to a version mismatch between client and daemon, or a shell that fails to start), the mutex is held forever, deadlocking the entire daemon. All operations (list,attach,detach,kill) that need theshellsmutex hang indefinitely.This was observed in production when a daemon that had been running for 2+ days encountered a client/daemon binary mismatch after a package update. The
wait_for_startupsentinel handshake hung, and from that point on, everyshpool list,shpool attach, etc. blocked forever.Fix
Refactor
select_shell_desc()into three phases:spawn_subshell()(which may block inwait_for_startup)The critical change:
spawn_subshell()is now called after theshellsmutex is released. Ifwait_for_startupblocks, only the attach thread is affected —list,kill,detach, and otherattachrequests proceed normally.Also adds a 30-second timeout to
wait_for_startup()as defense-in-depth. Previously it was an infinite loop.Regression Test
New test (
deadlock.rs) that:attach(which hangs inwait_for_startup)shpool liststill completes within 5 secondsBefore fix:
DEADLOCK DETECTED— list times out after 5sAfter fix: list returns immediately, test passes in 3.2s
Test Results
All existing tests pass (51 passed, 1 pre-existing
prompt_prefix_fishfailure unrelated to this change).