Repository navigation
batteries: Close the temporary Selector in NioPeerHandler.connect - #187
Open
saidalaoui-sq wants to merge 1 commit into
Open
saidalaoui-sq wants to merge 1 commit into
saidalaoui-sq wants to merge 1 commit into
Conversation
`connect` opens a `Selector` to wait for `OP_CONNECT` and never closes it. Each call leaks the selector's fds and native memory. The socket's fd also stays open after the channel is closed, because the leaked selector still holds a key for the channel, and the JDK releases a channel's fd only once no selector has it registered. On Linux this is 3 fds per call (epoll fd, eventfd and the socket), on both failed connects and connects the peer later drops. A peer that drops every connection, or an application that retries connects in a loop, runs the process out of fds. Close the selector with try-with-resources. Closing it removes only its own key, so the later `register(this.selector, ...)` is unaffected.
saidalaoui-sq
marked this pull request as ready for review
November 2, 2026 12:38
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.
NioPeerHandler.connectopens aSelectorto wait forOP_CONNECTand never closes it. Each call leaks the selector's fds and native memory. The socket's fd also stays open after the channel closes, because the leaked selector still holds a key for it. On Linux this is 3 fds per call, on failed connects and on connects that the peer later drops. A peer that drops every connection, or an application that retriesconnectin a loop, runs the process out of fds.This change closes the selector with try-with-resources. Closing it removes only its own key, so the later registration with the handler's main selector is unaffected.
The
0.1andmainbranches have the same code, and this commit cherry-picks cleanly onto both.