Repository navigation
Description
Most of the wrappers around COM objects are neither Send nor Sync today, since the windows interface types aren't. Most of the underlying objects are agile though, so they could be. cpal needs this (RustAudio/cpal#1418 wraps AudioClient, AudioClock and Handle in a SendSync<T>), and @LastExceed offered to help in #94, so here is what I found so far.
I checked each wrapped object for IAgileObject, and for whether IMarshal gives the free-threaded marshaler as the unmarshal class. Results are the same when created on an STA and on an MTA thread (Windows 11):
| Wrapper | IAgileObject |
FTM |
|---|---|---|
AudioClient, AudioRenderClient, AudioCaptureClient, AudioClock |
yes | yes |
AudioSessionManager, AudioSessionEnumerator, AudioSessionControl, EventRegistration |
yes | yes |
AudioEffectsManager, AcousticEchoCancellationControl |
yes | yes |
DeviceEnumerator, AudioMeterInformation, DeviceEventRegistration |
no | yes |
Device, DeviceCollection |
no | no |
IMMDevice and IMMDeviceCollection also have no proxy registered, so RoGetAgileReference and CoMarshalInterThreadInterfaceInStream both fail with REGDB_E_IIDNOTREG.
So everything except Device and DeviceCollection could be Send, and probably Sync too. None of this is documented, but Microsoft is very unlikely to make these objects non-agile now, since a lot of existing code relies on using them across threads. So my idea is a plain unsafe impl Send with a safety comment, plus a device test that checks the objects are still agile.
Device and DeviceCollection would stay as they are. To use a device on another thread, send its id and look it up there with DeviceEnumerator::get_device, or create the AudioClient first and send that.
Open questions:
- Is "
IAgileObjector FTM" the right test for the device test, or is there a case where that isn't enough? Syncfor everything, or only where sharing makes sense (AudioClient,AudioClock, the session and meter types)?- @LastExceed, your
activate_audio_interfacein WASAPI-WinRT host RustAudio/cpal#1418 has a timeout. Have you seen activation hang?AudioClient::from_device_pathin Add AudioClient::from_device_path #94 waits without one. - Anything I've missed?
Activity
LastExceed commentedon Nov 21, 2026
Yes. Either of those allows ignoring apartment boundaries.
Yes. I see no reason to omit it.
No, but cpal's
Streamtrait mandates a timeout parameter anyway, so I figured I might as well use it.I don't think this is something
wasapi-rsshould decide on behalf of its consumers. Surely Microsoft made this function async for a reason. I really think the wrapper should be async too.Yes, a soundness hole: While I agree that Microsoft is unlikely to retroactively remove agility from any of these objects, there is no guarantee that the objects we're dealing with are Microsoft's in the first place. COM interfaces can be implemented by anyone, so if someone were to create a non-agile implementation of
IAudioClient, and then handed that toAudioClient::from_iaudioclient(), it would enable them to silently violate COM's threading rules without anyunsafecode.Since this is quite the edge case, I'd recommend simply restricting
AudioClient::from_iaudioclient()to agile implementations ofIAudioClient, either by contract (making the functionunsafe), or by verifying agility at runtime (returning an error or panicing when unfulfilled).HEnquist commentedon Nov 21, 2026
Thanks, sounds good. "
IAgileObjector FTM" it is, andSyncfor everything.About the hole: I did think of it, but only as "Microsoft won't make these non-agile", and figured that was safe to skip. Your point is a different one, and a better one. It's not about Microsoft at all, it's about objects that aren't theirs. And it's a bit wider than
from_iaudioclient. A customIMMDevicepassed toDevice::from_immdevicecan hand out any client it likes fromActivate, and a custom client can do the same fromGetService. A runtime check would only catch the honest ones though.#[implement]makes objects agile by default without requiringSendorSync, so a fake agile object is easy to make in safe code.AgileReferencehas the same problem.windows-rs is churning quite a bit right now, and how raw interfaces get in and out of here depends on where that lands. #74 (switching to
windows-coreplus generated bindings) waits for 0.101 at least, so I'll hold off on this until then.LastExceed commentedon Nov 22, 2026
oh wow, I would have never thought of that. But its not a big deal, it just means
Device::from_immdeviceneeds a similar contract.This should not be a concern. If a COM object actively lies about its capabilities, that's on them.
There is nothing fake about this. Using
#[implement]means its a rust object, and thus subject to rust's memory safety.HEnquist commentedon Nov 22, 2026
Yep. In #74 that one becomes
Device::from_raw, which is alreadyunsafe, so it's just a line in the safety docs.Agreed.
I wasn't so sure about that one, so I tried it.
#[implement]doesn't requireSendorSync, so anRcinside one can get hit from two threads in safe code. ThroughAgileReferenceit ends in an access violation within a couple of seconds. Filed it as microsoft/windows-rs#5469. Not ours to fix, but good to know.