fix(coreaudio): share one AudioQueue per format and recreate failed start handles #337

Merged
Sheffy007 merged 1 commit from upstream-pr/337 into main 2026-11-27 16:14:20 +00:00
Sheffy007 commented 2026-11-27 12:47:48 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T12:47:48Z.


Closes #336
Refs #8
Refs #258

Problem

In world runs on current main (with #288 merged), sound still cuts out when the title repeatedly re-activates its spatial audio stream during heavy world-load pressure:

  • #8 (#issuecomment-7217386312): AudioQueueStart failed with OSStatus 2003329396 ('what') on 4 of 14 stream activations in the default run and 6 of 32 in the heap run (0% free memory), falling back to silence.
  • #258: "Sound went silent soon after the world began; the audio device kept running."

Every XAudio2 engine, WASAPI IAudioClient::Initialize, and ISpatialAudioClient::ActivateSpatialAudioStream calls coreaudio::open_default(48_000, 2, render), which previously opened and started a separate AudioToolbox AudioQueue (AudioQueueNewOutput + AudioQueueStart) each time. When a mid-run spatial stream re-activation hit 'what' from coreaudiod under load, #288 retried AudioQueueStart on the same failed handle (stalling 500 ms) and then permanently bound the newly activated stream to Silent ("no audio device: sound is mixed and dropped") even while the process's initial 48 kHz stereo AudioQueue kept running.

Fix

  1. Share one AudioQueue per (rate, channels) (Hub / MixerQueue): Active CoreAudio handles attach a Slot to the running (48_000, 2) queue and mix in userspace (f32 sum into pre-allocated scratch buffers in refill with zero heap allocations on the callback thread; silence when zero streams are attached). Mid-run spatial/WASAPI/XAudio2 stream activations attach in userspace without calling AudioQueueNewOutput or AudioQueueStart again, and only reopen if AudioQueueEnqueueBuffer reports a runtime device error.
  2. Recreate the queue handle on AudioQueueStart retry: When opening a hardware AudioQueue for the first time (or after a runtime failure), each retry disposes the failed AudioQueueRef and allocates/primes a fresh queue handle instead of retrying AudioQueueStart on the broken handle.
  3. Thread-aware Drop without callback lock cycles: refill invokes each slot's Render outside the slot mutex (in_call: Option<ThreadId> + Condvar), waiting for any in-flight callback from other threads on Drop while avoiding self-deadlock if a callback ever drops its own Output re-entrantly.

Tests

  • host_posix::coreaudio::tests::the_device_opens_pulls_frames_and_closes & the_silent_output_keeps_time
  • host_posix::coreaudio::tests::streams_share_one_queue_mix_and_survive_repeated_reactivations (2 concurrent streams mix 0.25 + 0.50 = 0.75, idle outputs 0.0, and 32 stream tear-downs/re-activations make 1 AudioQueueNewOutput and 1 AudioQueueStart call total)
  • host_posix::coreaudio::tests::start_retry_disposes_failed_queue_and_recovers_after_runtime_error ('what' on attempt 0 disposes 0x1001 and starts fresh 0x1002; runtime AudioQueueEnqueueBuffer failure notifies active streams and reopens 0x1003)
  • host_posix::coreaudio::tests::reentrant_drop_inside_render_callback_does_not_deadlock
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5526883747 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/337). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-27T12:47:48Z. --- Closes #336 Refs #8 Refs #258 ## Problem In world runs on current `main` (with #288 merged), sound still cuts out when the title repeatedly re-activates its spatial audio stream during heavy world-load pressure: - #8 (`#issuecomment-7217386312`): `AudioQueueStart failed with OSStatus 2003329396` (`'what'`) on 4 of 14 stream activations in the default run and 6 of 32 in the heap run (`0%` free memory), falling back to silence. - #258: *"Sound went silent soon after the world began; the audio device kept running."* Every `XAudio2` engine, WASAPI `IAudioClient::Initialize`, and `ISpatialAudioClient::ActivateSpatialAudioStream` calls `coreaudio::open_default(48_000, 2, render)`, which previously opened and started a separate AudioToolbox `AudioQueue` (`AudioQueueNewOutput` + `AudioQueueStart`) each time. When a mid-run spatial stream re-activation hit `'what'` from `coreaudiod` under load, #288 retried `AudioQueueStart` on the same failed handle (stalling `500 ms`) and then permanently bound the newly activated stream to `Silent` ("no audio device: sound is mixed and dropped") even while the process's initial 48 kHz stereo `AudioQueue` kept running. ## Fix 1. **Share one `AudioQueue` per `(rate, channels)` (`Hub` / `MixerQueue`):** Active `CoreAudio` handles attach a `Slot` to the running `(48_000, 2)` queue and mix in userspace (`f32` sum into pre-allocated scratch buffers in `refill` with zero heap allocations on the callback thread; silence when zero streams are attached). Mid-run spatial/WASAPI/XAudio2 stream activations attach in userspace without calling `AudioQueueNewOutput` or `AudioQueueStart` again, and only reopen if `AudioQueueEnqueueBuffer` reports a runtime device error. 2. **Recreate the queue handle on `AudioQueueStart` retry:** When opening a hardware `AudioQueue` for the first time (or after a runtime failure), each retry disposes the failed `AudioQueueRef` and allocates/primes a fresh queue handle instead of retrying `AudioQueueStart` on the broken handle. 3. **Thread-aware `Drop` without callback lock cycles:** `refill` invokes each slot's `Render` outside the slot mutex (`in_call: Option<ThreadId>` + `Condvar`), waiting for any in-flight callback from other threads on `Drop` while avoiding self-deadlock if a callback ever drops its own `Output` re-entrantly. ## Tests - `host_posix::coreaudio::tests::the_device_opens_pulls_frames_and_closes` & `the_silent_output_keeps_time` - `host_posix::coreaudio::tests::streams_share_one_queue_mix_and_survive_repeated_reactivations` (2 concurrent streams mix `0.25 + 0.50 = 0.75`, idle outputs `0.0`, and 32 stream tear-downs/re-activations make `1` `AudioQueueNewOutput` and `1` `AudioQueueStart` call total) - `host_posix::coreaudio::tests::start_retry_disposes_failed_queue_and_recovers_after_runtime_error` (`'what'` on attempt 0 disposes `0x1001` and starts fresh `0x1002`; runtime `AudioQueueEnqueueBuffer` failure notifies active streams and reopens `0x1003`) - `host_posix::coreaudio::tests::reentrant_drop_inside_render_callback_does_not_deadlock`
Olima34 commented 2026-11-27 13:28:51 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-27T13:28:51Z.


