Skip to content

fix(wasapi): initialize notification threads as MTA - #1413

Open
dannyward630 wants to merge 6 commits into
RustAudio:masterfrom
dannyward630:fix/1402-wasapi-callback-mta
Open

dannyward630 wants to merge 6 commits into
RustAudio:masterfrom
dannyward630:fix/1402-wasapi-callback-mta

Conversation

@dannyward630

@dannyward630 dannyward630 commented Oct 24, 2026 •

Copy link
Copy Markdown

Closes #1402

Summary

  • Run endpoint notification handling on a dedicated MTA worker and keep its COM calls on that apartment. Notification callbacks only update atomics and signal events.
  • Reuse one MTA worker and enumerator for default-endpoint availability checks, while querying the current endpoint for every request.
  • Cache caller-side enumerators per thread with a matching COM initialization guard, avoiding a process-wide first-caller apartment binding.
  • Use Arc-owned RAII event handles for the monitor, notification callback, and audio loop, including the stop event.
  • Start monitoring before constructing a default-device stream.

AI assistance: Codex contributed implementation and tests.

Validation

  • cargo fmt --all -- --check — passed.
  • cargo check --tests --target x86_64-pc-windows-gnu — passed; this cross-compiled the Windows library and test code but did not execute the tests.
  • cargo clippy --lib --target x86_64-pc-windows-gnu -- -A dead_code — passed; the allowance is for the existing unrelated frames_to_duration warning.
  • git diff --check — passed.
  • GitHub Actions on c806859 passed across Code Quality, platform compatibility, and sanitizer workflows.

@Marco-Farruggio

Copy link
Copy Markdown
Collaborator

The repository's strict pre-commit Clippy command reports the existing dead_code in unchanged src/host.rs:241 (frames_to_duration)

#1411 will fix this, so no concern there.

As for validation, have you tried any specific code which would have failed and tested it now works, or ran anything live? All the validation seems in theory, an actual reproduction of #1402, working, would be nice. (Edit: Just saw notification_callbacks_do_not_initialize_com())

As for the diff, anything COM related is more @LastExceed's territory, so I haven't really checked it over. I believe there was already a lot of discussion of this/similar in #1302

@Marco-Farruggio Marco-Farruggio mentioned this pull request Oct 25, 2026
13 tasks done
@LastExceed

Copy link
Copy Markdown
Collaborator

The whole situation is a mess, there really shouldn't be an STA requirement for WASAPI in the first place. I also find the current WASAPI code to be rather ugly in general, and this PR doesn't exactly improve the situation. What we really need is a full rewrite of the WASAPI host, based on WinRT to get rid of all the COM headaches in general, and using the wasapi-rs crate to outsource all the FFI stuff. I have already started working on this, but it will probably take much more time to get ready. I'm not sure yet how I feel about hacking our way through the current implementation in the meantime

@roderickvd

Copy link
Copy Markdown
Member

What we really need is a full rewrite of the WASAPI host, based on WinRT to get rid of all the COM headaches in general, and using the wasapi-rs crate to outsource all the FFI stuff. I have already started working on this, but it will probably take much more time to get ready.

Having something more robust: sure, why not, but like you said, it might take time. Second, I'm always careful to long for greener pastures. Legacy code has typically accumulated so many fixes and edge cases, that sometimes it's easier to refactor in place than to rewrite entirely.

I'm not sure yet how I feel about hacking our way through the current implementation in the meantime

In the meantime I think we should definitely continue to consider fixes and refactoring. I'm just not sure if this is it though. At first glance, I'm seeing some correctness and idiomatic issues.

Comment thread src/host/wasapi/device.rs Resolved
Comment thread src/host/wasapi/device.rs Resolved
Comment thread src/host/wasapi/stream.rs Resolved
Comment thread src/host/wasapi/stream.rs Resolved
Comment thread src/host/wasapi/stream.rs Resolved
Comment thread src/host/wasapi/stream.rs Resolved
Comment thread src/host/wasapi/stream.rs Resolved
Comment thread src/host/wasapi/stream.rs Resolved
Comment thread src/host/wasapi/device.rs Resolved

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WASAPI: default-device activation fails with RPC_E_CHANGED_MODE after endpoint-notification callbacks put thread-pool threads into an STA

4 participants