Skip to content

Fix data race contributing to wrong value of ended_at#868

Merged
milos-lk merged 3 commits intomainfrom
fix-ended-at
Mar 25, 2026
Merged

Fix data race contributing to wrong value of ended_at#868
milos-lk merged 3 commits intomainfrom
fix-ended-at

Conversation

@milos-lk
Copy link
Copy Markdown
Contributor

Fix endedAt == startedAt (zero duration) when all tracks are removed before Synchronizer.End() is called.

This is a race in the egress shutdown path: track workers call RemoveTrack (which deletes from p.tracks) before the pipeline calls End(). When End() iterates participants to find maxPTS, the tracks map is already empty, so maxPTS = 0 and endedAt = startedAt.

The bug was latent before the duration inflation fix but masked by time.Now() fallback.

Fix `endedAt == startedAt` (zero duration) when all tracks are removed before `Synchronizer.End()` is called.

This is a race in the egress shutdown path: track workers call `RemoveTrack` (which deletes from `p.tracks`) before the pipeline calls `End()`. When `End()` iterates participants to find `maxPTS`, the tracks map is already empty, so `maxPTS = 0` and `endedAt = startedAt`.

The bug was latent before the duration inflation fix but masked by `time.Now()` fallback.
Comment thread pkg/synchronizer/participant.go Outdated
defer p.Unlock()

if t := p.tracks[ssrc]; t != nil {
t.Lock()
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.

can we make this an API call to TrackSynchronizer. Would be good to keep locking inside that struct and just return the value.

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.

updated

Copy link
Copy Markdown
Contributor

@biglittlebigben biglittlebigben left a comment

Choose a reason for hiding this comment

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

nice catch

@milos-lk milos-lk merged commit fbcb73e into main Mar 25, 2026
7 checks passed
@milos-lk milos-lk deleted the fix-ended-at branch March 25, 2026 21:44
milos-lk added a commit to livekit/egress that referenced this pull request Mar 25, 2026
milos-lk added a commit to livekit/egress that referenced this pull request Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants