Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

future cannot be sent between threads safely #118718

Closed
levkk opened this issue Dec 7, 2023 · 2 comments
Closed

future cannot be sent between threads safely #118718

levkk opened this issue Dec 7, 2023 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@levkk
Copy link

levkk commented Dec 7, 2023

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

@levkk levkk added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 7, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 7, 2023
@compiler-errors
Copy link
Member

Thanks for the error report. For the record, I fixed this in #115294, which is in stable Rust 1.74. I encourage you to upgrade your toolchain 😸

@compiler-errors compiler-errors closed this as not planned Won't fix, can't repro, duplicate, stale Dec 7, 2023
@levkk
Copy link
Author

levkk commented Dec 7, 2023

Thank you so much! Really appreciate your work.

@fmease fmease removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants