Skip to content

#[implement] doesn't require Send + Sync, so safe code can race on a !Sync implementation #5469

Description

#[implement] makes objects agile by default, but doesn't require the implementing type to be Send or Sync. So safe code can end up calling a !Sync implementation from two threads at once.

Here's a repro with no unsafe, two threads cloning the same Rc through IStringable::ToString:

use std::rc::Rc;
use std::sync::{Arc, Barrier};
use std::thread;
use windows::Foundation::{IStringable, IStringable_Impl};
use windows_core::{implement, AgileReference, Result, HSTRING};

#[implement(IStringable)]
struct Big {
    buffer: Rc<Vec<u8>>,
}

impl IStringable_Impl for Big_Impl {
    fn ToString(&self) -> Result<HSTRING> {
        // black_box stops the compiler from folding the clone away
        let buffer = std::hint::black_box(self.buffer.clone());
        std::hint::black_box(buffer[buffer.len() - 1]);
        Ok(HSTRING::new())
    }
}

fn hammer(stringable: &IStringable, start: &Barrier) -> Result<()> {
    start.wait();
    for _ in 0..10_000_000 {
        stringable.ToString()?;
    }
    Ok(())
}

fn main() -> Result<()> {
    loop {
        let stringable: IStringable = Big { buffer: Rc::new(vec![0; 16 << 20]) }.into();
        let agile = AgileReference::new(&stringable)?;
        let start = Arc::new(Barrier::new(2));
        let other_start = start.clone();
        let other = thread::spawn(move || hammer(&agile.resolve()?, &other_start));
        hammer(&stringable, &start)?;
        other.join().unwrap()?;
        println!("survived, trying again");
    }
}

In a release build this crashes with an access violation within a couple of seconds, sometimes STATUS_HEAP_CORRUPTION instead. Lost increments on the Rc count free the buffer while Big still holds it. It's big enough to come straight from VirtualAlloc, so the next read faults. Lost decrements just leak it, that's what the retry loop is for.

AgileReference is just the easy way to show it. Any Windows API that gets hold of the object can call it from its own threads. And Agile = false doesn't help in the MTA. A Cell counter with Agile = false and both threads in the MTA loses about half its increments.

Delegates already require F: Send (EventHandler::new for example), so requiring Send + Sync here would match. That breaks implementations holding a Cell or RefCell though, like the example in #2304.

Tested on Windows 11 arm64 with Rust 1.97.1, with windows 0.62.2 and with current master (0c5b7e7, windows-core 0.101.0). Same result on both.

Activity

  1. added
    windows-implement
    on Nov 23, 2026
  2. HEnquist commented on Nov 25, 2026

    @HEnquist
    Author

    This got closed by #5487, I think by accident. Its description says "This does not close #5469", and GitHub reads "close #5469" in there as a closing keyword.

    The #[implement] problem itself is unchanged. The repro above still builds as is on current master (5a875c0) and crashes within about 3 seconds, every run (access violation, once heap corruption). Could you reopen it?

  3. kennykerr commented on Nov 30, 2026

    @kennykerr
    Collaborator

    The fix is now published in 0.101.0. #[implement] implementations must be 'static + Send + Sync, including with Agile = false, so your Rc example is rejected at compile time.

    You were right that the earlier automatic closure was premature; the implementation bounds were added afterward in #5503.

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

    windows-implement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions