Skip to content

Stop the keyframe nudge tearing down a connection that is still coming up - #8

Merged
davidtorcivia merged 1 commit into
masterfrom
fix/keyframe-nudge-escalation-race
Aug 12, 2026
Merged

davidtorcivia merged 1 commit into
masterfrom
fix/keyframe-nudge-escalation-race

Conversation

@davidtorcivia

Copy link
Copy Markdown
Owner

scheduleKeyframeNudge sends three PLIs and then escalates to a full re-subscription, on the theory stated in its own comment: the decoder is waiting on a keyframe lost in flight. That is only reachable once the transport is up, and the cycle never checked. It is armed from handleTrack, which is the ontrack handler, and ontrack runs during setRemoteDescription, so the cycle starts BEFORE ICE completes and its attempts expire 350+350+700+1050 = 2450ms later.

A viewer on a TURN relay needs about that long to connect, so the fuse raced ICE and every PLI it sent while connecting was a guaranteed no-op.

From the 2026-08-11 session in room glg-hk-color:

Time Event
21:49:39.376 subscription answered, state connecting
21:49:41.823 torn down at 2.45s, still connecting, never reached connected
21:49:44.396 replacement connects, having taken 2.43s

The replacement needed 2.43s and the original was killed at 2.45s, so it was about to succeed. The initial join in that same session cleared the fuse by roughly 140ms, which is what makes this an ordinary-join bug rather than something only reachable after a failure. Worst case it spends the whole 6-per-300s resubscribe budget and shows "We couldn't reach the stream after several attempts" to a viewer whose network was fine.

Changes

decideKeyframeNudge returns wait while the connection is new or connecting: re-poll without spending an attempt and without sending a PLI. failed and closed deliberately fall through, because the manager's ICE-restart chain owns them and parking there would leave a dead peer connection polling instead of being rebuilt. A null state means there is no peer connection left to wait for. The connecting watchdog stays the escalation for a transport that never arrives, on a 15s budget that suits ICE rather than decoding.

armConnectingWatchdog fixes a second bug that removing the fuse would otherwise expose. connectingWatchdog is a plain handle rather than $state, so the effect that arms it re-runs only when overlayState changes. A resubscribe calls clearConnectingWatchdog while the overlay is already connecting, so nothing re-armed the 15s escalation and the 2450ms fuse was the only thing still covering those paths. Without this, the ice restart failed and local renegotiation wedged paths would have been left with no escalation at all.

attemptAutoplay now clears the cycle before scheduling it. A fresh server offer after a websocket reconnect rebinds without going through requestResubscribe, and scheduleKeyframeNudge returns early when a timer is already armed, so a cycle left over from the previous peer connection would carry its spent attempts onto the new one.

Structure

The decision lives in web/src/lib/video/keyframe-nudge.ts rather than in the route file because every frontend test runs against web/src/lib and nothing tests +page.svelte, so in place it could not be covered. It mirrors stream-overlay.ts, which exists for the same reason.

Tests

Nine cases cover each branch, including that a spent attempt budget still waits while connecting, that failed and closed do not wait, and that a null connection state escalates. Deleting the wait guard fails two of them, checked by hand before pushing.

…g up

scheduleKeyframeNudge sends three PLIs and then escalates to a full
re-subscription, on the theory stated in its own comment: the decoder is
waiting on a keyframe lost in flight. That is only reachable once the
transport is up, and the cycle never checked. It is armed from
handleTrack, which is the ontrack handler, and ontrack runs during
setRemoteDescription, so the cycle starts BEFORE ICE completes and its
attempts expire 350+350+700+1050 = 2450ms later.

A viewer on a TURN relay needs about that long to connect, so the fuse
raced ICE and every PLI it sent while connecting was a guaranteed no-op.
In the 2026-08-11 session the subscription offered at 21:49:39.376 was
torn down at 21:49:41.823, still in connecting, having never reached
connected; its replacement then took 2.43s to connect. The initial join
in that same session cleared the fuse by roughly 140ms, so this bit
ordinary joins and not only recovery after a failure.

decideKeyframeNudge returns wait while the connection is new or
connecting: re-poll without spending an attempt and without sending a
PLI. failed and closed deliberately fall through, because the manager's
ICE-restart chain owns them and parking there would leave a dead peer
connection polling instead of being rebuilt. A null state means there is
no peer connection left to wait for. The connecting watchdog stays the
escalation for a transport that never arrives, on a 15s budget that
suits ICE rather than decoding.

The decision lives in web/src/lib/video/keyframe-nudge.ts rather than in
the route file because every frontend test runs against web/src/lib and
nothing tests +page.svelte, so in place it could not be covered. It
mirrors stream-overlay.ts, which exists for the same reason.

armConnectingWatchdog fixes a second bug that removing the fuse would
otherwise expose. connectingWatchdog is a plain handle rather than
$state, so the effect that arms it re-runs only when overlayState
changes. A resubscribe calls clearConnectingWatchdog while the overlay
is already connecting, so nothing re-armed the 15s escalation and the
2450ms fuse was the only thing still covering those paths. The
ice-restart-failed and renegotiation-wedged paths would have been left
with no escalation at all.

attemptAutoplay now clears the cycle before scheduling it. A fresh
server offer after a websocket reconnect rebinds without going through
requestResubscribe, and scheduleKeyframeNudge returns early when a timer
is already armed, so a cycle left over from the previous peer connection
would carry its spent attempts onto the new one.

Tests cover each branch of the decision, including that a spent attempt
budget still waits while connecting, that failed and closed do not wait,
and that a null connection state escalates. Deleting the wait guard
fails two of them.
@davidtorcivia
davidtorcivia merged commit de65a8b into master Aug 12, 2026
7 checks passed
@davidtorcivia
davidtorcivia deleted the fix/keyframe-nudge-escalation-race branch August 12, 2026 01:36
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.

1 participant