-
Notifications
You must be signed in to change notification settings - Fork 110
fix: deduplicate getSid() listeners to prevent leak on concurrent calls #635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b9c8e42
46682ea
807b2bc
4c1ccce
104deab
9e38994
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@livekit/rtc-node': patch | ||
| --- | ||
|
|
||
| Deduplicate getSid() listeners to prevent event listener leak on concurrent calls |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -131,6 +131,11 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks> | |
| return this._serverUrl; | ||
| } | ||
|
|
||
| // Shared promise for concurrent getSid() callers. Without this, each call | ||
| // registers its own RoomSidChanged + Disconnected listeners, and if many | ||
| // calls race only one of each pair is cleaned up — leaking the rest. | ||
| private sidPromise?: Promise<string>; | ||
|
|
||
| /** | ||
| * Gets the room's server ID. This ID is assigned by the LiveKit server | ||
| * and is unique for each room session. | ||
|
|
@@ -144,19 +149,26 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks> | |
| if (this.info?.sid && this.info.sid !== '') { | ||
| return this.info.sid; | ||
| } | ||
| return new Promise((resolve, reject) => { | ||
| const handleRoomUpdate = (sid: string) => { | ||
| if (sid !== '') { | ||
| if (!this.sidPromise) { | ||
| this.sidPromise = new Promise<string>((resolve, reject) => { | ||
| const handleDisconnect = () => { | ||
| this.off(RoomEvent.RoomSidChanged, handleRoomUpdate); | ||
| resolve(sid); | ||
| } | ||
| }; | ||
| this.on(RoomEvent.RoomSidChanged, handleRoomUpdate); | ||
| this.once(RoomEvent.Disconnected, () => { | ||
| this.off(RoomEvent.RoomSidChanged, handleRoomUpdate); | ||
| reject('Room disconnected before room server id was available'); | ||
| this.sidPromise = undefined; | ||
| reject('Room disconnected before room server id was available'); | ||
| }; | ||
| const handleRoomUpdate = (sid: string) => { | ||
| if (sid !== '') { | ||
| this.off(RoomEvent.RoomSidChanged, handleRoomUpdate); | ||
| this.off(RoomEvent.Disconnected as any, handleDisconnect); | ||
| this.sidPromise = undefined; | ||
| resolve(sid); | ||
| } | ||
| }; | ||
| this.on(RoomEvent.RoomSidChanged, handleRoomUpdate); | ||
| this.once(RoomEvent.Disconnected, handleDisconnect); | ||
| }); | ||
| }); | ||
| } | ||
| return this.sidPromise; | ||
| } | ||
|
|
||
| get numParticipants(): number { | ||
|
|
@@ -283,6 +295,10 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks> | |
| return ev.message.case == 'disconnect' && ev.message.value.asyncId == res.asyncId; | ||
| }); | ||
|
|
||
| // Clear sidPromise before removing listeners so that a reconnect | ||
| // doesn't return a stale, permanently-pending promise. | ||
| this.sidPromise = undefined; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should already be accounted for if you don't remove the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The once-listener depends on the 'disconnected' FFI event arriving and being processed before the 'disconnect' callback resolves. Since we can't guarantee that ordering, and if the callback resolves first, removeAllListeners() wipes the once-listener before it fires, the explicit cleanup here is needed as a safety measure to prevent a stale sidPromise across reconnects. |
||
|
|
||
| FfiClient.instance.removeListener(FfiClientEvent.FfiEvent, this.onFfiEvent); | ||
| this.removeAllListeners(); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.