Fix EventSource native handlers accumulating on unsubscribe/resubscribe cycles - #34
Open
alexanderkasten with Copilot wants to merge 3 commits into
Open
Fix EventSource native handlers accumulating on unsubscribe/resubscribe cycles#34alexanderkasten with Copilot wants to merge 3 commits into
alexanderkasten with Copilot wants to merge 3 commits into
Conversation
Contributor
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…ibe cycles Co-authored-by: alexanderkasten <[email protected]>
Copilot
AI
changed the title
[WIP] Fix duplicate EventSource listeners on unsubscribe/resubscribe
Fix EventSource native handlers accumulating on unsubscribe/resubscribe cycles
Feb 19, 2026
There was a problem hiding this comment.
Pull request overview
Fixes an SSE subscription lifecycle bug where native EventSource handlers accumulate across unsubscribe/resubscribe cycles, leading to duplicate message delivery in the client SSE layer.
Changes:
- Update
SSEManagerto store a per-event native forwarder function alongside the user listener set, enabling proper native handler removal when the last listener unsubscribes. - Add a Jest test covering the unsubscribe/resubscribe cycle to prevent regressions.
- Update
package-lock.json(includes manypeermetadata changes).
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/client/sse-manager.ts | Retains native forwarder references per event name and removes them when the last user listener is removed. |
| src/client/sse-manager.test.ts | Adds coverage for unsubscribe/resubscribe cycles and native handler detachment behavior. |
| package-lock.json | Large lockfile metadata churn unrelated to the described SSE fix (needs confirmation/intent). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <[email protected]>
alexanderkasten
marked this pull request as ready for review
February 19, 2026 13:20
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.
Each time all listeners for an event name were removed and then re-added, a new anonymous forwarding function was attached to the native
EventSourcewhile the old one remained — causing duplicate (and compounding) message delivery.Root cause
addEventListenerguarded against duplicate native handlers viaconnection.listeners.has(eventName), butremoveEventListenerdeleted the Map key when theSetemptied. No reference to the anonymous forwarder was retained, so it could never be removed from the nativeEventSource. On the next subscribe, the guard missed and a second handler was attached.Changes
sse-manager.ts— Change thelistenersMap value fromSet<Listener>to{ forwarder, userListeners }, storing the native forwarding function alongside the user-listener set.addEventListener: creates a namedforwarder, stores it in the entry, passes it tosource.addEventListener.removeEventListener: whenuserListenersempties, callssource.removeEventListener(eventName, forwarder)before deleting the Map key.sse-manager.test.ts— Adds a test covering the unsubscribe/resubscribe cycle: verifiesremoveEventListeneris called on the native source when the set empties, exactly one new handler is registered on resubscribe, and messages arrive exactly once.Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.