fix: dispose track publication handles on participant disconnect#634
fix: dispose track publication handles on participant disconnect#634LautaroPetaccio wants to merge 6 commits intolivekit:mainfrom
Conversation
🦋 Changeset detectedLatest commit: b1cd48c The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
| participant.info.disconnectReason = ev.value.disconnectReason; | ||
| // Emit before disposing so listeners can still access trackPublications. | ||
| this.emit(RoomEvent.ParticipantDisconnected, participant); | ||
| // Dispose each track publication's FfiHandle to prevent FD leaks. |
There was a problem hiding this comment.
for disconnecting participants we first receive a trackUnpublished event, which would arguably be the more obvious place to perform this cleanup.
Agree though, that the cleanup needs to happen at some point.
We could wrap it in queueMicrotask in the trackUnpublished handler.
There was a problem hiding this comment.
The trackUnpublished handler currently doesn't dispose the FfiHandles either, it only removes the publication from the Map. So this would mean adding new disposal logic there, not moving it. I went with participantDisconnected as a single cleanup sweep that catches everything regardless of whether trackUnpublished fired for each track. Happy to move it if you feel strongly though.
Why
When the room processes a
participantDisconnectedevent, it deletes the participant from theremoteParticipantsmap and emits an event — but never touches the participant'strackPublications. EachTrackPublicationwraps anFfiHandlethat maps to a native resource. Dropping the JS reference without callingdispose()means the native side never frees the handle. With N participants each publishing M tracks, every disconnect leaks N×M handles.How
In the
participantDisconnectedevent handler, before emitting the event, loop throughparticipant.trackPublications, callffiHandle.dispose()on each publication, and clear the map.Test coverage
cleans up track publications when a remote participant disconnects— publishes a track, disconnects the publisher, assertstrackPublicationsis empty on the observer sidecleans up resources when multiple participants disconnect simultaneously— 4 participants each with a track, all disconnect at once