Skip to content

[bug] A replan at the end of an episode exhausts the copy routes into a transcode, and the finished session never ends #3597

Open

Description

@CoffeeKnyte

What happened

A viewer finished an episode, the TV sat on the last frame, and the server ended up running a full video transcode for a session that was not playing anything. The admin view still showed that session as playing a transcode 17 hours later.

Two problems combine here. They share a cause with #666 (progress heartbeats count as liveness), but this case is different: the client did not go away. It is alive, connected over WebSocket, and parked at the end of the file.

1. Failures at end-of-file count as route failures and walk the plan ladder down to a transcode

Timeline (UTC, 2026-11-05), Silo Android TV 1.0.0 on a Fire TV (model AFTKRT, MediaTek AVC decoder), one TV episode with two versions:

Neither file has multiple_pps set. The episode is 2673 s long.

Time Event Plan
09:45:23 start server_remux_hls of A (video copy, DTS → stereo AAC). First frame in 481 ms; plays normally to the end (~10:30).
10:50:00 plan_invalidated / output_route_changed replan with start_position=2673 (end of file). Remux of A; ffmpeg starts and exits at once.
10:58:16 plan_failed / player_failure, ERROR_CODE_TIMEOUT (1003) original_http of B
10:58:38, 10:58:39 output_route_changed ×2 server_remux_hls of A again; ffmpeg exits at once each time
10:58:50 player_failure 1003 original_http of B
11:02:51 player_failure 1003 server_remux_hls of B (video and audio copy); ffmpeg exits at once
11:13:04 player_failure 1003 server_transcode_hls of B: h264 QSV encode plus stereo AAC downmix, seek_seconds=2672. Frozen recipe has "video_copy_failed": true. ffmpeg exits at once; a seek restart (reason=transcode_not_running, segment 1335) also exits at once.

Every replan after 10:50 asked for start_position 2672–2673, the last second of the file. Each ffmpeg had nothing left to produce and exited immediately, and the player then timed out waiting for media (ERROR_CODE_TIMEOUT). The decoder had played this exact stream for 45 minutes. Its failures came from asking to play past the end, not from video copy.

The replan path still counted each one as a failure of the current route. It appended the plan key to the attempted keys, and with open PR #3475 applied it also set the sticky video_copy_failed mark (internal/api/handlers/playback_v3.go, record.FrozenRecipe.VideoCopyFailed = true, which only excludes network/subtitle/node failures). After direct play and both remux variants were used up, the only route left was a full video transcode.

Nothing was played after 11:13. If the viewer had pressed play again (start over, or play the next episode in the same attempt), they would have got a transcode of a file this device direct-plays.

2. The finished session never ends

From 11:13 on Nov 5 until at least 04:26 UTC on Nov 6, the client POSTed /api/v1/playback/{session_id}/progress every 10 s and fetched no segments. The playback_sessions_sync row at 04:26:

play_method       | transcode
position_seconds  | 2672
is_paused         | f
has_websocket     | t
target_video_codec| h264
transcode_hw_accel| qsv
started_at        | 2026-11-05 09:45:23

No ffmpeg process was running for it (checked against the live process list). The admin activity view still listed it as a transcode, so an operator counting transcodes counts a session that is doing nothing.

It never expires because:

  • SessionManager.updateProgress (internal/playback/session.go) calls touchSessionLocked on every progress sample, whatever its position or pause state, so the idle grace never runs out while heartbeats arrive.
  • DefaultPausedSessionGrace (30 min) applies only once progress stops. A paused client that keeps heartbeating lives forever, and here the client did not even report paused.
  • The comment on remoteTransportGrace notes that "there is no absolute session lifetime cap in this manager".
  • The Android TV client did not send a stop when playback reached the end. That part is a client issue.

Steps to reproduce

