fix: shield Query.close() wait against cross-task cancel scope error#1042
Open
ashishpatel26 wants to merge 1 commit into
Open
fix: shield Query.close() wait against cross-task cancel scope error#1042ashishpatel26 wants to merge 1 commit into
ashishpatel26 wants to merge 1 commit into
Conversation
…nthropics#983) Query.close() raised 'Attempted to exit cancel scope in a different task' when called during async-generator teardown (aclose()). Shield the read-task wait so it completes cleanly regardless of the calling task's cancellation state.
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
Resolves #983
Query.close()raised'Attempted to exit cancel scope in a different task'when invoked during async-generator teardown (aclose()). The read task was spawned in one task context but cancelled+awaited from another, violating anyio/trio's cancel-scope invariant.Root Cause
close()calledawait self._read_task.wait()from a different task than the one that created the read task. Under trio, cancel scopes must be entered and exited in the same task.Fix
Wrap the
wait()call inanyio.CancelScope(shield=True)so the wait completes cleanly even when the calling task is itself being cancelled.Changes Made
src/claude_agent_sdk/_internal/query.py: shield the read-taskwait()inclose()withanyio.CancelScope(shield=True)tests/test_query.py: regression test forclose()called from async-generator teardownTesting
Related Issue
Closes #983