Skip to content

future cannot be sent between threads safely #118718

Closed as not planned
Closed as not planned
@levkk

Description

@levkk

Code

#[tokio::main]
async fn main() {
    tokio::task::spawn(async move {
        unknown_variable_used().await;
    });
}


async fn unknown_variable_used() -> anyhow::Result<bool> {
    let s = v;
    Ok(true)
}

Current output

error[E0425]: cannot find value `v` in this scope
  --> src/main.rs:11:13
   |
11 |     let s = v;
   |             ^ not found in this scope

error: future cannot be sent between threads safely
   --> src/main.rs:4:24
    |
4   |       tokio::task::spawn(async move {
    |  ________________________^
5   | |         unknown_variable_used().await;
6   | |     });
    | |_____^ future created by async block is not `Send`
    |
note: opaque type is declared here
   --> src/main.rs:10:37
    |
10  | async fn unknown_variable_used() -> anyhow::Result<bool> {
    |                                     ^^^^^^^^^^^^^^^^^^^^
note: this item depends on auto traits of the hidden type, but may also be registering the hidden type. This is not supported right now. You can try moving the opaque type and the item that actually registers a hidden type into a new submodule
   --> src/main.rs:3:10
    |
3   | async fn main() {
    |          ^^^^
note: future is not `Send` as it awaits another future which is not `Send`
   --> src/main.rs:5:9
    |
5   |         unknown_variable_used().await;
    |         ^^^^^^^^^^^^^^^^^^^^^^^ await occurs here on type `impl Future<Output = Result<bool, anyhow::Error>>`, which is not `Send`
note: required by a bound in `tokio::spawn`
   --> /home/lev/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.34.0/src/task/spawn.rs:166:21
    |
164 |     pub fn spawn<F>(future: F) -> JoinHandle<F::Output>
    |            ----- required by a bound in this function
165 |     where
166 |         F: Future + Send + 'static,
    |                     ^^^^ required by this bound in `spawn`

For more information about this error, try `rustc --explain E0425`.
error: could not compile `showcase` (bin "showcase") due to 2 previous errors

Desired output

error[E0425]: cannot find value `v` in this scope
  --> src/main.rs:11:13
   |
11 |     let s = v;
   |             ^ not found in this scope

Rationale and extra context

Something happened to error output sometime between rustc 1.67 and later. Before, it would just output a sensible short error telling me that I have a syntax error, unknown variable, whatever, and calmly stop. Now, whenever I have a problem with a function inside an async scope, the compiler blows up with pages of text of cascading problems. In my pretty large code base, the error is typically is buried in layers of async functions, so for every single one, I get a separate future cannot be sent between threads safely (think 20 errors I have to scroll through until I get to my actual error).

The devex for async programming is now pretty much miserable. I'll settle for a "stop at first error" flag, e.g. #27189

Other cases

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions