fix: close stdout stream explicitly to prevent CLOSE_WAIT socket leak#712
Open
olegnazarov23 wants to merge 1 commit intoanthropics:mainfrom
Open
fix: close stdout stream explicitly to prevent CLOSE_WAIT socket leak#712olegnazarov23 wants to merge 1 commit intoanthropics:mainfrom
olegnazarov23 wants to merge 1 commit intoanthropics:mainfrom
Conversation
…anthropics#665) When SubprocessCLITransport.close() runs, it closes stdin and stderr streams but never explicitly closes the stdout stream — only sets it to None. This leaves the underlying file descriptor registered in the event loop's kqueue/epoll selector. If the subprocess held open TCP connections (e.g. to the Anthropic API), those sockets enter CLOSE_WAIT after the process exits. A CLOSE_WAIT socket is permanently readable (EOF pending), so with its FD still registered in kqueue, the event loop busy-spins at ~24% CPU instead of blocking — indefinitely. Fix: explicitly call aclose() on the stdout stream before setting it to None, which deregisters the FD from the event loop and closes the underlying socket. Closes anthropics#665
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
Fixes #665 — after a
ClaudeSDKClientsession ends, the Python process burns ~24% CPU indefinitely due to a leaked CLOSE_WAIT TCP socket keeping the event loop busy-spinning.Root Cause
SubprocessCLITransport.close()closes stdin and stderr streams but never explicitly closes the stdout stream — it only setsself._stdout_stream = None. The underlying file descriptor remains registered in the event loop's kqueue/epoll selector.When the subprocess exits, any TCP connections it held (e.g. to the Anthropic API) enter CLOSE_WAIT. Since the socket's FD is still registered, kqueue reports it as readable every poll cycle (EOF pending), causing
asyncioto busy-spin instead of blocking.Fix
Explicitly call
aclose()on the stdoutTextReceiveStreambefore nulling the reference. This deregisters the FD from the event loop and closes the underlying socket, breaking the spin cycle.This follows the same pattern already used for stdin and stderr streams in the same method.
Test
Added
test_close_explicitly_closes_stdout_stream— verifies thatclose()callsaclose()on the stdout stream and cleans up all stream references.All 40 transport tests pass.