Skip to content

rtc: release pending tracks that never receive media - #4971

Closed
kevin-pan-skydio wants to merge 1 commit into
livekit:masterfrom
kevin-pan-skydio:fix/release-abandoned-pending-tracks
Closed

kevin-pan-skydio wants to merge 1 commit into
livekit:masterfrom
kevin-pan-skydio:fix/release-abandoned-pending-tracks

Conversation

@kevin-pan-skydio

Copy link
Copy Markdown

A pending track is only removed from pendingTracks when its first RTP arrives or the participant closes. If a client abandons a publish (AddTrack followed by removing the transceiver before any media flows), there is no signal the server acts on, so the entry is held for the rest of the session. Long-lived publishers that churn tracks eventually hit cMaxPendingTracks and every subsequent AddTrack is rejected with "too many pending tracks".

The participant supervisor already detects this case: it reports "publish time out" after 30s of a connected, unmuted publication with no media. When ReconnectOnPublicationError is not set, that error was only logged. Now the participant drops the pending track, removes its supervisor monitor, and tells the client the track is unpublished. The full-reconnect behavior is unchanged when the option is enabled.

Fixes: #4970

A pending track is only removed from pendingTracks when its first RTP
arrives or the participant closes. If a client abandons a publish
(AddTrack followed by removing the transceiver before any media flows),
there is no signal the server acts on, so the entry is held for the rest
of the session. Long-lived publishers that churn tracks eventually hit
cMaxPendingTracks and every subsequent AddTrack is rejected with
"too many pending tracks".

The participant supervisor already detects this case: it reports
"publish time out" after 30s of a connected, unmuted publication with
no media. When ReconnectOnPublicationError is not set, that error was
only logged. Now the participant drops the pending track, removes its
supervisor monitor, and tells the client the track is unpublished.
The full-reconnect behavior is unchanged when the option is enabled.

Fixes: livekit#4970
@kevin-pan-skydio
kevin-pan-skydio requested a review from a team as a code owner October 9, 2026 18:46

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

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.

Devin Review found 2 potential issues.

Devin Review

Comment thread pkg/rtc/participant.go
Comment on lines +3993 to +4002
for _, ti := range pti.trackInfos {
if livekit.TrackID(ti.Sid) == trackID {
released = ti
continue
}
remaining = append(remaining, ti)
}
pti.trackInfos = remaining
if len(pti.trackInfos) == 0 {
delete(p.pendingTracks, cid)

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.

🔴 Queued publishes vanish on timeout

When a CID has queued publishes, releasePendingTrack deletes every entry sharing the timed-out SID. setTrackID reuses that SID for queued requests, so those publishes never get their turn.

Learn more

A pending track keeps multiple publish requests in trackInfos when the same CID is added again. setTrackID assigns each request the first entry's SID. addMediaTrack normally consumes only the first entry and keeps the remaining requests queued. Removing every matching SID instead discards requests that have not yet been attempted.

Example: A client calls AddTrack twice with CID camera, producing two pending entries with SID TR_1. The first publish times out. Both entries are deleted, even though the second request was queued to publish afterward.

Recommended fix: Retire only the timed-out request and preserve or explicitly cancel and notify each queued request. Update the queue state and supervisor monitoring so that a surviving request can proceed after the first is removed.

Devin Review


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

Comment thread pkg/rtc/participant.go
Comment on lines +4021 to +4024
if p.ProtocolVersion().SupportsUnpublish() {
p.sendTrackUnpublished(trackID)
} else {
p.sendTrackMuted(trackID, true)

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.

🔴 Late timeout unpublishes a live track

If media arrives after checkPublications reports a timeout, releasePendingTrack can find a queued request sharing the published SID. It deletes that request and sends TrackUnpublished for the live track, stopping the client's active publication.

Learn more

The supervisor checkPublications collects timeout IDs before invoking the participant callback. Meanwhile, addMediaTrack can publish the first request and retain later requests under the same CID and SID. A delayed timeout callback sees those queued entries and mistakes the SID for an unpublished track. The resulting unpublish signal targets a published track.

Example: Two requests for CID camera share SID TR_1. The supervisor checks just before media publishes the first request. When its callback runs afterward, the second request remains pending, so the server sends TrackUnpublished for TR_1 despite the live first publish.

Recommended fix: Validate that the timed-out publication still corresponds to the current pending request, and that its SID is not published, before sending an unpublish signal. Coordinate this decision with pending-track and supervisor state across the check/callback boundary.

Devin Review


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

@kevin-pan-skydio

Copy link
Copy Markdown
Author

Thanks for the fix (#4998)! @boks1971

@kevin-pan-skydio

Copy link
Copy Markdown
Author

Closing this issue now

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.

Abandoned pending tracks are never released, so long-lived participants permanently hit too many pending tracks

1 participant