Skip to content

fix(native): keep playback log reads off the main thread - #566

Merged
superuser404notfound merged 2 commits into
superuser404notfound:mainfrom
brandomoore:brandomoore-playback-diagnostic-hang
Sep 20, 2026
Merged

superuser404notfound merged 2 commits into
superuser404notfound:mainfrom
brandomoore:brandomoore-playback-diagnostic-hang

Conversation

@brandomoore

@brandomoore brandomoore commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A tvOS hang report showed the main thread blocked for about 41 seconds while reading AVPlayerItem.accessLog(). This moves the remaining playback log reads off the main thread and out of AVFoundation callbacks, following #134.

What changed

  • Read video/audio item logs in the background, combining repeated notifications and ignoring results from replaced items—even if a subscriber replaces the item during delivery.
  • Limit this diagnostic work to two concurrent reads and one pending retirement read. A blocked getter cannot create unlimited replacement work.
  • Preserve startup error handling, the five-entry access-log limit, and byte/drop counters across item swaps. If final counters cannot be read, keep the last observed totals and log that they are incomplete.

This prevents diagnostic reads from blocking the UI; it does not fix the underlying media-service stall. The report's exact application caller remains unsymbolicated.

Test plan

On an Apple Silicon Mac running macOS 27 / Xcode 27: 111 targeted tests passed, including blocked-getter, notification-burst, teardown and synchronous item-replacement regressions. The 12 new tests also passed five repeat runs. macOS build and tvOS Simulator library/test-bundle compilation passed; no physical-device reproduction is claimed.

CI: full macOS tests hit the 30-minute limit; the unchanged base had a matching timeout. All other checks passed.

Checklist

  • Changelog and architecture docs updated
  • Conventional commits; no public API changes
  • Engine fix only; no host-app dependency changes

@brandomoore brandomoore changed the title fix(native): move item log diagnostics off playback callbacks fix(native): keep playback log reads off the main thread Sep 19, 2026
@superuser404notfound
superuser404notfound merged commit aac321e into superuser404notfound:main Sep 20, 2026
13 of 14 checks passed
@superuser404notfound

Copy link
Copy Markdown
Owner

Merged as aac321e. Thank you, this is a careful piece of work, and it closes the half of #134 that was left open: the notification reads, the .failed dump and the counter retirement were still synchronous getters on the main actor, so a busy media server could still park the UI there.

On the CI timeout: the base run you linked was genuinely red, but it was infrastructure rather than the base commit. 3d72e67 is 0027df2 plus one README line and its swift test job finished in 5m40s, and every main run since has been in that range. Several main runs on 2026-09-19 between 14:53 and 16:41 hit the same 30 minute wall and then stopped happening without a code change. Re-running the job on this PR unchanged gave a pass in 4m50s. Locally the full suite is green in 30 seconds here (625 XCTest, 3116 swift-testing), with ItemDiagnosticReadTests at 8.4s, so nothing in the new tests is holding a lane open.

Two things worth noting for the record, neither of them blocking. The retired byte and dropped frame totals now reset on prepareForItemHandover, so an episode handover starts a fresh session total instead of carrying the previous episode's bytes into it. That reads as the correct behaviour for AE#443 and the new test pins it, it is just a change the description does not call out. And AVFoundationOffMain's own doc comment still says the queue is a caller-owned serial one, while the pool hands it a concurrent queue on purpose; that comment gets a follow-up here.

superuser404notfound pushed a commit that referenced this pull request Sep 20, 2026
#566)

The comment promised a serial queue, which stopped being true when
ItemDiagnosticReadPool started passing a concurrent one to keep a second
lane open for the incoming item. Name both shapes and why the choice
belongs to the caller.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_015PM3xUJB6ZQyqnmGK1fp6F
superuser404notfound pushed a commit that referenced this pull request Sep 20, 2026
The two lanes shared a private concurrent queue. A concurrent queue draws
from the non-overcommit root, so it stops starting work as soon as the global
pool is saturated, which is the state a stranded media server produces. The
carrier failed in the one case it existed for.

Measured on macOS 27, 8 cores, with 64 work items blocked on the global pool:
a private concurrent queue and DispatchQueue.global() had not started after
30 s, a private serial queue started in 0.2 ms, a Thread in 0.1 ms. Pushing
to ~512 blocked overcommit threads stops the serial queue too, so a queue
moves the wall and a thread removes it.

The lanes already count their own admission, so each read now carries a
thread for exactly its own lifetime, two lanes meaning at most two threads.
The three serial-queue callers keep their queue, which there IS the admission
policy. The pool test asserts the carrier alongside the lane bound, so a
return to a shared queue fails where the bound is checked.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_015PM3xUJB6ZQyqnmGK1fp6F
superuser404notfound pushed a commit that referenced this pull request Sep 20, 2026
…573)

External contribution by @tschuegy. `SoftwarePlaybackHost.seek(to:)` handed
the session-axis target straight to the demuxer, the packet store, the
decoder's skip threshold and the synchronizer clock, all of which speak the
source's own timestamps. On a mid-stream-joined source (#107) the demuxer
clamped to the start of the file and the store's reservoir read as the whole
offset, so the producer stopped reading and the picture stood with nothing
reported.

Verified on merge: `Demuxer.seekBounded(to:)` takes an absolute source
timestamp (it passes `seconds * AV_TIME_BASE` into `avformat_seek_file`, and
the time-seekable path subtracts `sourcePTSOriginSeconds` itself), so the
carry does not double-apply; `SoftwarePacketReadAhead` is fed the raw clock
via `updatePlayhead`, so its keyframes and frontier really are source-axis;
and `clockSessionZero` is `firstSample - initialSeconds`, which keeps the
mapping correct after a resume as well.

Resolved the CHANGELOG conflict against #566/#568 and dropped three em-dashes
for the house style. CI green on 51b4b78, local `swift build --build-tests`
green.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_015PM3xUJB6ZQyqnmGK1fp6F
brandomoore added a commit to brandomoore/Plozz that referenced this pull request Sep 20, 2026
Pin the upstream release containing the merged background diagnostic-read
and bounded subtitle OCR fixes from superuser404notfound/AetherEngine#566
and superuser404notfound/AetherEngine#568.

Resolve the engine's FFmpegBuild 3.4.0 dependency without changing unrelated
pins. Update the playback documentation and exact-release source credit,
and assert the compiled engine version in the integration suite.

Keep existing playback routing and optional feature settings unchanged.
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