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 generation can not recognize drop function #111636

Closed
YjyJeff opened this issue May 16, 2023 · 1 comment
Closed

Future generation can not recognize drop function #111636

YjyJeff opened this issue May 16, 2023 · 1 comment
Labels
C-bug Category: This is a bug.

Comments

@YjyJeff
Copy link

YjyJeff commented May 16, 2023

drop function does not end the lifetime of a variable in in async block

I tried this code:

async fn hello() {
    println!("hello");
}

tokio::spawn(async move {
    let mut rng = rand::thread_rng();
    let v = rng.gen_range(0..100);
    drop(rng);
    hello().await;
    v
});

The compiler compiles it with an error

error: future cannot be sent between threads safely
   --> src/main.rs:6:26
    |
6   |       let t = tokio::spawn(async move {
    |  __________________________^
7   | |         let mut rng = rand::thread_rng();
8   | |         let v = rng.gen_range(0..100);
9   | |         drop(rng);
10  | |         hello().await;
11  | |         v
12  | |     });
    | |_____^ future created by async block is not `Send`
    |
    = help: within `[async block@src/main.rs:6:26: 12:6]`, the trait `std::marker::Send` is not implemented for `Rc<UnsafeCell<ReseedingRng<rand_chacha::chacha::ChaCha12Core, OsRng>>>`
note: future is not `Send` as this value is used across an await
   --> src/main.rs:10:16
    |
7   |         let mut rng = rand::thread_rng();
    |             ------- has type `ThreadRng` which is not `Send`
...
10  |         hello().await;
    |                ^^^^^^ await occurs here, with `mut rng` maybe used later
11  |         v
12  |     });
    |     - `mut rng` is later dropped here
note: required by a bound in `tokio::spawn`
   --> /Users/jefffffyang/.cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/tokio-1.21.2/src/task/spawn.rs:127:21
    |
127 |         T: Future + Send + 'static,
    |                     ^^^^ required by this bound in `spawn`

I dropped the rng variable in line 9, but the compiler says rng is dropped in line 12. I also write the following code to confirm it is caused by the drop function:

tokio::spawn(async move {
    let v = {
        let mut rng = rand::thread_rng();
        rng.gen_range(0..100)
    };
    hello().await;
    v
});

In the above function, rng is dropped by the compiler implicitly. It works well.

Meta

rustc --version --verbose:

rustc 1.71.0-nightly (ce5919fce 2023-05-15)
@YjyJeff YjyJeff added the C-bug Category: This is a bug. label May 16, 2023
@SkiFire13
Copy link
Contributor

This is a known issue, see #69663

@YjyJeff YjyJeff closed this as completed May 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants