Skip to content

Make the wrappers around agile COM objects Send and Sync #95

Open

Description

@HEnquist

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 "IAgileObject or FTM" the right test for the device test, or is there a case where that isn't enough?
  • Sync for everything, or only where sharing makes sense (AudioClient, AudioClock, the session and meter types)?
  • @LastExceed, your activate_audio_interface in WASAPI-WinRT host RustAudio/cpal#1418 has a timeout. Have you seen activation hang? AudioClient::from_device_path in Add AudioClient::from_device_path #94 waits without one.
  • Anything I've missed?

Activity

  1. LastExceed commented on Nov 21, 2026

    @LastExceed
    Contributor

    Is "IAgileObject or FTM" the right test?

    Yes. Either of those allows ignoring apartment boundaries.

    Sync for everything?

    Yes. I see no reason to omit it.

    Have you seen activation hang?

    No, but cpal's Stream trait mandates a timeout parameter anyway, so I figured I might as well use it.

    AudioClient::from_device_path in #94 waits without one

    I don't think this is something wasapi-rs should decide on behalf of its consumers. Surely Microsoft made this function async for a reason. I really think the wrapper should be async too.

    Anything I've missed?

    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 to AudioClient::from_iaudioclient(), it would enable them to silently violate COM's threading rules without any unsafe code.

    Since this is quite the edge case, I'd recommend simply restricting AudioClient::from_iaudioclient() to agile implementations of IAudioClient, either by contract (making the function unsafe), or by verifying agility at runtime (returning an error or panicing when unfulfilled).

  2. HEnquist commented on Nov 21, 2026

    @HEnquist
    OwnerAuthor

    Thanks, sounds good. "IAgileObject or FTM" it is, and Sync for 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 custom IMMDevice passed to Device::from_immdevice can hand out any client it likes from Activate, and a custom client can do the same from GetService. A runtime check would only catch the honest ones though. #[implement] makes objects agile by default without requiring Send or Sync, so a fake agile object is easy to make in safe code. AgileReference has 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-core plus generated bindings) waits for 0.101 at least, so I'll hold off on this until then.

  3. LastExceed commented on Nov 22, 2026

    @LastExceed
    Contributor

    it's a bit wider than from_iaudioclient. A custom IMMDevice passed to Device::from_immdevice can hand out any client it likes from Activate

    oh wow, I would have never thought of that. But its not a big deal, it just means Device::from_immdevice needs a similar contract.

    A runtime check would only catch the honest ones

    This should not be a concern. If a COM object actively lies about its capabilities, that's on them.

    #[implement] makes objects agile by default without requiring Send or Sync, so a fake agile object is easy to make in safe code. AgileReference has the same problem.

    There is nothing fake about this. Using #[implement] means its a rust object, and thus subject to rust's memory safety.

  4. HEnquist commented on Nov 22, 2026

    @HEnquist
    OwnerAuthor

    it just means Device::from_immdevice needs a similar contract.

    Yep. In #74 that one becomes Device::from_raw, which is already unsafe, so it's just a line in the safety docs.

    If a COM object actively lies about its capabilities, that's on them.

    Agreed.

    Using #[implement] means its a rust object, and thus subject to rust's memory safety.

    I wasn't so sure about that one, so I tried it. #[implement] doesn't require Send or Sync, so an Rc inside one can get hit from two threads in safe code. Through AgileReference it 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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions