Skip to content

Fix realtime buffer-growth violation in audio callback - #117

Merged
arizkami merged 1 commit into
mainfrom
FixAudioThread
Aug 22, 2026
Merged

Fix realtime buffer-growth violation in audio callback#117
arizkami merged 1 commit into
mainfrom
FixAudioThread

Conversation

@yuumarealzxlys

@yuumarealzxlys yuumarealzxlys commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Summary

  • scratch_multi was resize()'d on the audio thread every block for multi-out bridged plugin inserts — a real-time-safety violation (buffer growth in the audio callback). Fixed by pre-reserving its capacity on the control thread in resolve_bridge_sinks, so the audio-thread resize never reallocates, while keeping the exact .len() invariant scatter_vsti_output_children depends on to recover the channel stride.
  • Removed redundant manual zero-loops in the cpal (Linux ALSA / macOS CoreAudio / Windows WASAPI Shared / ASIO), WASAPI Exclusive, and WDM-KS output callbacks — fill_output_f32 already overwrites every sample of its output slice on every reachable path, so the pre-zero was dead work every block.
  • Clamped (instead of grew) a defensively-sized scratch buffer in the WASAPI Exclusive path for the same reason (provably unreachable in practice, but still violated the no-growth rule as written).
  • Unrelated one-line fix: vst2_processor.cpp was missing #include <dlfcn.h>, which broke the whole crate's build on Linux (pre-existing on main, unrelated to the buffer work — included only because it blocked verifying everything else).

Test plan

  • cargo test -p sphere_directaudioengine --lib — 236 passed, including vsti_output_children_scatter_demuxes_channel_pairs (the test that depends on the scratch_multi.len() invariant)
  • cargo check --workspace — clean
  • cargo fmt -p sphere_directaudioengine -- --check — clean
  • wasapi_exclusive.rs / wdm_ks.rs are Windows-only and could not be compile-checked or runtime-tested on this Linux machine — needs a Windows build/runtime check before merge

…callback

scratch_multi was resize()'d on the audio thread every block for multi-out
bridged inserts; capacity is now pre-reserved on the control thread in
resolve_bridge_sinks so the audio-thread resize never reallocates, while
preserving the exact .len() invariant scatter_vsti_output_children depends on.

Also removes redundant pre-zero loops in the cpal, WASAPI exclusive, and
WDM-KS output callbacks — fill_output_f32 already overwrites every sample on
every reachable path — and clamps rather than grows a defensively-sized
scratch buffer in the WASAPI exclusive path.

Includes an unrelated one-line fix (missing <dlfcn.h> include) that was
blocking the crate from building on Linux at all.
@arizkami
arizkami merged commit 39bb97d into main Aug 22, 2026
5 of 9 checks passed
@arizkami
arizkami deleted the FixAudioThread branch August 22, 2026 15:53
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.

2 participants