Skip to content

fix(player): AudioPlaybackHost/SoftwarePlaybackHost memory leaks. - #213

Merged
superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
nathanpiper:fix/software-host-leaks
Jul 25, 2026
Merged

superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
nathanpiper:fix/software-host-leaks

Conversation

@nathanpiper

@nathanpiper nathanpiper commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Resolves memory leaks in SoftwarePlaybackHost / AudioPlaybackHost on long running loops by wrapping them in autoreleasepool.

Closes #205

What changed

Sources/AetherEngine/Native/SoftwarePlaybackHost.swift (130 lines, +88/-42)

Extracted the body of each of the four dispatch-block loops into a local *Iteration() -> Bool function, replacing break/continue with return false/return true. The outer while !stopRequested() then calls it inside autoreleasepool:

Loop Function Queue
Live reader readerIteration() demuxQueue
Audio look-ahead pump pumpIteration() feedQueue
Live feeder feederIteration() feedQueue
VOD / live-non-ring demux demuxIteration() demuxQueue

Additionally, 5 inner wait/poll loops that can spin for extended periods within a single outer iteration got their own autoreleasepool drains:

  • feederIteration() paused condition.wait(until: Date(…)) (every 0.5 s)
  • feederIteration() live-edge condition.wait(until: Date(…)) (single wait)
  • feederIteration() video back-pressure Thread.sleep(0.005) / isReadyForMoreMediaData spin
  • demuxIteration() paused condition.wait(until: Date(…)) (every 0.5 s)
  • demuxIteration() back-pressure poll with nested paused wait — outer body wrapped in autoreleasepool, inner condition.wait also gets its own drain

Sources/AetherEngine/Audio/AudioPlaybackHost.swift (45 lines, +31/-14)

Same pattern applied to runDemuxLoop, which previously had zero autoreleasepool usage:

  • Extracted loop body into demuxIteration() -> Bool
  • Wrapped outer while call in autoreleasepool { demuxIteration() }
  • Wrapped 3 inner wait loops in autoreleasepool:
    • Paused condition.wait(until: Date(…))
    • Back-pressure Thread.sleep(0.05)
    • EOF drain Thread.sleep(0.1) — uses autoreleasepool { if seekedAway { … } else { … } } + post-pool break check so the seekedAway escape path works correctly

Test plan

  • Device / OS: Apple TV 4K (3rd Gen) / tvOS 26.5
  • Source media (container / video codec + profile / audio / HDR-DV): Live TV / long Opus audio file
  • Result: Leak is gone for live TV / Software host, negligible difference on audio only host

Note: before/after memory logs attached.

Checklist

  • CHANGELOG.md updated
  • Commit messages follow Conventional Commits (feat(...), fix(...), chore(...))
  • The fix lives in the engine, not in a host-side workaround
  • Public API changes are intentional and documented

memprobe-opus-audio-host-before.txt
memprobe-livetv-software-host-after.txt
memprobe-livetv-software-host-before.txt
memprobe-opus-audio-host-after.txt

@nathanpiper
nathanpiper force-pushed the fix/software-host-leaks branch from cdc74fa to 1f68f81 Compare July 25, 2026 06:33
@superuser404notfound
superuser404notfound merged commit 4c3a461 into superuser404notfound:main Jul 25, 2026
3 checks passed
@superuser404notfound

Copy link
Copy Markdown
Owner

Merged, thank you. This was a well-run report from start to finish: the diagnosis was right, the before/after memprobe pair from the device makes the result unambiguous (physFP flat at ~163 MB across 13 minutes where it previously climbed 174 -> 451 MB linearly, and mallocBlocks plateaus at ~916k instead of running away), and the control-flow rewrite from break/continue into the *Iteration() -> Bool shape is faithful at every site I checked.

One gap remained, so I swept it in a follow-up (08b4973): the background audio-only pacing gate in runDemuxLoop is the mirror image of the video back-pressure gate you wrapped, and it kept spinning without a pool, both the outer isReadyForMoreMediaData poll and its inner paused condition wait. Your test run was live TV in the foreground so it never entered that path. Smaller in effect than the main leak now that the outer iteration drains, but a long park there accrues the same way.

The fix ships in the next release.

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.

Long running SoftwarePlaybackHost live sessions result in host app being terminated due to memory pressure

2 participants