Repository navigation
fix(seek): run the software/audio host demuxer reposition off the main actor (#254) - #256
Merged
Merged
Conversation
…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
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.
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
SoftwarePlaybackHostis@MainActorand its VOD seek randem.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.readPacketholdsaccessLockacross the whole ofav_read_frame, and a remote read may sit there for up toAVIOReader.connStallTimeout(20 s). The main thread was waiting out one slow network read.That also means swapping
seekforseekBoundedwould not have prevented these two hangs on its own:seekBoundedarms its read deadline afteraccessLock.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
Demuxergains an asyncseekBounded(to:anchorStreamIndex:timeout:on:isSuperseded:)that runs on a caller-owned serial queue and reportslanded/stalled/superseded.@MainActorhosts await it instead of calling the synchronous form: on the transport seek, and onload()'sstartPositionresume, which had the same shape and covers every resume into a remote source.stopRequestedafter the await, since astop()can now land inside it. Inload()that await is the only suspension point, so it is the only place a mid-load teardown can interleave..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.landedthere 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=completeclean, 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