Seen once in production. The likely steps:

  1. On Silo Android TV, play an H.264 episode that has a remux route (for example DTS audio, so the server remuxes) and a second version.
  2. Let it play to the end and leave the player on the last frame without pressing back.
  3. Change the audio output route (for example the TV or AVR switching off over HDMI-CEC), which sends output_route_changed and a replan at the end position.
  4. Watch the replans walk direct play → remux → transcode, then check the admin activity view hours later.

Expected behavior

  • A replan or a player_failure at or near the end of the file is not evidence against the route. It must not add the route to the attempted keys or set video_copy_failed, and it should not run a new ffmpeg to produce zero segments.
  • A session that has reached the end of the file and stays there ends by itself, releases its stream/transcode slot, and leaves the activity view.
  • A paused session, or one whose position has stopped moving, ends within a bounded time even while heartbeats keep arriving.

Suggested fixes

  1. End-of-file guard on replan and start. If start_position is within a few seconds of the source duration (or past the last segment), treat it as "playback ended". Answer with an ended outcome, or clamp to a short tail, instead of planning a route. Do not record a route failure for failures in that window. This change also has to cover the VideoCopyFailed logic in PR feat(playback): try a remux of multi-PPS H.264 first and transcode only after it fails #3475 before it merges.

  2. Auto-end near the end. On the server, end a session whose position has stayed within the last ~30 s (or past the credits marker, when one exists) for a few minutes with no segment or byte requests. On Android, send a stop when the player reaches the end. That needs a silo-android follow-up.

  3. Reap paused or stalled sessions on position, not heartbeats. Track the time since the position last advanced, separately from LastActivityAt. Proposed limits:

    • paused (or not advancing) for 2 hours: end the session and release its slots, even if heartbeats continue.
    • claiming to play, but the position has not moved and no bytes have been served for 15–30 minutes: end it.

    2 hours covers a long dinner or film-break pause; 6 hours seems too long given that a parked session holds a transcode slot against max_transcodes. The reason the paused grace was kept long in Playback freezes / won't resume on player state changes (resume after long pause, intro-skip, manual seek) #243 still applies: reaping kills the transcode, so pressing Play after a reap must recover through a replan/restart instead of freezing the player. Confirm that path before shortening anything.

Items 2 and 3 overlap with #666. #666 is about clients that stopped fetching media but keep heartbeating. This issue adds a live client parked at end-of-file and paused clients. One position-based liveness rule would cover all of them.

Version

Production build: a local fork of main at 6aa6791 with open PR #3475 applied (the source of the video_copy_failed mark). The attempted-keys exhaustion and the progress-based liveness are on main; I have not reproduced this on an unmodified main.

Deployment: Docker Compose, single node, Intel QSV.


AI-use disclosure: Claude Code (claude-opus-5-5) investigated this from production logs, playback tables, and the source, and drafted this issue. Filed at a maintainer's request.

Activity

  1. added
    bug
    on Nov 6, 2026
  2. added
    impact: playback
    priority: P2
    on Nov 6, 2026
  3. Rhainland commented on Nov 9, 2026

    @Rhainland
    Contributor

    Reproduced the replan half on origin/main at 4018816 with a synthetic H.264 + DTS file, through the v1 playback API. The server started a remux. An output_route_changed replan at the last second of the file (180 of 180 s) returned server_transcode_hls with -ss 180, and that ffmpeg exited within about 70 ms. A player_failure at 179.5 s then ended in a terminal response. The same failures at 60 s walk the same ladder, which is correct mid-file. v2 was not run live; it shares replanPlaybackApplicationV3 and executeReplanV3 with v1.

    Cause: executeReplanV3 adds the current plan key to the attempted keys for any failure-shaped replan, whatever the position.

    I have not changed any code, because the fix depends on a client contract decision. Android (PlaybackSessionManager.kt) and Apple (PlaybackSessionBridge.swift) raise a fatal replan_loop_detected when a failure replan returns a key they already sent. They add the failed key themselves, so simply not counting the route at end of file brings the same key back and shows the viewer an error on the last frame. Android's unchanged-output output_route_changed is failure-shaped too, so this issue's own trigger would hit that check.

    Options:

    • A. Server only. At end of file, don't count the route and clamp the start to a short tail. No wire change, but failure-shaped replans there end in the loop error on Android and Apple.
    • B. A new "ended" outcome or terminal. This is the only option that ends quietly, and the one I'd recommend. It needs a contract addition and work in silo-apple and silo-android, and until those ship, clients would probably show it as a generic terminal.
    • C. Clamp only. Removes the empty ffmpeg and the player timeouts with no contract change. An unchanged-output output_route_changed at end of file would still walk the ladder, so this only partly meets the expected behavior.

    Whichever is chosen, VideoCopyFailed in #3475 needs the same end-of-file exemption. The second half of this issue, the session that never ends, depends on the protocol decision #666 is waiting on, so I left it out.

    Which option should this take?

    AI disclosure: reproduced and analysed by Claude Code (claude-opus-5-5) at the maintainer's direction.

  4. blurbery commented on Nov 13, 2026

    @blurbery
    Contributor

    Thanks for the repro. I'd like to pick this up, and I'd go with B, using C as the fallback for clients that haven't opted in.

    What viewers see before any client ships depends on how the current clients react, so I read through both (silo-android 9ce12edf1, silo-apple 5780c84f):

    • Android sends the current key in attempted_plan_keys on every failure_recovery. That includes output_route_changed when the route hasn't changed, because Android has no output_change operation (PlaybackSessionManager.kt 98-105, 1152-1162). A repeated key raises replan_loop_detected (1267-1286), which stops the session, shows the error screen and cancels Up Next. So A on its own swaps today's transcode for an error on the last frame.
    • Apple only sends attempted keys after a player error. It already treats an error within 8 s of the end, or at 98.5%, as the end of the file, so it mostly avoids this. Its chained load-failure recovery after a replan isn't covered by that check.
    • Neither client understands an "ended" answer. An unknown outcome is an error on both (invalid_playback_outcome on Android, invalid_replan on Apple), and an unknown terminal reason gets the generic error screen. Android doesn't read retryable.

    So the new answer has to be opt-in. Until a client opts in, the only answer it handles cleanly is a playable plan with a key it hasn't tried, which is C.

    Proposed shape:

    1. A new terminal reason, playback_ended (adaptation_unavailable, not retryable), sent only to clients that advertise a new playback_ended_v1 client feature, with a matching server feature. I'd use a terminal reason rather than a new outcome: both clients already have a terminal path, and the outcome pair stays as it is.
    2. The end check runs in executeReplanV3 before the attempted-keys block (internal/api/handlers/playback_v3.go:5843 on 0387303d9). It covers failure recovery, seek failure recovery and the output and quality intent replans when position_seconds falls inside a short window at the end of the effective file. The route isn't counted, no ffmpeg starts, and the VideoCopyFailed mark from feat(playback): try a remux of multi-PPS H.264 first and transcode only after it fails #3475, which goes in that same block, never sees it. A seek reanchor keeps its existing invalid_seek_position check.
    3. For clients without the feature, the server moves the start back from the end so the plan produces real media, and keeps the key accounting as it is so the client never gets a repeated key. That removes the empty ffmpeg runs and the player timeouts that walked this session down to a transcode. An unchanged-route output_route_changed from Android can still step down one rung, which needs the Android change below.
    4. The window is a few seconds and at least one segment of the plan. There's no check when the duration is unknown, so a real decoder failure 30 s before the end still walks the ladder as it should. I'd settle the exact number in the PR.

    Client follow-ups I'd file:

    • silo-android: advertise playback_ended_v1 and treat playback_ended as the end of playback (keep the finished or Up Next state, stop the session, no error). Separately, stop sending output_route_changed as failure_recovery when the route hasn't materially changed, or after STATE_ENDED.
    • silo-apple: advertise the feature and handle the reason quietly, including on the chained load-failure path.

    Two questions before I start:

    If that works for you, I'll do the server part with handler tests and open the two client issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugimpact: playbackpriority: P2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions