Skip to content

Unbounded aggregate memory growth in SubtitlePacketStore — per-stream cap exists, but nothing bounds the total across concurrent subtitle streams #166

Closed

Description

@G00380316

What happened?

During normal VOD playback, process memory climbs without bound until the host app hits iOS's jetsam memory limit and gets killed. The engine's own memprobe diagnostic shows the growth is not in the bounded AV packet cache (cacheMB, which stays small and tracks video bytes downloaded linearly) — it's in raw heap allocations (mallocMB), and it correlates with subtitle track count (subTracks), not with playback time or bytes transferred.

Root cause: SubtitlePacketStore (Sources/AetherEngine/Subtitles/SubtitlePacketStore.swift) caps retained packets at 32MB per stream (perStreamByteCap, line 27), but the subtitle tap harvests packets for every subtitle stream index the demuxer exposes, not just the one the host has selected (AetherEngine+Loading.swift:935-938 for the software host, Video/HLSVideoEngine.swift:286-287 for the other path). Nothing caps the total across streams. On a source with many embedded subtitle tracks (99, in our repro), each stream independently climbs toward its own 32MB ceiling; with enough bitmap/PGS tracks (the store's own comment notes these are the expensive kind — "a bitmap track keeps a wide trailing window" vs. text tracks staying far below the cap), aggregate retention scales with track count and reaches multiple gigabytes.

Steps to reproduce

  1. Load a VOD source with a large number of embedded subtitle tracks (our repro case: 99 tracks, mostly bitmap-based) through either playback path.
  2. Play normally for a few minutes.
  3. Watch the engine's own memprobe log: mallocMB (and physFP) climbs steadily, disproportionate to cacheMB and unrelated to bytes downloaded.
  4. On one of our two test devices, the climb was visible within the first ~3 minutes; on the other, after about ~20 minutes — consistent with track count/subtitle format mix driving the rate, not elapsed time.

AetherEngine version or commit SHA

5.14.0 / 396d138

Host app

Custom / my own integration

Platform

iOS

OS version

iOS 27 (iPhone 16 Pro Max); iPadOS 26.5.1 (iPad 10th generation)

Device / chip

iPhone 16 Pro Max; iPad (10th generation)

Playback path

Both — the bug is in the shared SubtitlePacketStore, tapped by both the software host (AetherEngine+Loading.swift:935-938) and HLSVideoEngine (Video/HLSVideoEngine.swift:286-287).

Source media (for playback bugs)

Not captured via ffprobe for this report — what we have is the engine's own memprobe telemetry showing subTracks=99 on the affected title. Happy to pull exact container/codec details if useful, but the mechanism reproduces on any source with a large number of subtitle streams, particularly bitmap/PGS ones.

Error codes / log lines

Host app's own VOD disk/memory cache was ruled out first — its accounting stayed flat and tracked normal download rate throughout the run. AetherEngine's memprobe on the same run:

t=30s  mallocMB=149  cacheMB=39   subCues=34   subTracks=99  physFP=311MB
t=150s mallocMB=197  cacheMB=118  subCues=52   subTracks=99  physFP=289MB
t=180s mallocMB=368  cacheMB=145  subCues=72   subTracks=99  physFP=447MB
t=210s mallocMB=790  cacheMB=158  subCues=83   subTracks=99  physFP=917MB
t=240s mallocMB=1241 cacheMB=168  subCues=91   subTracks=99  physFP=1367MB
t=300s mallocMB=1872 cacheMB=192  subCues=107  subTracks=99  physFP=1977MB

Anything else

Suggested fix: add an aggregate byte budget across entriesByStream in SubtitlePacketStore, mirroring the existing per-item cap (evict oldest entries from the least-recently-touched non-selected stream first once the aggregate budget is exceeded — same two-tier pattern as the existing per-stream eviction in appendLocked). Alternatively/additionally, consider not harvesting packets at all for subtitle streams that aren't currently selected and aren't queued by SubtitleForwardPrefetcher.

99 tracks × up to 32MB/track ≈ 3.2GB theoretical ceiling, which lines up with the observed climb above. Happy to provide more device logs or test further if useful.

Activity

  1. superuser404notfound commented on Jul 20, 2026

    @superuser404notfound
    Owner

    Fixed in 5.15.5 (just released).

    Thank you for an unusually complete report. The memprobe trace, ruling out the host cache layer by layer, and pointing straight at perStreamByteCap vs. the aggregate across entriesByStream made the root cause unambiguous.

    Your analysis was exactly right: the per-stream 32MB cap was the only bound, and both the pump tap and the forward prefetcher (#151) harvest every embedded subtitle stream into the session store so a track switch backfills instantly without a side demuxer (#112). With 99 tracks each climbing toward its own ceiling, aggregate retention scaled with track count toward ~3.2GB, which matches the physFP curve you captured.

    The fix follows your first suggestion (the two-tier eviction): an aggregate byte budget (96MB) across all streams on top of the per-stream cap.

    • The active drain targets (primary + secondary selection) are protected, so switching back to a selected track still backfills from a full window.
    • The coldest (least-recently-touched) non-selected streams evict oldest-first once the aggregate budget is exceeded.
    • Total retained bytes are tracked incrementally so the per-append check stays O(1), and the engine keeps the protected set in sync with the current selection on every change.

    I kept the harvest-all behavior rather than dropping non-selected streams (your alternative), so #112's instant selection is preserved for the normal case; only cold, long-untouched tracks on pathological many-track sources get evicted, and they re-harvest if selected later.

    If you can retest on 5.15.5 with the same run, mallocMB / physFP should plateau instead of tracking subTracks. The offer of exact container details is appreciated, but not needed here: the bound is independent of format. Thanks again for the detailed telemetry.

  2. G00380316 commented on Jul 20, 2026

    @G00380316
    Author

    Thanks for the swift action, Testing now 5.15.5 now

  3. G00380316 commented on Jul 21, 2026

    @G00380316
    Author

    Confirmed fixed on 5.15.5. Retested the same repro case (subtitle active, subTracks=99) — mallocMB/physFP plateau as expected instead of tracking subTracks, no more runaway growth tied to subtitle activity.

    Separately: we're chasing a different memory growth pattern on the same device/title (VOD-cache playback path specifically, subtitle inactive this time — subActive=false/subCues=0 throughout) that clearly isn't this bug. Ruled out the obvious candidates on our end (disk-cache accounting, a reconnect storm, SegmentCache) and confirmed via Instruments that it isn't showing up in tracked malloc/anonymous-VM allocations either, so it may be sitting in an area Instruments' Allocations tool doesn't attribute cleanly. Flagging only for context in case it turns out related down the line — not attributing it to this fix, and not filing it yet since we don't have a confident root cause. Thanks again for the quick turnaround on this one.

  4. superuser404notfound commented on Jul 21, 2026

    @superuser404notfound
    Owner

    Confirmed on my side too, and thanks for closing the loop with the plateau trace. Closing this as fixed in 5.15.5.

    On the separate VOD-cache growth you flagged: the fact that it does not show up in Instruments' tracked malloc / anonymous-VM is itself the strongest clue. The Allocations instrument only attributes heap and anonymous VM. It does not account for IOSurface-backed pixel buffers (VideoToolbox decode output) or CoreMedia block/sample-buffer backing, which live in IOKit-mapped and purgeable regions. That is exactly where a native decode/playback path can retain memory that both mallocMB and the Allocations total miss while physFP keeps climbing.

    Two things that would pin it independently:

    • Capture a region-split footprint instead of the malloc view. vmmap --summary <pid> sampled over time, or VM Tracker enabled in Instruments, and watch the IOSurface / IOKit / CoreMedia region sizes rather than the Allocations total. If the growth is there, it narrows to the decoder/pixel-buffer side and rules out any host cache for good.
    • On the native VOD-cache path specifically (the loopback-HLS producer feeding VideoToolbox), the SegmentCache you already ruled out is disk-backed and capped, so the resident-growth candidates that remain are the decode output pool and the sample-buffer queue, not the segment store.

    If the region-split trace confirms it climbs on IOSurface/CoreMedia, please open a fresh issue with that vmmap --summary delta plus the same physFP curve. It is a different subsystem from this one and deserves its own thread. The careful isolation you already did rules out most of the false leads, so a fresh trace should land close to root cause.

    (Current latest is 5.16.0, but that release only touches the live-TV blocking-reload path, nothing on VOD, so no need to jump versions for this.)

  5. G00380316 commented on Jul 21, 2026

    @G00380316
    Author

    Thanks will do !!!

  6. superuser404notfound commented on Jul 21, 2026

    @superuser404notfound
    Owner

    Closing the loop on the separate VOD growth flagged above: it became #174 and is fixed in 5.17.3.

    For the record, my speculation above was wrong on the subsystem. It was neither IOSurface/VideoToolbox decode output nor CoreMedia sample-buffer backing. The growth was URLSession-internal transport buffering on the source fetch: the persistent reader applied backpressure by blocking the delegate callback, which has no flow-control contract, so on the TLS/H2 path the connection kept reading from the origin at line rate and the undelivered body accumulated without bound (malloc-backed, but transport-internal, which is why the Allocations view still missed it while mallocMB/physFP climbed). The thread-stack sweep in #174 is what pinned it, no vmmap needed. Full root cause and the fix are in that thread.

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