Skip to content

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

@jacobsa

Description

@jacobsa

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.

Metadata

Metadata

Assignees

Labels

A-async-awaitArea: Async & AwaitA-auto-traitsArea: auto traits (e.g., `auto trait Send {}`)C-bugCategory: This is a bug.WG-asyncWorking group: Async & await

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions