fix: use unbounded message buffer to prevent deadlock on multi-turn queries (#558)#572
Open
naga-k wants to merge 2 commits intoanthropics:mainfrom
Open
fix: use unbounded message buffer to prevent deadlock on multi-turn queries (#558)#572naga-k wants to merge 2 commits intoanthropics:mainfrom
naga-k wants to merge 2 commits intoanthropics:mainfrom
Conversation
…ueries The _read_messages() loop handles both control protocol routing and regular message buffering in a single async loop. With a bounded buffer (max_buffer_size=100), when receive_response() stops consuming at a ResultMessage, the buffer fills up and _read_messages() blocks on send(). This prevents it from reading any transport data — including control messages the CLI subprocess needs answered to process new queries. The result is a deadlock where subsequent queries hang indefinitely. Fix: use math.inf for an unbounded buffer. Messages are still consumed by receive_response(), so memory stays bounded in practice. The unbounded buffer just prevents the routing loop from stalling. Fixes anthropics#558
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
When using
ClaudeSDKClientfor multi-turn conversations, the secondquery()call hangs indefinitely after the first query involves subagentTaskinvocations.Root Cause
The
_read_messages()loop inQueryhandles both control protocol routing (init acknowledgments, tool result requests) and regular message buffering in a single async loop. The message buffer is bounded at 100 slots viaanyio.create_memory_object_stream(max_buffer_size=100).When
receive_response()stops consuming at aResultMessage, unconsumed messages (e.g.,task_notificationfrom subagent completion) remain in the buffer. If enough accumulate to fill the 100-slot buffer,_read_messages()blocks onawait self._message_send.send(message).This creates a deadlock chain:
_read_messages()blocked on buffer send → can't read stdoutreceive_response()hangs foreverFix
Change
max_buffer_size=100tomax_buffer_size=math.inf(unbounded). This is safe because:anyio.create_memory_object_streamexplicitly supportsmath.inffor unbounded buffersreceive_response(), so memory stays bounded in practiceChanges
query.py):import math+ unbounded buffer + explanatory commenttest_message_buffer_deadlock.py): 3 tests that reproduce the deadlock with the old bounded buffer and verify the fix resolves itTesting
test_bounded_buffer_blocks_control_message_routing— patches Query withbuffer=100, sends 110 messages with no consumer, proves control request times out (deadlock reproduced)test_unbounded_buffer_allows_control_message_routing— same scenario withmath.inf, control request succeeds (fix verified)test_query_class_uses_unbounded_buffer— asserts the config ismath.infFixes #558