Skip to content

fix: deduplicate getSid() listeners to prevent leak on concurrent calls#635

Open
LautaroPetaccio wants to merge 6 commits intolivekit:mainfrom
LautaroPetaccio:fix/deduplicate-getsid-listeners
Open

fix: deduplicate getSid() listeners to prevent leak on concurrent calls#635
LautaroPetaccio wants to merge 6 commits intolivekit:mainfrom
LautaroPetaccio:fix/deduplicate-getsid-listeners

Conversation

@LautaroPetaccio
Copy link
Copy Markdown

Why

Each getSid() call creates its own RoomSidChanged + Disconnected listener pair. If multiple calls race, only one resolves the SID and cleans up its RoomSidChanged listener — the rest stay attached. Additionally, the Disconnected listener registered via once() was never removed on the success path, persisting until a disconnect event or removeAllListeners().

How

Add a private sidPromise?: Promise<string> field. On the first getSid() call that needs to wait, create the promise and store it. Subsequent concurrent calls return the same promise. When the promise resolves or rejects, sidPromise is cleared so future calls after a reconnect work normally. Both the resolve and reject paths now clean up both listeners — on success, handleDisconnect is removed; on disconnect, handleRoomUpdate is removed.

Test coverage

New E2E test concurrent getSid() calls share a single listener and resolve consistently — fires 3 concurrent getSid() calls, asserts all return the same SID.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 1, 2026

🦋 Changeset detected

Latest commit: 9e38994

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@livekit/rtc-node Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

devin-ai-integration[bot]

This comment was marked as resolved.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant