fix(hosts): detach connection-close listener when host disconnects - #82
Open
krazyjakee wants to merge 1 commit into
Open
fix(hosts): detach connection-close listener when host disconnects#82krazyjakee wants to merge 1 commit into
krazyjakee wants to merge 1 commit into
Conversation
handleRegisterHost registered a listener on the process-global NorayEvents bus for every register-host command and never removed it. The listener array (and the socket/host each closure retained) grew without bound, and every connection-close emit became O(number of connections ever made). Over long uptimes this leaked memory and starved the reactor until it stopped answering commands while the TCP socket still accepted connections, requiring a restart. Make the listener self-removing so it is detached once its socket has closed. Memory then plateaus and per-disconnect cost stays constant. Fixes foxssake#81 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
krazyjakee
force-pushed
the
fix/register-host-listener-leak
branch
from
July 21, 2026 23:02
e426280 to
6635365
Compare
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.
What
handleRegisterHostadds a listener to the process-globalNorayEventsbus on everyregister-hostcommand and never removes it. This makes the listener self-removing so it is detached once its socket has closed.Fixes #81.
Why
In
src/hosts/host.commands.ts, eachregister-hostdoes:NorayEvents(src/events.ts) is a module-level singleton whoseEventBusstores handlers in a plain array that is only ever pushed to — there was no matching.off(...)anywhere. Since clients also issueregister-host, every connection (host or client) permanently appends a closure that retainssocketandhost. Over time:emit("connection-close", ...)becomes O(number of connections ever made), since each disconnect walks the entire accumulated array.dbg/unresponsive-tcp/fix/unresponsive-tcpbranches appear to be investigating; those branches add diagnostics but don't detach this listener.Reproduction
Against
ghcr.io/foxssake/noray:main(v1.5.4), 15,000 connect →register-host→ disconnect cycles:mainDriver:
noray_active_hostsalso stops drifting above the real host count with the fix.Change
Extract the inline listener into a named
onConnectionCloseand callNorayEvents.off("connection-close", onConnectionClose)once it has handled its own socket's close. No behavioural change for a live host — only the cleanup that was missing.