Skip to content

fix(playback): make the stall watchdog level-triggered with a host escalation rung - #342

Merged
superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
tschuegy:fix/stall-watchdog-level-rearm
Aug 9, 2026
Merged

superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
tschuegy:fix/stall-watchdog-level-rearm

Conversation

@tschuegy

@tschuegy tschuegy commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Problem

The #65 stall re-engage watchdog is one-shot and edge-triggered: armed per playbackStalled notification, then a single instantaneous check 6 s later. Any media fetch inside that grace window disarms it permanently — only a new playbackStalled re-arms.

Field trace: a live playlist froze at its last segment (pump death; recovery arm added in #341). AVPlayer stalled, drained the remaining tail segments (fetches inside the grace window → watchdog dead), then parked in waitingToMinimizeStalls with ~2 s buffered:

[NativeAVPlayerHost] #7 playbackStalled
[LagDiag] clk=483.66 dclk=0.00 tcs=waiting rate=1.00 wait=AVPlayerWaitingToMinimizeStallsReason fwd=2.0 keepUp=n empty=n ... (forever)

In that parked state nothing can ever fire again: playbackStalled needs a buffer-exhaustion transition (buffer stays non-empty), a waiting player never posts failedToPlayToEndTime, and the producer-side wedge detector died with the pump. The freeze was terminal from the couch.

Fix

Three changes; together they close every exit from the trap:

  1. Level re-watch. Fetch activity during the grace window re-baselines and keeps watching (bounded by maxStallWatchPasses = 10 ≈ 60 s) instead of returning permanently. Only a fully silent window escalates into the unchanged nudge → stage-2-reload ladder; a recovered/paused/failed player still disarms (those states have other owners). Verdict extracted as a pure function.

  2. Persistent reload budget (storm shape). On a frozen playlist each stage-2 reload replays the tail and re-stalls within seconds, and the fresh stall supersedes the ladder task before any post-reload check can run — a per-task rung alone can never fire. A gate spanning stall events (same shape and epsilon as the Loopback-HLS: a single backward seek on heavy 4K can wedge the segment producer (video stalls, audio continues -> A/V desync) #93 item-death gate) admits two reloads at the same frozen position; after that the ladder publishes liveSourceReset instead of reloading again. Real progress restores the budget.

  3. Post-reload rung (silent-park shape). When no further stall supersedes, a rendered clock that has not moved a whole window (12 s) after the stage-2 reload while the player still waits publishes liveSourceReset: the local session is unrecoverable consumer-side and only the host can retune. Live-gated; decision extracted as a pure function.

liveSourceReset is the existing host contract ("negotiate a fresh URL and call load"); this only adds the two missing publishers for consumer-side dead ends.

Tests

StallWatchdogLevelRearmTests pins the incident case (fetch-during-grace must re-watch, not disarm), escalation on silence, disarm on recovery/failure/cap, the frozen-clock rung (live-only), and the storm-shape budget (two reloads at a frozen position, then escalate; progress restores).

🤖 Generated with Claude Code

…calation rung

The superuser404notfound#65 stall re-engage watchdog was one-shot and edge-triggered:
armed per playbackStalled notification, then a single instantaneous
check 6 s later. Any media fetch inside that grace window disarmed it
PERMANENTLY. Field trace: a live playlist froze at its last segment,
AVPlayer drained the remaining tail (fetches during the grace window
killed the watchdog), then parked in waitingToMinimizeStalls with ~2 s
buffered — a state in which playbackStalled never re-fires, a waiting
player never posts failedToPlayToEndTime, and the producer-side wedge
detector had died with the pump. No recovery layer ever re-examined
the session; the freeze was terminal from the couch.

Three changes, smallest that closes every exit from that trap:

- Level re-watch: fetch activity during the grace window re-baselines
  and keeps watching (bounded by maxStallWatchPasses) instead of
  returning permanently; only a fully silent window escalates into the
  unchanged nudge/reload ladder.
- Persistent reload budget (storm shape): on a frozen playlist each
  stage-2 reload replays the tail and re-stalls within seconds, and
  the fresh stall supersedes the ladder task before any post-reload
  check can run — so a per-task rung alone can never fire. A gate
  spanning stall events (same shape as the superuser404notfound#93 item-death gate) admits
  two reloads at the same frozen position, then the ladder publishes
  liveSourceReset instead of reloading again.
- Post-reload rung (silent-park shape): when no further stall
  supersedes, a rendered clock that has not moved a whole window after
  the stage-2 reload while the player still waits publishes
  liveSourceReset — the local session is unrecoverable consumer-side
  and only the host can retune.

Co-Authored-By: Claude Fable 5 <[email protected]>
@superuser404notfound
superuser404notfound merged commit 549f010 into superuser404notfound:main Aug 9, 2026
3 checks passed
superuser404notfound pushed a commit that referenced this pull request Aug 9, 2026
…ether it changed

Follow-up to #342. The level re-watch and the reload budget are right. The
post-reload rung tests `clockNow == clockAtReload`, and its own doc comment
promises something else ("has not moved a whole window").

Exact equality is the wrong question here because stage 2 is an in-place swap:
a swap that half took parks the FRESH item on its own clock, or on zero before
it is ready, while being exactly as dead as a frozen one. Equality reads that
as recovery, so the rung would sit out the shape it was written for. Now it
asks whether the clock advanced past the same 0.5 s progress epsilon the
item-death gate and the deferred-failure check already use.

Widening it is safe because the clock was never the false-positive guard,
`isWaitingToPlay` is: a consumer that recovered reports `.playing`, and nothing
reaches this rung without a stall, two silent grace windows and a stage-2
reload in front of it.

Tests: the two rung cases each moved one variable at a time (the old
"moved clock" case flipped the clock AND the player state, so it pinned
neither), plus the parked-on-a-different-clock case the equality test missed.
CHANGELOG entry, and the em-dashes.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HX5zV3Fcf7Nzq4DYGdXvQE
@superuser404notfound

Copy link
Copy Markdown
Owner

Merged. Verified onto current main (the branch was based on 6.15.2): 1689 swift-testing plus 477 XCTest green, tvOS Simulator build clean, and the watch cap is load-bearing (flipping passesSoFar + 1 < cap to <= fails the trickle test).

The trap analysis holds all the way down. mediaFetchCount increments in handleTargetChange, so it counts request ARRIVALS at the local server: a player draining tail segments really does bump it inside the grace window, and the old single check really did return for good. renderedTime comes off a 100 ms periodic observer, and reloadStalledConsumerItem reloads with inPlaceSwap: true, which returns before the renderedTime = 0 reset, so the post-reload baseline survives the swap.

One change on top in b4956c1: the final rung asked whether the clock CHANGED, while its own doc comment promised whether it moved. Since stage 2 is an in-place swap, a swap that half took parks the fresh item on its own clock (or on zero before it is ready) and is exactly as dead, but equality reads that as recovery, so the rung would sit out the shape it exists for. It now asks whether the clock advanced past the same 0.5 s progress epsilon ItemDeathReviveGate and shouldSurfaceDeferredFailure already use. Widening it is safe because the clock was never the false-positive guard, isWaitingToPlay is, and nothing reaches the rung without a stall, two silent grace windows and a reload in front of it.

Also split the two rung tests so each moves one variable (the old moved-clock case flipped the clock and the player state together, so it pinned neither) and added the parked-on-a-different-clock case.

Worth naming for the retest, since it is inherited rather than new: the escalation still triggers on a full grace window with zero segment REQUESTS while waiting, and the level re-watch gives that condition 60 s of chances instead of 6. A link slow enough to spend more than a window on a single segment can therefore reach the nudge where it previously would not. Two silent windows are still required for the reload, so the exposure is the same shape as before, just observed for longer.

Ships in the next release with #340 and #341.

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.

2 participants