You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
COM initialization maintenance is currently unsound #96
initialize_sta/mta() and deinitialize() currently don't come with any contract, in fact they aren't even marked unsafe. Calling deinitialize() while COM objects (agile or otherwise) are still in use is UB, and can lead to segfaults.
One way to fix this would be to increment the init ref count every time a struct wrapping a COM object is instantiated, and decrementing it in drop, instead of relying on the consumer to correctly use initialize_sta/mta() and deinitialize().
Alternatively, those functions could be made unsafe and a contract added requiring their correct usage.
Thanks, good catch, that's reachable UB from safe code.
Bit of history: initialize_mta/initialize_sta were copied from windows-rs when it dropped them, just for convenience, and deinitialize was added later on request in #19. Managing COM init properly was never meant to be wasapi-rs's job, and I'd rather not add a counter for it. It's up to the app.
I'd still keep the wrappers so a small tool doesn't need a Windows crate just for CoInitializeEx. The init functions can stay safe, since an unbalanced init only leaks. My suggested fix is to make deinitialize unsafe, with a safety section saying the calls must be balanced and nothing from the crate may outlive the init. The docs would also say you probably don't need it, since process exit cleans up anyway, and anyone who actually needs it already knows the rules.
Not decided yet though, so shout if you see a problem with that.
That's not what I meant. I was referring to the fact that COM initialization is already reference counted internally, meaning repeated calls to CoInitializeEx only increment an internal counter (and correspondingly CoUninitialize decrements it), with actual apartment buildup / teardown only happening on the first / last call.
My suggestion is to call CoInitializeEx() in every constructor, and CoUninitialize() in every destructor (actually we should be using the thread-independent counterparts CoIncrementMTAUsage() / CoDecrementMTAUsage() instead, since we're dealing with agile objects), which would also remove the need for consumers of wasapi-rs to handle COM initialization at all.
Ah, I misread that, thanks for clarifying. Makes a lot more sense now. It would work, but then wasapi-rs would be papering over COM init the way windows-rs does for WinRT types (it calls CoIncrementMTAUsage behind your back when needed). That's only a narrow fallback for WinRT activation though, and in microsoft/windows-rs#1169 they conclude that COM init can't reliably be done automatically and is better set up by the app.
My goal for wasapi-rs is to be a thin wrapper that presents WASAPI as it is, without trying to "fix" the Windows APIs. COM init is the app's business, not the job of the audio library.
A crate like cpal should definitely paper over platform quirks, since its whole point is a unified API across platforms. But wasapi-rs is Windows only, and there I think it's better to show the API as it is. Obviously a subjective view, but one I prefer for simplicity and ease of maintenance.
So the plan is to make deinitialize unsafe in the next release, as suggested above. Further ahead, once we move to windows-core 0.101 (or wherever we land, there is a lot of churn right now), it looks like the COM init helpers can be dropped altogether, since windows-core is getting its own init_mta() and init_sta().
wasapi-rs would be papering over COM init the way windows-rs does for WinRT types
Why do you say "papering over" like its a bad thing? As far as I can tell this is exactly how COM initialization is meant to be handled. Its akin to every COM-dependent struct holding an Arc<COMApartment> so to speak.
in microsoft/windows-rs#1169 they conclude that COM init can't reliably be done automatically and is better set up by the app
The problem discussed in that thread is outside the scope of wasapi-rs, and thus of no concern. If someone overrules the implicit MTA by explicitly joining another apartment, and then tears it down prematurely, that's on them. The init/uninit functions are unsafe after all.
Ok sorry, "papering over" was probably not the best choice of words. I didn't mean it as something bad. For something like cpal that's exactly the right call. And you're right about microsoft/windows-rs#1169, I had only skimmed that thread.
It's a neat idea, and with Drop handling the cleanup it's something Rust makes quite doable. But automatic COM init has to be done really well, since a slightly wrong version means crashes that are very hard to track down. For example, the WinRT fallback in windows-rs only takes its own MTA reference if COM isn't already up. If wasapi-rs happens to hold the MTA at that moment and releases it in Drop later, COM can go away under the WinRT objects. Handling every such case properly is a lot of work, for something the app can do with a single line. And once the crate manages COM, every COM problem in an app that uses it becomes a wasapi-rs problem.
I looked at some other projects that use WASAPI directly (MPC-HC, Sanear, EarTrumpet, cubeb). They all leave COM init to the app.
So I'll keep it as it is. #97 makes deinitialize unsafe and documents that COM is up to the app. The init helpers get dropped once windows-core ships its own init_mta() and init_sta().
I feel like there is still a misunderstanding here:
the WinRT fallback in windows-rs only takes its own MTA reference if COM isn't already up, If wasapi-rs happens to hold the MTA at that moment and releases it in Drop later, COM can go away under the WinRT objects
This is a windows-rs problem, not a wasapi-rs problem. Which, by the way, I'm not even sure applies here at all, since what WinRT is calling internally is RoInitialize(), not CoInitialize() (though I admit that I don't fully understand the relation between the two yet) Edit: see follow-up comment
Handling every such case properly is a lot of work
I don't see there being any special cases to handle. If someone tries to reuse a COM apartment they aren't managing themselves, never declares a dependency on it (i.e. incrementing the ref count), and then runs into problems because the COM apartment shuts down too early on the assumption that there is nothing left depending on it, they really only got themselves to blame.
Anyone who wants/needs to can still manage apartments for themselves of course. Explicitly entering an apartment (via CoInitializeEx()) overrules the implicit MTA association (from CoIncrementMTAUsage()) after all.
Thanks, I do get the idea, and I can see it being useful, like how your cpal host relies on the implicit MTA init from the WinRT calls it makes. But leaving COM init to the app is a deliberate choice about scope, not a technical one. I want to keep wasapi-rs simple, and this adds a fair bit of complexity for little in return. So I'd like to leave the discussion there.
The Windows Runtime provides the RoInitialize function, which is a thin wrapper around CoInitializeEx
[...]
It’s analogous to OleInitialize, which also calls CoInitializeEx and then some
Activity
HEnquist commentedon Nov 21, 2026
Thanks, good catch, that's reachable UB from safe code.
Bit of history:
initialize_mta/initialize_stawere copied from windows-rs when it dropped them, just for convenience, anddeinitializewas added later on request in #19. Managing COM init properly was never meant to be wasapi-rs's job, and I'd rather not add a counter for it. It's up to the app.I'd still keep the wrappers so a small tool doesn't need a Windows crate just for
CoInitializeEx. The init functions can stay safe, since an unbalanced init only leaks. My suggested fix is to makedeinitializeunsafe, with a safety section saying the calls must be balanced and nothing from the crate may outlive the init. The docs would also say you probably don't need it, since process exit cleans up anyway, and anyone who actually needs it already knows the rules.Not decided yet though, so shout if you see a problem with that.
LastExceed commentedon Nov 21, 2026
That's not what I meant. I was referring to the fact that COM initialization is already reference counted internally, meaning repeated calls to
CoInitializeExonly increment an internal counter (and correspondinglyCoUninitializedecrements it), with actual apartment buildup / teardown only happening on the first / last call.My suggestion is to call
CoInitializeEx()in every constructor, andCoUninitialize()in every destructor (actually we should be using the thread-independent counterpartsCoIncrementMTAUsage()/CoDecrementMTAUsage()instead, since we're dealing with agile objects), which would also remove the need for consumers ofwasapi-rsto handle COM initialization at all.HEnquist commentedon Nov 21, 2026
Ah, I misread that, thanks for clarifying. Makes a lot more sense now. It would work, but then wasapi-rs would be papering over COM init the way windows-rs does for WinRT types (it calls
CoIncrementMTAUsagebehind your back when needed). That's only a narrow fallback for WinRT activation though, and in microsoft/windows-rs#1169 they conclude that COM init can't reliably be done automatically and is better set up by the app.My goal for wasapi-rs is to be a thin wrapper that presents WASAPI as it is, without trying to "fix" the Windows APIs. COM init is the app's business, not the job of the audio library.
A crate like cpal should definitely paper over platform quirks, since its whole point is a unified API across platforms. But wasapi-rs is Windows only, and there I think it's better to show the API as it is. Obviously a subjective view, but one I prefer for simplicity and ease of maintenance.
So the plan is to make
deinitializeunsafe in the next release, as suggested above. Further ahead, once we move to windows-core 0.101 (or wherever we land, there is a lot of churn right now), it looks like the COM init helpers can be dropped altogether, since windows-core is getting its owninit_mta()andinit_sta().LastExceed commentedon Nov 22, 2026
Why do you say "papering over" like its a bad thing? As far as I can tell this is exactly how COM initialization is meant to be handled. Its akin to every COM-dependent struct holding an
Arc<COMApartment>so to speak.The problem discussed in that thread is outside the scope of
wasapi-rs, and thus of no concern. If someone overrules the implicit MTA by explicitly joining another apartment, and then tears it down prematurely, that's on them. The init/uninit functions areunsafeafter all.HEnquist commentedon Nov 22, 2026
Ok sorry, "papering over" was probably not the best choice of words. I didn't mean it as something bad. For something like cpal that's exactly the right call. And you're right about microsoft/windows-rs#1169, I had only skimmed that thread.
It's a neat idea, and with
Drophandling the cleanup it's something Rust makes quite doable. But automatic COM init has to be done really well, since a slightly wrong version means crashes that are very hard to track down. For example, the WinRT fallback in windows-rs only takes its own MTA reference if COM isn't already up. If wasapi-rs happens to hold the MTA at that moment and releases it inDroplater, COM can go away under the WinRT objects. Handling every such case properly is a lot of work, for something the app can do with a single line. And once the crate manages COM, every COM problem in an app that uses it becomes a wasapi-rs problem.I looked at some other projects that use WASAPI directly (MPC-HC, Sanear, EarTrumpet, cubeb). They all leave COM init to the app.
So I'll keep it as it is. #97 makes
deinitializeunsafe and documents that COM is up to the app. The init helpers get dropped once windows-core ships its owninit_mta()andinit_sta().LastExceed commentedon Nov 23, 2026
I feel like there is still a misunderstanding here:
This is a
windows-rsproblem, not awasapi-rsproblem.Which, by the way, I'm not even sure applies here at all, since what WinRT is calling internally isEdit: see follow-up commentRoInitialize(), notCoInitialize()(though I admit that I don't fully understand the relation between the two yet)I don't see there being any special cases to handle. If someone tries to reuse a COM apartment they aren't managing themselves, never declares a dependency on it (i.e. incrementing the ref count), and then runs into problems because the COM apartment shuts down too early on the assumption that there is nothing left depending on it, they really only got themselves to blame.
Anyone who wants/needs to can still manage apartments for themselves of course. Explicitly entering an apartment (via
CoInitializeEx()) overrules the implicit MTA association (fromCoIncrementMTAUsage()) after all.HEnquist commentedon Nov 23, 2026
Thanks, I do get the idea, and I can see it being useful, like how your cpal host relies on the implicit MTA init from the WinRT calls it makes. But leaving COM init to the app is a deliberate choice about scope, not a technical one. I want to keep wasapi-rs simple, and this adds a fair bit of complexity for little in return. So I'd like to leave the discussion there.
LastExceed commentedon Nov 23, 2026
Well this is embarassing. You just caught me doing the very thing I just pointed out as bad practice 😅 Oops!
Alright, your call.
HEnquist commentedon Nov 23, 2026
Haha, I was sure you did that on purpose :D
LastExceed commentedon Nov 24, 2026
Small follow-up regarding
RoInitialize, in case it interests you:From https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/august/windows-with-c-the-windows-runtime-application-model