Skip to content

fix: dispose track publication handles on participant disconnect#634

Open
LautaroPetaccio wants to merge 8 commits intolivekit:mainfrom
LautaroPetaccio:fix/dispose-track-publications-on-disconnect
Open

fix: dispose track publication handles on participant disconnect#634
LautaroPetaccio wants to merge 8 commits intolivekit:mainfrom
LautaroPetaccio:fix/dispose-track-publications-on-disconnect

Conversation

@LautaroPetaccio
Copy link
Copy Markdown
Contributor

Why

When the room processes a participantDisconnected event, it deletes the participant from the remoteParticipants map and emits an event — but never touches the participant's trackPublications. Each TrackPublication wraps an FfiHandle that maps to a native resource. Dropping the JS reference without calling dispose() means the native side never frees the handle. With N participants each publishing M tracks, every disconnect leaks N×M handles.

How

In the participantDisconnected event handler, before emitting the event, loop through participant.trackPublications, call ffiHandle.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, asserts trackPublications is empty on the observer side
  • cleans up resources when multiple participants disconnect simultaneously — 4 participants each with a track, all disconnect at once

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 1, 2026

🦋 Changeset detected

Latest commit: 7605e1c

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.

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.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sorry for the confusion. I meant adding your new disposal logic to the trackUnpublished handler instead. The main reason I would prefer it there is that a participant could, in theory, publish and subsequently unpublish hundreds of tracks before ever disconnecting.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Move the primary disposal to the trackUnpublished handler so handles
are freed as soon as tracks are unpublished, preventing accumulation
during long-lived sessions with track churn. Keep the sweep in
participantDisconnected as a safety net for abrupt disconnects where
individual trackUnpublished events may not fire.
trackUnpublished events always fire before participantDisconnected
(the SDK already depends on this ordering via requireRemoteParticipant),
so the safety-net loop was dead code. The eager disposal in the
trackUnpublished handler is sufficient.
@LautaroPetaccio LautaroPetaccio force-pushed the fix/dispose-track-publications-on-disconnect branch from b1cd48c to 251b6d5 Compare April 10, 2026 19:36
Copy link
Copy Markdown

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 new potential issue.

View 6 additional findings in Devin Review.

Open in Devin Review

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 participantDisconnected handler does not dispose remaining publication FfiHandles, causing FD leaks if trackUnpublished events are skipped

The PR's stated intent is to prevent FD leaks from track publication handles on participant disconnect, but the disposal is only added in the trackUnpublished handler (room.ts:451). The participantDisconnected handler (room.ts:408-416) removes the participant from remoteParticipants without disposing any remaining publication ffiHandles. If trackUnpublished events don't fire for every track before participantDisconnected (e.g., during abrupt disconnects or server-side removals), those handles will leak — the exact scenario the PR aims to fix. The test at line 562 asserts trackPublications.size === 0, validating only the happy path where trackUnpublished precedes participantDisconnected. A defensive cleanup loop in the participantDisconnected handler would close this gap.

(Refers to lines 410-413)

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

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.

2 participants