fix(query): guard Query.close() cleanup with try/finally (#886)#887
fix(query): guard Query.close() cleanup with try/finally (#886)#887MukundaKatta wants to merge 1 commit into
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
seeincodes
left a comment
There was a problem hiding this comment.
Drive-by review (not a maintainer).
LGTM in shape — try/finally is exactly the right pattern, and the comment block explaining the leak modes (_transcript_mirror_batcher.close() and _read_task.wait()) is a nice touch. Cancellation is handled correctly: a BaseException from inside the try (e.g. CancelledError during _read_task.wait()) will still run transport.close() in the finally before propagating.
One small suggestion on test coverage:
The regression test only exercises the path where _transcript_mirror_batcher.close() raises. The fix's stated motivation includes _read_task.wait() raising as a second leak mode. Worth a parametrized version or a second test that simulates the read-task raising — e.g. by spawning a _read_task whose underlying coroutine raises and asserting transport.close() still runs. The shape would be analogous, and it pins both branches of the fix's value rather than relying on inspection that try/finally covers them transitively.
Nothing else worth flagging — _message_send.close() is sync so it can't raise an awaitable error, and transport.close() raising in the finally (if it does) replaces the inner exception via standard Python semantics, which is the right behavior for cleanup paths.
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.