ClaudeSession: RLock so switch_model/restart don't self-deadlock#495
Merged
Conversation
session.prompt acquires self._lock via ``with self:`` then calls switch_model, which ALSO does ``with self._lock:`` to serialize the swap. threading.Lock is non-reentrant — the second acquire from the same thread blocks forever. restart() has the same shape. Use threading.RLock so the inner acquire is allowed to succeed when called from the outer context. Other callers (external threads) still serialize correctly because the RLock counter only increments for the owning thread. Unblocks set_status for phase-2 (emoji) when it needs to switch from sonnet back to opus — previously every set_status after the first phase hung because of this self-deadlock.
rhencke
approved these changes
Apr 14, 2026
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.
session.promptacquiresself._lockviawith self:then callsswitch_model, which ALSO doeswith self._lock:to serialize the swap.threading.Lockis non-reentrant — the second acquire from the same thread blocks forever.restart()has the same shape.Use
threading.RLockso the inner acquire is allowed when called from the outer context. Other callers (external threads) still serialize correctly because the RLock counter only increments for the owning thread.Unblocks
set_statuson phase-2 (emoji) when it needs to switch back to opus — everyset_statusafter the first phase was wedging on this self-deadlock.