Skip to content

sfu: bind retransmissions to the source cache generation - #5213

Merged
boks1971 merged 3 commits into
livekit:masterfrom
perfloop-oss:perfloop-pr-open-b75fc6entw
Nov 17, 2026
Merged

boks1971 merged 3 commits into
livekit:masterfrom
perfloop-oss:perfloop-pr-open-b75fc6entw

Conversation

@perfloop-agent

@perfloop-agent perfloop-agent commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

If a receiver restarts (track-info resync or a detected discontinuity) while a NACK repair is in flight, reused source sequence numbers can pair old outgoing headers with new media. Carry the source cache generation through forwarding metadata and reject an obsolete read under the buffer’s existing read lock.

This retains the sequencer flushing from #4760 and covers snapshots already held by the worker. Cache identities distinguish buffer instances as well as resets. Media-backed probes use the same validation and retain their padding fallback. A packet copied before the restart can still be retransmitted unchanged.

TrackReceiver.ReadRTP gains a streamGeneration parameter, BufferProvider gains GetPacketForStream, and ExtPacket gains StreamGeneration. The identity is a uint64 value from one process-wide atomic.Uint64 counter.

sequenceDiagram
    participant N as NACK worker
    participant D as DownTrack sequencer
    participant R as Receiver restart
    participant B as Packet buffer
    N->>D: Snapshot metadata and cache identity
    R->>B: Replace identity and resync
    R->>D: Flush sequencer
    N->>B: Read using snapshot identity
    B-->>N: Reject obsolete identity
Loading

pkg/sfu/downtrack_restart_test.go drives the real receiver restart coordinator at four handoff schedules for NACKs and cached-media probes. It checks original media or a completed drop rather than relying on a timed absence assertion. Additional native tests cover repeated resets, different buffers, and identity propagation through RED and dummy receivers.

Shared test packet fixtures now carry a nonzero stream generation; production cache reads still reject generation zero.

The focused Go race tests passed for forwarding, NACKs and probes, sequencer state, cache generations, RED readers, receiver wrappers, and the shared packet fixture users. Scoped golangci-lint reported no issues.

The schedules are bounded, not exhaustive interleaving coverage; overlap frequency in deployed traffic is unknown.

Case and evidence


Made by Perfloop. Human sponsor: Tomás Senart.

@perfloop-agent
perfloop-agent requested a review from a team as a code owner November 15, 2026 16:33

@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: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@tsenart

tsenart commented Nov 15, 2026

Copy link
Copy Markdown

cc @boks1971 @cnderrauber, since you reviewed our earlier SFU change in #4681.

@NaaeemMalik

Copy link
Copy Markdown

Critical architectural fix for in-flight NACK packet retransmission during ICE restarts and track resets. When a receiver resets its sequence space, asynchronous NACK repair workers holding previous snapshots can easily cross-contaminate sequence numbers across stream discontinuities. Carrying an opaque *buffer.StreamGeneration token through ExtPacket and validating under the buffer read lock cleanly prevents stale media pairing without introducing lock contention on the hot packet path.

One nuance to verify in the DownTrack sequencer: when ReadRTP rejects an obsolete generation read, ensuring the sequencer immediately emits silence padding or records the packet as dropped prevents the downstream WebRTC jitter buffer from stalling while waiting for an unresolvable sequence gap before hitting receiver timeout. Keeping the generation as an opaque pointer token enables lock-free pointer equality checks, preserving throughput across high-density SFU subscriber trees.

@boks1971 boks1971 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.

LGTM! One small code comment.

Also, ICE Restart does not do a receiver restart now. It is hard to get that alignment right between signalling and media. As media keeps flowing during an ICE restart and clients typically keep the sequence number contiguous, it is okay in practice. But, in theory it is possible that it can change.

Have to rely on restart detection to do that. Had some code for it, but it was not enabled by default because of fear of false negatives. Making some improvements to that path. Will observe that once that code goes in and can then rely on in-band detection for restarts.

Comment thread pkg/sfu/buffer/buffer_base.go Outdated
VideoLayer
Arrival int64
ExtSequenceNumber uint64
StreamGeneration *StreamGeneration // packet cache identity for GetPacketForStream

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 just make this an atomic counter?

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.

It now uses a process-wide atomic counter in the sfu: use an atomic counter for cache generations commit.

@perfloop
perfloop Bot force-pushed the perfloop-pr-open-b75fc6entw branch from 6f8d05f to 045ffb8 Compare November 17, 2026 08:33
@perfloop-agent

Copy link
Copy Markdown
Contributor Author

The update preserves the restart regression coverage while switching cache generations to an atomic counter.

@perfloop-agent

Copy link
Copy Markdown
Contributor Author

The description now omits ICE restart as a receiver-restart trigger.

if b.isClosed.Load() {
return 0, io.EOF
}
if generation == 0 || generation != b.streamGeneration {

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.

One more thing.

Dropping 0 is okay I think, but there are tests that may have issues. There is one place in sfu/testutils/data.go. Maybe add a generation to those test packets? Or can accept 0 with the knowledge that the real packet path will never have it and only tests could have it.

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 sfu: give test packets a stream generation commit assigns generation 1 to shared test packets while retaining production rejection of generation 0.

@boks1971
boks1971 merged commit 1d0bae0 into livekit:master Nov 17, 2026
3 of 5 checks passed
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.

4 participants