Skip to content

LiveTelemetrySampler: synchronous AVFoundation IPC on the main actor at 1 Hz → fatal app hangs on the native path #134

Closed

Description

@l984-451

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:

  • emitLagDiag pays its IPC cost even when logging is off. It does the reads at L221/L225/L230 and only reaches EngineLog.emit at 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 feeds extractorYieldState.setForwardBuffer() at L188, which drives FrameExtractor.shouldYield. The accessLog() at L143 and the whole of emitLagDiag exist purely to populate diagnostics.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

  1. tvOS host, engine 5.5.1, .native backend, any HEVC 4K HDR direct-play source.
  2. 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).
  3. While the server is slow to reply, tick()'s accessLog() blocks the main actor for the length of the stall.
  4. With Sentry app-hang tracking at a 2s threshold (enableAppHangTracking, appHangTimeoutInterval = 2) this surfaces as "App Hang Fully Blocked" with LiveTelemetrySampler.tick as the culprit. Attaching a debugger suppresses the report, so it only shows up in release/TestFlight telemetry.

Activity

  1. superuser404notfound commented on Jul 17, 2026

    @superuser404notfound
    Owner

    Fixed in 5.6.1. Your analysis was exact, and I went with your primary proposal.

    What shipped:

    1. All AVFoundation reads of a native tick now run as one coalesced batch on a dedicated serial background queue: accessLog() and currentTime() once per tick instead of twice, plus loadedTimeRanges, 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 after stop(), 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.

    2. Your report prompted an audit for siblings, and there were two: the 30 s memory probe reads currentTime()/loadedTimeRanges for its buffer inspection (same class, now hopped through the same helper), and seekableEnd was a per-call seekableTimeRanges read 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 of seekableTimeRanges, so every consumer, including the live seek paths, reads a cached value.

    3. Your second aggravation (emitLagDiag paying 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 LoadOptions flag to skip the diagnostics-only reads): holding off for now, same reasoning as the preferMediaPlaylist call 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 what diagnostics.liveTelemetry means 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.tick as the culprit frame goes quiet? With your 2 s appHangTimeoutInterval, 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.

  2. l984-451 commented on Jul 17, 2026

    @l984-451
    Author

    Thanks for the instant turnaround!

    I've implemented it and will follow up with anything new learned in Sentry.

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