Repository navigation
Description
LiveTelemetrySampler: synchronous AVFoundation IPC on the main actor at 1 Hz → fatal app hangs on the native path
What happens
Production tvOS hosts hang with the main thread parked in mach_msg waiting on a reply from the media server process, LiveTelemetrySampler.tick as the culprit frame. Sentry groups these as "App Hang Fully Blocked" (6.1–6.9s observed) and, in the worst cases, watchdog termination. thermal_state is nominal on the reported events, so this isn't throttling.
at swift::runJobInEstablishedExecutorContext
at closure in LiveTelemetrySampler.start (LiveTelemetrySampler.swift:79)
at LiveTelemetrySampler.tick (LiveTelemetrySampler.swift:143)
at -[AVPlayerItem(AVPlayerItemLogging) accessLog]
at itemasync_CopyProperty
at _dispatch_sync_invoke_and_complete_recurse
at playerasync_runSynchronousCommandOnQueue
at remoteXPCItem_CopyProperty
at FigXPCSendStdCopyPropertyMessage
at FigXPCRemoteClientSendSyncMessageCreatingReply
at xpc_connection_send_message_with_reply_sync
at dispatch_mach_send_with_result_and_wait_for_reply
at _dispatch_mach_send_and_wait_for_reply
at mach_msg
at mach_msg2_trap
Observed in the field on 5.0.x pins. The file is unchanged from 5.0.1 through 5.5.1 (git diff 5.0.1 5.5.1 -- Sources/AetherEngine/Diagnostics/LiveTelemetrySampler.swift is empty), so 5.5.1 behaves identically.
When it fires
LiveTelemetrySampler is @MainActor and runs a 1 Hz Task { @MainActor } for the whole session. On the .native backend each tick makes roughly six synchronous XPC round-trips to figplayer/mediaserverd, all on the main actor (line numbers at 5.5.1):
| Call site | Blocking read | Consumer |
|---|---|---|
tick() L143 |
item.accessLog() |
LiveTelemetry snapshot only |
tick() L157 → computeNativeForwardBuffer L261, L264 |
item.loadedTimeRanges, player.currentTime() |
snapshot + the extractor yield gate (L188) |
tick() L191 → emitLagDiag L221, L225, L230 |
player.currentTime(), item.accessLog(), player.timeControlStatus |
EngineLog line only |
Each is a sync IPC round-trip. When the media server is momentarily busy — a display-mode change on a 4K HDR start is one case where we see it — the reply is slow and the main actor blocks for the duration. The stack proves the sampler is the blocked frame; it doesn't pin down what the server was busy with, and the bug holds regardless of the cause.
To be clear about causality: the sampler doesn't stall the media server, it's the victim. But it's what converts an invisible, recoverable backend stall into a fully-blocked main thread and a watchdog kill. A 1 Hz diagnostics tick shouldn't be able to terminate the host process.
Two aggravating details:
emitLagDiagpays its IPC cost even when logging is off. It does the reads at L221/L225/L230 and only reachesEngineLog.emitat L245; the sole early-out is the nil player/item guard. Disabling engine logging doesn't avoid the round-trips.- Only 2 of the ~6 round-trips are load-bearing.
computeNativeForwardBuffer's pair feedsextractorYieldState.setForwardBuffer()at L188, which drivesFrameExtractor.shouldYield. TheaccessLog()at L143 and the whole ofemitLagDiagexist purely to populatediagnostics.liveTelemetry(which has no readers inside the engine — it's a host-facing output port) and one log line.
Suggested fix
Primary: move the AVFoundation reads off the main actor. accessLog(), loadedTimeRanges, currentTime(), and timeControlStatus are all safe to read from a background context; the sampler only needs @MainActor for the engine counters. Hopping off for the AVFoundation cluster and back to publish the snapshot and call setForwardBuffer is behaviour-preserving, needs no new API, and removes the hang for every host.
Secondary, optional: let hosts skip the diagnostics-only reads. A LoadOptions flag or setLiveTelemetryEnabled(_:) gating just the L143 accessLog() and emitLagDiag would drop ~4 of the 6 round-trips for hosts that draw no stats overlay, while computeNativeForwardBuffer keeps running for the yield gate.
One caution if you'd rather gate the whole sampler instead: that would silently break the extractor yield gate. With no ticks, _consecutiveHealthyTicks stays at 0, and shouldYield is restartInFlight || consecutiveHealthyTicks < 3, so the extractor would yield forever. The two outputs need separating before the sampler as a whole can be made optional — which is why the off-main-actor fix seems like the better primary.
Happy to send a PR for the primary fix if you'd like it.
Repro
- tvOS host, engine
5.5.1,.nativebackend, any HEVC 4K HDR direct-play source. - Start playback under any condition that makes the media server briefly slow to answer property queries (a display-mode change on the HDR handshake is one we've seen).
- While the server is slow to reply,
tick()'saccessLog()blocks the main actor for the length of the stall. - With Sentry app-hang tracking at a 2s threshold (
enableAppHangTracking,appHangTimeoutInterval = 2) this surfaces as "App Hang Fully Blocked" withLiveTelemetrySampler.tickas the culprit. Attaching a debugger suppresses the report, so it only shows up in release/TestFlight telemetry.
Activity
fix(diagnostics): review round: per-instance read queue, reload-seam …
Merge fix/134-telemetry-offmain: telemetry and diagnostics AVFoundati…
superuser404notfound commentedon Jul 17, 2026
Fixed in 5.6.1. Your analysis was exact, and I went with your primary proposal.
What shipped:
All AVFoundation reads of a native tick now run as one coalesced batch on a dedicated serial background queue:
accessLog()andcurrentTime()once per tick instead of twice, plusloadedTimeRanges,timeControlStatus, and the LagDiag property cluster in the same hop. The main actor only suspends across the batch; a stalled mediaserverd reply parks a GCD worker, not the main thread. A tick that resumes afterstop(), or after a reload seam swapped the player mid-read, drops its snapshot instead of publishing stale data into the new session, and the queue is per sampler instance so a wedged read from a stopped session cannot delay the next session's first read.Your report prompted an audit for siblings, and there were two: the 30 s memory probe reads
currentTime()/loadedTimeRangesfor its buffer inspection (same class, now hopped through the same helper), andseekableEndwas a per-callseekableTimeRangesread that live clock-tick sinks and the 1 Hz paused-live window timer hit at cadence on the main actor. It is now a KVO mirror ofseekableTimeRanges, so every consumer, including the live seek paths, reads a cached value.Your second aggravation (
emitLagDiagpaying IPC with logging off) fell out structurally: the LagDiag line consumes the same pre-read batch as the snapshot, so it adds zero AVFoundation reads of its own.On the secondary ask (a
LoadOptionsflag to skip the diagnostics-only reads): holding off for now, same reasoning as thepreferMediaPlaylistcall in #130. With the batch off-main and coalesced, the main-actor cost is zero for every host, and what remains is roughly one extra property read per second on a background queue. A public flag would fragment whatdiagnostics.liveTelemetrymeans per host for a saving that no longer buys responsiveness. If your telemetry still shows the sampler's background reads contributing measurable mediaserverd load, open an issue with numbers and I will revisit; the yield-gate and telemetry outputs are now cleanly separable, exactly because of the caution you flagged about gating the whole sampler (you were right that it would have broken the extractor yield gate).New tests pin the off-main behavior with deliberately stalled injected reads, including a mutation-verified guard test for the mid-read player swap; 670 tests green.
Could you run 5.6.1 through your TestFlight/Sentry setup and confirm the "App Hang Fully Blocked" group with
LiveTelemetrySampler.tickas the culprit frame goes quiet? With your 2 sappHangTimeoutInterval, any residual main-thread hold from the sampler should now be gone entirely, not just below threshold. And thank you for an exemplary report: the stack, the exact read inventory with line numbers, and the load-bearing/diagnostics-only split saved a full day of triage.l984-451 commentedon Jul 17, 2026
Thanks for the instant turnaround!
I've implemented it and will follow up with anything new learned in Sentry.
fix(test): widen telemetry off-main timing backstops to de-flake CI (#…
fix(diagnostics): move native telemetry AVFoundation reads off the ma…
fix(ci): serialize swift test to de-flake main-actor timing tests (#134)
fix(ci): revert --no-parallel, widen the main-actor backstops instead (…
fix(test): make telemetry drop-guard reads unbounded barriers (#134)
fix(native): recovery paths must not read AVPlayer on the main actor (s…
fix(native): keep playback log reads off the main thread (#134) (#566)