fix(query): guard Query.close() cleanup with try/finally (#886)#887
Open
MukundaKatta wants to merge 1 commit intoanthropics:mainfrom
Open
fix(query): guard Query.close() cleanup with try/finally (#886)#887MukundaKatta wants to merge 1 commit intoanthropics:mainfrom
MukundaKatta wants to merge 1 commit intoanthropics:mainfrom
Conversation
Wraps the pre-transport cleanup in close() with try/finally so transport.close() always runs. Without this, a raise from _transcript_mirror_batcher.close() or _read_task.wait() leaked the CLI subprocess and the stderr reader task. Adds a regression test that injects a mirror-batcher whose close() raises and asserts transport.close() is still invoked. Fixes anthropics#886
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
Query.close()runs severalawaitcleanup steps (_transcript_mirror_batcher.close(), child-task cancel,_read_task.wait(),_message_send.close()) before finally callingawait self.transport.close(). Because these are not wrapped intry/finally, a raise from any earlier step short-circuits and skipstransport.close()— leaking the CLI subprocess and the stderr reader task.This wraps the pre-transport cleanup in
try/finallysotransport.close()always runs, regardless of earlier failures.Fixes #886.
Changes
src/claude_agent_sdk/_internal/query.py: wrap pre-transport cleanup intry/finally; theawait self.transport.close()now lives in thefinallybranch.tests/test_query_close_cleanup.py: regression test that injects a mirror-batcher whoseclose()raises and assertstransport.close()is still called.Test plan
test_close_runs_transport_close_when_mirror_batcher_close_raisesfails on the unpatched code (becausetransport.close()is never invoked) and passes with the fix.tests/test_query.py(test_close_from_same_task_still_works,test_close_from_different_task_does_not_raise, the trio backend variants) still pass — the behavior on the happy path is identical, only the error path changes.