Skip to content

fix: prevent close listener leak on rapid tunnel reconnections - #2

Merged
punkpeye merged 3 commits into
punkpeye:mainfrom
paulleseute:fix-close-listener-leak
May 13, 2026
Merged

fix: prevent close listener leak on rapid tunnel reconnections#2
punkpeye merged 3 commits into
punkpeye:mainfrom
paulleseute:fix-close-listener-leak

Conversation

@paulleseute

Copy link
Copy Markdown
Contributor

Fixes the MaxListenersExceededWarning that shows up when tunnels reconnect a lot.

Every time a tunnel socket opens, we register a close handler on the Tunnel so we can clean up if someone calls tunnel.close(). That handler gets removed when the socket fires its close event.

The problem is when a socket dies and we immediately open a replacement — the old socket hasn't fired close yet (it's async), so its handler is still hanging around when the new one gets added. Do this enough times and handlers pile up until Node starts complaining.

Instead of waiting for the socket's close event to clean up, we do it right away when we know the socket is dead:

  • TunnelCluster now passes the dead socket reference along with the dead event
  • Tunnel keeps a map of socket → handler, and removes the entry as soon as dead fires
  • Added an integration test that simulates 20 rapid reconnections and checks that listenerCount('close') stays flat

@punkpeye

punkpeye commented Apr 7, 2026

Copy link
Copy Markdown
Owner

Just a minor lint issue

@paulleseute

Copy link
Copy Markdown
Contributor Author

Just a minor lint issue

Fixed, PTAL

@fredericbarthelet

Copy link
Copy Markdown

I'm experiencing this too. Any chance this can get merged @punkpeye 🙏?

@punkpeye
punkpeye merged commit d85cfa6 into punkpeye:main May 13, 2026
1 check passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.4.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@punkpeye

Copy link
Copy Markdown
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants