Skip to content

EXC_RESOURCE memory-limit crash: native HLS pipeline keeps reading from origin while muxer is correctly backpressure-stalled #174

Closed

Description

@G00380316

What happened?

VOD playback (routed through the native/loopback HLS-ingest pipeline — HLSVideoEngine + HLSSegmentProducer + HLSLocalServer) crashes with EXC_RESOURCE (RESOURCE_TYPE_MEMORY: high watermark memory limit exceeded, limit=3376 MB) after several minutes of otherwise-normal playback.

The engine's own memprobe shows the growth is dominated by compressed memory, not resident/active memory:

t=511s rss=651MB vmInt=606MB vmExt=42MB vmCmp=1916MB physFP=2536MB mallocBlocks=1459697 mallocMB=2401 avioFetchedMB=542 cacheMB=482 subTracks=102 subActive=false
t=541s rss=558MB vmInt=515MB vmExt=42MB vmCmp=2421MB physFP=2950MB mallocBlocks=1606681 mallocMB=2810 avioFetchedMB=576 cacheMB=514 subTracks=102 subActive=false

vmCmp climbs ~500MB in 30 seconds while rss stays flat (and even drops slightly) — memory that was allocated, went cold, and got compressed rather than freed. subActive=false/subCues=0 throughout, so this is unrelated to #166 (already fixed in 5.15.5, confirmed).

At the moment of the crash (device paused in the debugger on the EXC_RESOURCE), I checked every AetherEngine-owned thread's stack:

  • AetherEngine.HLSSegmentProducer.pump (serial): blocked in HLSSegmentProducer.runPumpLoop() -> advanceMuxer(to:) -> awaitBackpressureRelease(target:head:context:) -> SegmentCache.awaitFetchHighWater(reaching:timeout:) -> NSCondition.waitUntilDate: -> __psynch_cvwait. Correctly backpressured, waiting for the consumer to catch up before muxing more.
  • com.aetherengine.hls.accept (serial): blocked in HLSLocalServer.acceptLoop() -> __accept. Idle, waiting for a new loopback connection.
  • com.aetherengine.hls.work (concurrent): blocked in HLSLocalServer.handleConnection(_:) -> readHTTPRequest(_:) -> __recvfrom. Idle, waiting for AVPlayer's next segment request.
  • The crashing thread (com.apple.network.connections, serial): the only one actively executing — mid-read: _platform_memmove <- SSL_peek <- SSL_read <- boringssl_session_read <- nw_boringssl_read <- nw_protocol_boringssl_input_available (all libboringssl.dylib). This is a live TLS read from the real HTTPS origin (HLSLocalServer itself is loopback BSD sockets, no TLS, so this has to be the source-side network fetch, i.e. AVIOReader).

So at the instant of the crash: everything downstream of the network fetch (muxing, serving to AVPlayer) is idle and properly paused on backpressure, but the upstream network read is still actively busy pulling bytes. AVIOReader's persistent-mode reader is documented to backpressure the network thread once window is more than winHighWater (16 MB) ahead of the consumer's read position (appendPersistentData, blocking until ahead <= winHighWater) — but if the muxer/pump is stalled and the network thread is still churning regardless, that cap isn't holding here, and whatever it keeps reading accumulates (and eventually gets compressed rather than reclaimed) instead of pausing along with the rest of the pipeline.

Steps to reproduce

  1. Load a VOD title through the native/loopback HLS pipeline (not nativeRemoteHLS bypass) with subtitles inactive (rules out Unbounded aggregate memory growth in SubtitlePacketStore — per-stream cap exists, but nothing bounds the total across concurrent subtitle streams #166).
  2. Play normally; the engine's own memprobe shows vmCmp/mallocMB/physFP climbing steadily, uncorrelated with cacheMB (the bounded, tracked segment cache) or avioFetchedMB growing at a merely-linear rate.
  3. After several minutes (varies by title — as fast as ~3 min on one source, longer on others), the app is killed by EXC_RESOURCE at the configured memory limit.
  4. Pausing in the debugger at the crash and inspecting every AetherEngine thread shows the pattern above: muxer/server threads idle and backpressured, only the upstream network-read thread still active.

AetherEngine version or commit SHA

5.15.5 / 49dcac1

Host app

Custom / my own integration

Platform

iOS

OS version

iPadOS 26.5 (build 23F77)

Device / chip

iPad (10th generation)

Playback path

Native AVPlayer (loopback HLSLocalServer/HLSVideoEngine/HLSSegmentProducer pipeline)

Source media (for playback bugs)

VOD title, subTracks=102, audioTracks=2, subtitles inactive throughout (subActive=false, subCues=0).

Error codes / log lines

Thread 36 Queue: com.apple.network.connections (serial)
EXC_RESOURCE (RESOURCE_TYPE_MEMORY: high watermark memory limit exceeded (limit=3376 MB))

0 _platform_memmove (libsystem_platform.dylib)
1 SSL_peek (libboringssl.dylib)
2 SSL_read (libboringssl.dylib)
3 boringssl_session_read (libboringssl.dylib)
4 nw_boringssl_read (libboringssl.dylib)
5 nw_protocol_boringssl_input_available (libboringssl.dylib)
[AetherEngine] memprobe t=511s rss=651MB vmInt=606MB vmExt=42MB vmCmp=1916MB vmIOS=0MB physFP=2536MB mallocBlocks=1459697 mallocMB=2401 avioFetchedMB=542 cacheCount=131 cacheMB=482 packetsWritten=13578 srvConns=1 srvBytesMB=446 srvSfMB=426 pktAlive=3 pktTotal=298314 subCues=0 audioTracks=2 subTracks=102 subActive=false avBufAhead=4.4s avBufBehind=30.9s seams=1
[AetherEngine] memprobe t=541s rss=558MB vmInt=515MB vmExt=42MB vmCmp=2421MB vmIOS=0MB physFP=2950MB mallocBlocks=1606681 mallocMB=2810 avioFetchedMB=576 cacheCount=138 cacheMB=514 packetsWritten=14217 srvConns=1 srvBytesMB=467 srvSfMB=447 pktAlive=3 pktTotal=299789 subCues=0 audioTracks=2 subTracks=102 subActive=false avBufAhead=6.0s avBufBehind=31.7s seams=1

Anything else

Suggested fix direction: audit whether AVIOReader's persistent-mode read (appendPersistentData/winHighWater) is actually gated to the muxer's consumption position when the reader is feeding HLSSegmentProducer, rather than only to its own internal read cursor. If the backpressure check is comparing against a position that keeps advancing independent of the stalled muxer (e.g. tracking bytes handed to a demuxer/parse step rather than bytes actually consumed by the muxer/SegmentCache), the network thread would keep reading and retaining data well past the intended 16MB window exactly when the muxer pauses — matching what we observed here.

Confirmed independently that Instruments' Allocations instrument (malloc stack logging, "Live Allocations Only") does not show this growth in tracked heap/anonymous-VM allocations even during a run that later hit this same EXC_RESOURCE — consistent with the growth living in compressed/inactive pages the app never frees rather than a straightforward heap leak.

Activity

  1. added a commit that references this issue on Jul 21, 2026
  2. superuser404notfound commented on Jul 21, 2026

    @superuser404notfound
    Owner

    Root cause found, fixed in 5.17.3.

    Your suggested audit direction pointed at the right layer, with one twist: the backpressure gate in appendPersistentData was comparing against the correct position (the demuxer's actual consumption cursor, not an independently advancing one), and it did engage. The defect was the mechanism itself. The persistent reader applied backpressure by blocking the delegate callback until the window drained below winHighWater, and blocking a URLSession delegate has no flow-control contract. Whether the connection stops reading from the socket is a transport implementation detail:

    • On plain HTTP/1.1 (loopback test origin) the stack happens to park the transfer after ~4-5 MB of internal buffering, so this never reproduced in the engine's local tests.
    • On your HTTPS origin (boringssl in the crashing stack, so TLS, and likely h2/h3 behind the proxy) the connection keeps reading at line rate while delivery is parked, and the undelivered body accumulates in URLSession-internal allocations with no bound.

    That matches every number in your report: avioFetchedMB counts bytes actually delivered into the reader's window, and it grew at playback rate (~34 MB per 30 s, consistent with cacheMB), while mallocMB grew ~409 MB per 30 s, which is the origin's line rate. The delta is the transport's undelivered backlog: written once, read much later, so it goes cold and lands in the compressor (vmCmp climbing ~500 MB per 30 s while rss stays flat), in small blocks at ~4.9k new blocks per second (your mallocBlocks delta, ~2-3 KB average), which is TLS-record-fragment sized. It also explains the Allocations observation, since the growth sits in transport-internal buffering rather than anything the app-level live-allocation view attributes.

    The fix (#175): the persistent reader now uses task suspend/resume, the contractual flow control ("a task, while suspended, produces no network traffic"), the same mechanism the engine's streaming-mode reader already used. Delivery never blocks; the task suspends once the window exceeds the 16 MB high-water mark, and the read loop resumes it when the drain crosses the new 8 MB low-water mark (or before parking in the frontier wait, where a suspended task would never deliver). Every reconnect and teardown path balances a pending suspend before cancel. The window can still overshoot the high-water mark by the transport's in-flight amount (a few MB), which is bounded.

    Could you retest your repro on 5.17.3? Expected in your memprobe series: mallocMB/physFP plateau near avioFetchedMB + cacheMB instead of running away, vmCmp stays flat, and the source fetch turns bursty (the connection parks, then resumes in ~8 MB drains as playback consumes).

    Thanks for an exceptional report. The per-thread stack sweep at the crash instant plus the memprobe series pointed at the exact failing layer, and the delivered-vs-allocated split in your own counters is what confirmed the transport-buffering theory.

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