Check report: 45ed3281 checked together with #337 and #340 merged onto main 60baf130 (M5 Pro 48 GB, arm64, with the converter):

  • cargo test -p victory-hle -p victory -p victory-gpu -p xtask: all pass (victory-hle 144 passed, main 141 with the new tests of both PRs; victory-gpu 347 passed).
  • cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings: clean. cargo xtask policy: ok.
  • HLE only, so no sweep; not run in game.

Result: passes, and no review question is open. It can't be merged from my side (the branch rule requires an approving review), so it's ready for the maintainer. @Sheffy007, if you get a world run with it, tools/macos/run-summary.sh prints the audio section (spatial stream activations, whether the device kept playing); posting that before/after would show #336 is fixed in game.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7219528649 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/337#issuecomment-7219528649). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-27T13:28:51Z. --- Check report: `45ed3281` checked together with #337 and #340 merged onto main `60baf130` (M5 Pro 48 GB, arm64, with the converter): - `cargo test -p victory-hle -p victory -p victory-gpu -p xtask`: all pass (victory-hle 144 passed, main 141 with the new tests of both PRs; victory-gpu 347 passed). - `cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings`: clean. `cargo xtask policy`: ok. - HLE only, so no sweep; not run in game. Result: passes, and no review question is open. It can't be merged from my side (the branch rule requires an approving review), so it's ready for the maintainer. @Sheffy007, if you get a world run with it, `tools/macos/run-summary.sh` prints the audio section (spatial stream activations, whether the device kept playing); posting that before/after would show #336 is fixed in game.
LakshanSivakumar commented 2026-11-27 16:15:16 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T16:15:16Z.


Merged in 4cf84c83. Before pushing I ran the tests (native arm64), Windows clippy, the policy check, a release build, and one headless game run of the exact tree (170 s, Series S): menus at 30 fps, the world entered at about 81 s and kept presenting to the end at about 1 frame a second, 0 GPU errors, no crash. Audio kept playing through the run. Thanks.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7223040721 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/337#issuecomment-7223040721). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-27T16:15:16Z. --- Merged in 4cf84c83. Before pushing I ran the tests (native arm64), Windows clippy, the policy check, a release build, and one headless game run of the exact tree (170 s, Series S): menus at 30 fps, the world entered at about 81 s and kept presenting to the end at about 1 frame a second, 0 GPU errors, no crash. Audio kept playing through the run. Thanks.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
LakshanSivakumar/Native-Victory-macOS!337
No description provided.