Repository navigation
Stop the keyframe nudge tearing down a connection that is still coming up - #8
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
scheduleKeyframeNudgesends 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 fromhandleTrack, which is theontrackhandler, andontrackruns duringsetRemoteDescription, 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:connectingconnecting, never reachedconnectedThe 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
decideKeyframeNudgereturnswaitwhile the connection isneworconnecting: re-poll without spending an attempt and without sending a PLI.failedandcloseddeliberately 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.armConnectingWatchdogfixes a second bug that removing the fuse would otherwise expose.connectingWatchdogis a plain handle rather than$state, so the effect that arms it re-runs only whenoverlayStatechanges. A resubscribe callsclearConnectingWatchdogwhile the overlay is alreadyconnecting, so nothing re-armed the 15s escalation and the 2450ms fuse was the only thing still covering those paths. Without this, theice restart failedandlocal renegotiation wedgedpaths would have been left with no escalation at all.attemptAutoplaynow clears the cycle before scheduling it. A fresh server offer after a websocket reconnect rebinds without going throughrequestResubscribe, andscheduleKeyframeNudgereturns 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.tsrather than in the route file because every frontend test runs againstweb/src/liband nothing tests+page.svelte, so in place it could not be covered. It mirrorsstream-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
failedandcloseddo not wait, and that a null connection state escalates. Deleting thewaitguard fails two of them, checked by hand before pushing.