rust-lang / rust-lang/rust

Async function's future not Send even when non-Send type is not alive across await expression

Open
#151,942 3 comments 0 reactions 1 assignee View on GitHub

@tmandry is already working on this.

Since Apr 9, 2026.

A-async-await A-auto-traits C-bug WG-async
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

The following program fails to compile, complaining that the future for bar is not Send because it contains a MutexGuard:

use std::sync::Mutex;

async fn foo() {
    todo!();
}

async fn bar(mutex: &Mutex<()>) {
    let mut guard = mutex.lock().unwrap();
    loop {
        drop(guard);
        foo().await;
        guard = mutex.lock().unwrap();
    }
}

fn check_send() {
    fn require_send<T: Send>(_: T) {}
    require_send(bar(&Mutex::new(())));
}

(Playground)

The error:

error: future cannot be sent between threads safely
  --> src/lib.rs:18:18
   |
18 |     require_send(bar(&Mutex::new(())));
   |                  ^^^^^^^^^^^^^^^^^^^^ future returned by `bar` is not `Send`
   |
   = help: within `impl Future<Output = ()>`, the trait `Send` is not implemented for `std::sync::MutexGuard<'_, ()>`
note: future is not `Send` as this value is used across an await
  --> src/lib.rs:11:15
   |
 8 |     let mut guard = mutex.lock().unwrap();
   |         --------- has type `std::sync::MutexGuard<'_, ()>` which is not `Send`
...
11 |         foo().await;
   |               ^^^^^ await occurs here, with `mut guard` maybe used later

However this code should be accepted—there is not actually any MutexGuard alive across the await expression, so it is sound. Rejecting it makes it hard to write and use async condition variable types.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.