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

std::thread::sleep blocks another thread even when it's multi thread and multiple workers #6631

Closed
takassh opened this issue Jun 12, 2024 · 3 comments
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug.

Comments

@takassh
Copy link

takassh commented Jun 12, 2024

Version

tokio v1.38.0
    ├── bytes v1.6.0
    ├── libc v0.2.155
    ├── mio v0.8.11
    │   └── libc v0.2.155
    ├── num_cpus v1.16.0
    │   └── libc v0.2.155
    ├── parking_lot v0.12.3
    │   ├── lock_api v0.4.12
    │   │   └── scopeguard v1.2.0
    │   │   [build-dependencies]
    │   │   └── autocfg v1.3.0
    │   └── parking_lot_core v0.9.10
    │       ├── cfg-if v1.0.0
    │       ├── libc v0.2.155
    │       └── smallvec v1.13.2
    ├── pin-project-lite v0.2.14
    ├── signal-hook-registry v1.4.2
    │   └── libc v0.2.155
    ├── socket2 v0.5.7
    │   └── libc v0.2.155
    └── tokio-macros v2.3.0 (proc-macro)
        ├── proc-macro2 v1.0.85 (*)
        ├── quote v1.0.36 (*)
        └── syn v2.0.66 (*)

Platform

Darwin Takassh-MacBook-Pro.local 23.3.0 Darwin Kernel Version 23.3.0: Wed Dec 20 21:33:31 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T8112 arm64

Description
Hi thank you for watching this issue.
I'm not sure if this is intentional or not but when I run below code, it takes 2 seconds to finish.

use std::time::{Duration, Instant};

#[tokio::main(flavor = "multi_thread", worker_threads = 4)]
async fn main() {
    let start = Instant::now();

    tokio::spawn(async {
        let handle = tokio::spawn(async {
            std::thread::sleep(Duration::from_secs(1));
            println!("task 1 finished")
        });

        std::thread::sleep(Duration::from_secs(1));

        handle.await.unwrap();
    })
    .await
    .unwrap();

    let duration = start.elapsed();
    println!("Time elapsed is: {:?}", duration);
}

On the other hand, when I try this code, it takes 1 second. I just removed outer spawn.

use std::time::{Duration, Instant};

#[tokio::main(flavor = "multi_thread", worker_threads = 4)]
async fn main() {
    let start = Instant::now();

    let handle = tokio::spawn(async {
        std::thread::sleep(Duration::from_secs(1));
        println!("task 1 finished")
    });

    std::thread::sleep(Duration::from_secs(1));

    handle.await.unwrap();

    let duration = start.elapsed();
    println!("Time elapsed is: {:?}", duration);
}

I expected to see the first code is finished around 1 second because std::thread::sleep hangs only current thread.
doc says

Puts the current thread to sleep for at least the specified amount of time.

Sorry if I misunderstand anything but if it's bug I thought I should create an issue to heads up.

@takassh takassh added A-tokio Area: The main tokio crate C-bug Category: This is a bug. labels Jun 12, 2024
@wathenjiang
Copy link
Contributor

Hi. This issue might be duplicated to #6491.

@takassh takassh closed this as completed Jun 12, 2024
@Darksonn
Copy link
Contributor

See also #6315.

@takassh
Copy link
Author

takassh commented Jun 13, 2024

Thank you both!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants