Skip to content

fix(seek): run the software/audio host demuxer reposition off the main actor (#254) - #256

Merged
superuser404notfound merged 1 commit into
mainfrom
fix/issue254-off-main-reposition
Jul 30, 2026
Merged

superuser404notfound merged 1 commit into
mainfrom
fix/issue254-off-main-reposition

Conversation

@superuser404notfound

Copy link
Copy Markdown
Owner

Two field App Hangs, "Fully Blocked", 4.4 s and 5.2 s, both a seek on the software path against a remote HTTP source over a WAN link, both with the main thread parked in Demuxer.seek. Reported by @rrgomes with crash-reporter evidence in #254.

What actually blocked

SoftwarePlaybackHost is @MainActor and its VOD seek ran dem.seek(to:) inline, with no suspension point between the generation bump and the call. Two host seeks therefore cannot overlap, so the lock the main thread waited on was never another seek. Demuxer.readPacket holds accessLock across the whole of av_read_frame, and a remote read may sit there for up to AVIOReader.connStallTimeout (20 s). The main thread was waiting out one slow network read.

That also means swapping seek for seekBounded would not have prevented these two hangs on its own: 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, just not the one these reports evidence.

Change

  • Demuxer gains an async seekBounded(to:anchorStreamIndex:timeout:on:isSuperseded:) that runs on a caller-owned serial queue and reports landed / stalled / superseded.
  • Both @MainActor hosts await it instead of calling the synchronous form: on the transport seek, and on load()'s startPosition resume, which had the same shape and covers every resume into a remote source.
  • The supersede predicate is evaluated on that queue immediately before the FFmpeg call, so the reporter's trigger (20+ relative seeks in 10 s) collapses onto its last target instead of paying one lock wait per seek.
  • The 0.25 s time tick is gated while a reposition is in flight: the synchronizer still carries the pre-seek anchor and would otherwise drag the published position backwards over the target for the length of the reposition.
  • Both hosts re-check stopRequested after the await, since a stop() can now land inside it. In load() that await is the only suspension point, so it is the only place a mid-load teardown can interleave.
  • A reposition that spends its 8 s budget closes the Expose an accurate in-flight seek signal (isSeeking / seekTarget) that spans the real loopback-HLS landing and covers native scrubs #38 ticket as .stalled. Nothing on this path re-issues it, unlike the native recovery loop whose ticket stays open because AVPlayer keeps aiming at the target, so claiming .landed there would be a landing the engine cannot back up.

HLSVideoEngine (@unchecked Sendable) and the #232 interlace probe (already in a detached task) share the unbounded-seek property but not the main thread, so they are untouched.

Verification

Tests/AetherEngineTests/Issue254OffMainRepositionTests.swift: the reposition runs off the main thread; a reposition waiting on a busy demuxer leaves the main actor free (the inline call could not reach that assertion at all); a superseded reposition never reaches the demuxer; a stalled reposition does not close the ticket as a landing.

1304 tests green, swift build -Xswiftc -strict-concurrency=complete clean, tvOS + iOS Simulator builds clean. Device retest of the hang itself is with the reporter (it needs a real WAN source and is not reproducible on demand).

Refs #254

…n actor (#254)

Two field App Hangs, "Fully Blocked", 4.4 s and 5.2 s, both a seek on the
software path against a remote HTTP source over a WAN link, both with the
main thread parked in Demuxer.seek.

SoftwarePlaybackHost is @mainactor and its VOD seek ran dem.seek(to:)
inline, with no suspension point between the generation bump and the call.
Two host seeks therefore cannot overlap, so the lock the main thread waited
on was never another seek: Demuxer.readPacket holds accessLock across the
whole of av_read_frame, and a remote read may sit there for up to
AVIOReader.connStallTimeout (20 s). The main thread was waiting out one
slow network read.

That also means swapping seek for seekBounded would not have prevented
these two hangs on its own. 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, just not the one these reports
evidence.

Demuxer gains an async seekBounded that runs on a caller-owned serial
queue and reports landed/stalled/superseded. Both @mainactor hosts await
it instead of calling the synchronous form, on the transport seek and on
load()'s startPosition resume, which had the same shape and covers every
resume into a remote source. The supersede predicate is evaluated on that
queue immediately before the FFmpeg call, so the reporter's trigger (20+
relative seeks in 10 s) collapses onto its last target instead of paying
one lock wait per seek.

Two consequences of the seek becoming a real suspension point are handled
here rather than left to surface later: the 0.25 s time tick is gated
while a reposition is in flight, because the synchronizer still carries
the pre-seek anchor and would drag the published position backwards over
the target; and both hosts re-check stopRequested after the await, since a
stop() can now land inside it.

A reposition that spends its 8 s budget closes the #38 ticket as .stalled.
Nothing on this path re-issues it, unlike the native recovery loop whose
ticket stays open because AVPlayer keeps aiming at the target, so claiming
.landed there would be a landing the engine cannot back up.

HLSVideoEngine (@unchecked Sendable) and the #232 interlace probe (already
in a detached task) share the unbounded-seek property but not the main
thread, so they are untouched.

Verified: 1304 tests green, swift build -strict-concurrency=complete,
tvOS + iOS Simulator builds clean.

Reported by rrgomes from two production crash-reporter App Hang events.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01At1agC2qU2Y2MX2BKLdT4N
@superuser404notfound
superuser404notfound merged commit fa2a513 into main Jul 30, 2026
3 checks passed
@superuser404notfound
superuser404notfound deleted the fix/issue254-off-main-reposition branch July 30, 2026 05:23
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