Close the socket after each response instead of leaking a descriptor - #12
Merged
Conversation
NetworkSession is created per connection and nothing retains it: the only strong reference is the `[self]` capture inside the receive handler, which is released as soon as that handler returns. The send completion then cancelled the connection through `weak self`, so by the time it fired self was usually already nil and NWConnection.cancel() never ran. Every served request leaked one file descriptor. The process holds a 256 soft file limit, so a client polling GET /status once a second — which the bundled browser extension does — exhausts it in well under an hour. Observed in the wild: 2,532 sockets stuck in CLOSED state and the listener no longer accepting connections while the app appeared healthy. Capture the connection rather than the session, so cancellation is guaranteed regardless of session lifetime. The regression test counts the test process's own descriptors across a burst of requests. Against the previous code it reports growth of exactly 200 over 200 requests; with the fix, growth stays near zero and the listener still answers after the burst. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Merged
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.
The bug
NetworkSessionis created per connection, and nothing retains it —NetworkListenermakes it a local, callsstart(), and lets it go. The only strong reference is the[self]capture inside the receive handler, which is released as soon as that handler returns.The response path then cancelled the connection through
weak self:So
NWConnection.cancel()typically never ran, and every served request leaked one file descriptor.Why it matters
The app runs against a 256 soft file limit. A client polling
GET /statusonce a second — which the bundled browser extension does — exhausts that in well under an hour.Observed on a live instance: 2,532 sockets stuck in
(CLOSED), 2,625 descriptors held, and the listener silently refusing new connections while the menu bar app looked perfectly healthy. Restarting the app "fixes" it until the count climbs again, which makes it easy to misread as a network or firewall problem.The fix
Capture the connection instead of the session, so cancellation can't depend on session lifetime:
Verification
The new test counts the test process's own open descriptors (via
/dev/fd) across a burst of requests, then confirms the listener still answers afterward.🤖 Generated with Claude Code