Fix socket fd leaks on AsyncSocket connect/accept failure paths - #227
Merged
swhitty merged 3 commits intoJul 22, 2026
Conversation
connected(to:pool:timeout:) never closed the freshly created socket when AsyncSocket.init, connect, or the timeout failed; accept() leaked the accepted descriptor if AsyncSocket.init threw. Both now close the underlying socket before rethrowing the original error. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #227 +/- ##
==========================================
- Coverage 92.88% 92.85% -0.04%
==========================================
Files 71 71
Lines 3727 3735 +8
==========================================
+ Hits 3462 3468 +6
- Misses 265 267 +2 ☔ View full report in Codecov by Harness. |
Counts open descriptors around 50 failing connects; fails against the pre-fix code (+50 fds) and passes with the fix. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Counting process-wide descriptors is nondeterministic when the full suite runs in parallel: CI failed with +86 (macOS) and +54 (Linux) of unrelated churn in the measurement window. Deterministic coverage of the cleanup path is tracked separately via an ownership-transfer helper refactor. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
swhitty
approved these changes
Jul 22, 2026
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
Two file-descriptor leaks in
AsyncSocket:connected(to:pool:timeout:)— the freshly createdSocketwas never closed whenAsyncSocket.initthrew,connect(to:)failed terminally (e.g. ECONNREFUSED), or the timeout fired. Every failed or timed-out client connect leaked one fd. SincewithThrowingTimeoutawaits the body task's result after cancelling it, acatchinside the body is guaranteed to run on the timeout path too.accept()— ifAsyncSocket.initthrew aftersocket.accept()returned a valid descriptor, the accepted fd was orphaned.Both sites now close the underlying socket before rethrowing the original error (
try? close()so the original error is preserved).Testing
connected_ThrowsError_WhenConnectFailsconnects to a nonexistent unix-socket path — deterministic (synchronous failure, no network timing) — and code coverage confirms it executes the new close-on-failure path.accept()catch path isn't reachable in unit form (would require forcingfcntl F_SETFLto fail on a valid fd); it mirrors the tested connect path.🤖 Generated with Claude Code