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

Sleeping for 0 ms results in a sleep of 1 ms #6390

Open
baoyachi opened this issue Mar 11, 2024 · 1 comment
Open

Sleeping for 0 ms results in a sleep of 1 ms #6390

baoyachi opened this issue Mar 11, 2024 · 1 comment
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-time Module: tokio/time

Comments

@baoyachi
Copy link
Contributor

Version
List the versions of all tokio crates you are using. The easiest way to get
this information is using cargo tree subcommand:

tokio v1.36.0

Platform
Darwin MacBook-Pro.local 23.2.0 Darwin Kernel Version 23.2.0: Wed Nov 15 21:55:06 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T6020 arm64

Description
I set the code for Duration:: from secs (0) and expect the delay of the tokio sleep to be 0, as if the sleep executed for 1ms. At present, the code that sleep (0) does not sleep does not take effect

use std::time::Duration;
use simple_log::info;
#[tokio::main]
async fn main() {
    simple_log::console("debug").unwrap();
    info!("hello");

    for _ in 1..1000{
        let d = Duration::from_secs(0);
        tokio::time::sleep(d).await;
    }
    info!("world");

}

the output:

2024-03-11 11:54:35.595466000 [INFO] <demo:6>:hello
2024-03-11 11:54:36.756138000 [INFO] <demo:12>:world
@baoyachi baoyachi added A-tokio Area: The main tokio crate C-bug Category: This is a bug. labels Mar 11, 2024
@Darksonn Darksonn added the M-time Module: tokio/time label Mar 11, 2024
@Darksonn Darksonn changed the title tokio time::sleep 0 not work. Sleeping for 0 ms results in a sleep of 1 ms Mar 11, 2024
@carllerche
Copy link
Member

I haven't looked in depth; however, sleep is permitted sleep for an arbitrary amount of time. It isn't so much a bug as a behavior discussion. I would check past issues related to sleep zero. I remember there was an issue where doing so would unexpectedly not yield. I could see a sleep(0) doing a yield_now. My guess is previously, yield_now was not guaranteed to return back to the runtime, so we couldn't use it. I believe it does now.

@baoyachi could you do some historical searching through github for sleep(0) issues and report back?

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. M-time Module: tokio/time
Projects
None yet
Development

No branches or pull requests

3 participants