Skip to content

SoftwarePlaybackHost.seek runs the deadline-free Demuxer.seek on the main actor — a remote source can block the main thread for seconds (field App Hangs) #254

Open

Description

@rrgomes

Two App Hangs in the field, both a seek on the software path against a remote source, both with the main thread blocked in avformat_seek_file.

Verified present at eb4facb (post-6.0.2), not just on the build that reported it.

Field evidence

Two crash-reporter App Hang events, "Fully Blocked", 4.4 s and 5.2 s, 2026-07-28T19:43:42Z and 19:47:57Z. Apple TV 4K 3rd gen, AetherEngine 5.19.1, a remote HTTP source over a WAN link, software decode path. Reported by a remote user, not a bench.

SoftwarePlaybackHost.seek
  Demuxer.seek
    _pthread_mutex_firstfit_lock_wait
      __psynch_mutexwait

Host-side corroboration from the same window: a seek burst, 20+ serialized relative seeks of +10 s each between 19:42:00 and 19:42:10Z. The mutex frame is the second seek queuing; the first one is what is actually blocking.

Mechanism

SoftwarePlaybackHost.seek(to:) calls dem.seek(to: seconds) — Demuxer.seek(to seconds: Double), which takes accessLock and runs avformat_seek_file plus avformat_flush with no read deadline.

seekBounded exists directly below it and its own doc comment names this exact failure:

Needed for VOD cue prewarm: missing/truncated MKV Cues causes matroska to degrade from "1-2 byte-range reads" into a linear half-file scan on a remote 70+ GB source (de-facto hang). Only AVIOReader honours the deadline.

So the hazard is known and the mitigation is already written; the software host just does not use it.

The part that makes it an App Hang rather than a stall

SoftwarePlaybackHost is @MainActor. The synchronous, deadline-free seek therefore runs on the main thread. That is why the reporter sees a frozen UI rather than a playback stall, and it means these are two independent problems:

  1. No deadline — a degraded seek can scan indefinitely.
  2. Main actor — even a bounded seek that takes the full 8 s budget is 8 s of frozen UI. Fixing only the deadline converts an unbounded hang into a bounded one.

A burst makes both worse: every subsequent seek serializes on accessLock behind the first, so N slow seeks compound instead of the later ones superseding the earlier.

Other callers of the unbounded variant

Not implicated by these reports, listed because they share the property:

  • HLSVideoEngine+SegmentPlanning.swift:245 — demuxer.seek(to: 0) under rewindBeforeScan
  • AetherEngine.swift:2656 — probe.seek(to: 0) after the interlace sample probe

What I cannot confirm

The asset's container. The rating key in our telemetry no longer resolves, so I cannot state it was Matroska, and the Cues degradation the seekBounded comment describes is Matroska-specific. In our deployment the software path predominantly serves AV1/VP9, which is Matroska-family, so it is very likely — but treat it as unproven. Two events, no local reproduction; the seek burst is reproducible, the slow remote seek is not on demand.

A client issuing 20 seeks in 10 seconds is arguably also ours to fix, so I am reporting the burst as the trigger rather than as the cause.

Activity

  1. superuser404notfound commented on Jul 30, 2026

    @superuser404notfound
    Owner

    Confirmed and fixed in #256 (839b64c). Thanks for the report, the two hang stacks plus the seek-burst window from the same period were enough to pin this without a local repro.

    One correction to the mechanism, because it changes what the fix has to be.

    The report reads the mutex frame as the second seek queuing behind the first. That cannot be what happened: SoftwarePlaybackHost is @MainActor (SoftwarePlaybackHost.swift:12) and the VOD branch of seek(to:) had no suspension point between the generation bump and dem.seek(to:), so two host seeks can never overlap. The main actor serialized them.

    The lock holder was the demux loop. Demuxer.readPacket takes accessLock and holds it across the whole of av_read_frame (Demuxer.swift:992-998), and that loop runs on demuxQueue, so on a remote source the lock is held for the length of one network read, up to AVIOReader.connStallTimeout (20 s). 4.4 s and 5.2 s sit comfortably inside that. The subtitle side readers are not candidates here, they open their own Demuxer instances.

    The consequence: swapping seek for seekBounded would not have prevented either hang. seekBounded arms its read deadline after accessLock.lock(), on the far side of the wait that actually blocked. The missing deadline is a real second hazard, it is just not the one these two events evidence. The main-actor isolation is.

    So the fix is both, in that order. Demuxer gained an async seekBounded that runs on a caller-owned serial queue and reports landed / stalled / superseded; both @MainActor hosts now await it. The supersede predicate is evaluated on that queue immediately before the FFmpeg call, so the burst that triggered this collapses onto its last target instead of paying one lock wait per seek. That covers the client-side half you offered to own.

    Two things the report did not cover that turned out to be the same defect:

    • SoftwarePlaybackHost.load and AudioPlaybackHost.load ran their startPosition resume seek the same way, inline on the main actor. That is every resume into a remote source, not just a scrub.
    • AudioPlaybackHost.seek is the same shape without the video half.

    The two other unbounded callers listed in the report are untouched, and neither is a hang risk: HLSVideoEngine is @unchecked Sendable, so rebuildHEVCExtradataWithInBandParameterSets runs on a producer thread, and AetherEngine.swift:2656 is already inside a Task.detached.

    Since the seek is now a real suspension point, two follow-on cases are handled in the same change rather than left to surface later: the time tick is gated while a reposition is in flight, because the synchronizer still carries the pre-seek anchor and would otherwise walk the published position over the target; and both hosts re-check teardown after the await. A reposition that spends its 8 s budget closes the #38 ticket as .stalled rather than reporting a landing the engine cannot back up.

    Leaving this open for a retest on the real setup. The scrub burst is reproducible on your end, the slow remote seek is not on demand, so a clean run of the same pattern over the WAN link is the only real verification available.

  2. added 2 commits that reference this issue on Jul 30, 2026
  3. superuser404notfound commented on Jul 30, 2026

    @superuser404notfound
    Owner

    Released in 6.1.1 (f40b263). No API change, no source change on the consumer side.

    Leaving this open for a retest over the real WAN link. The failure mode to watch for is the absence of the App Hang under the same scrub burst; if a seek is genuinely slow it should now surface as a slow seek with a live UI, and a reposition that spends its 8 s budget will close its seekEvents ticket as .stalled rather than .landed, which is the cleanest signal to check for on a synchronized-playback host.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions