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

tokio::time::interval is not working #6545

Closed
rcitsz opened this issue May 8, 2024 · 3 comments
Closed

tokio::time::interval is not working #6545

rcitsz opened this issue May 8, 2024 · 3 comments
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-time Module: tokio/time

Comments

@rcitsz
Copy link

rcitsz commented May 8, 2024

Version

root@6a2e14c7eff5:~/projects/rustprj/rs-proxy/server# cargo version
cargo 1.78.0 (54d8815d0 2024-03-26)

root@6a2e14c7eff5:~/projects/rustprj/rs-proxy/server# cargo tree | grep tokio
│   │   │   │   │   │   ├── tokio v1.37.0
│   │   │   │   │   │   │   └── tokio-macros v2.2.0 (proc-macro)
│   │   │   │   │   │   ├── tokio-util v0.7.10
│   │   │   │   │   │   │   ├── tokio v1.37.0 (*)
│   │   │   │   │   ├── tokio v1.37.0 (*)
│   │   │   │   │   ├── tokio v1.37.0 (*)
│   │   │   │   │   ├── tokio-util v0.7.10 (*)
│   │   │   │   ├── tokio v1.37.0 (*)
│   │   │   │   └── tokio-io-timeout v1.2.0
│   │   │   │       └── tokio v1.37.0 (*)
│   │   │   ├── tokio v1.37.0 (*)
│   │   │   ├── tokio-stream v0.1.15
│   │   │   │   └── tokio v1.37.0 (*)
│   ├── tokio v1.37.0 (*)
│   ├── tokio-stream v0.1.15 (*)
│   │   ├── tokio v1.37.0 (*)
│   │   └── tokio-native-tls v0.3.1
│   │       └── tokio v1.37.0 (*)
│   ├── tokio v1.37.0 (*)
│   ├── tokio-native-tls v0.3.1 (*)
├── tokio v1.37.0 (*)
├── tokio-rustls v0.24.1
│   └── tokio v1.37.0 (*)

Platform

root@6a2e14c7eff5:~/projects/rustprj/rs-proxy/server# uname -a
Linux 6a2e14c7eff5 6.6.16-linuxkit #1 SMP Fri Feb 16 11:54:02 UTC 2024 aarch64 aarch64 aarch64 GNU/Linux

Description

Test code is as follows, look at the code comments,thanks.

async fn do_work(s: &mut bool) {
    if *s {
        *s = false;
        tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
    }
}

#[test]
fn test() {
    tokio::runtime::Runtime::new().unwrap().block_on(async {
        tokio::spawn(async move {
            let mut timer = tokio::time::interval(tokio::time::Duration::from_secs(1));
            let mut s = true; 
            loop {

                 // If add this line, the timer will work properly, otherwise, not woking.
                 // tokio::task::yield_now().await;

                tokio::select! {
                    _= timer.tick() => {
                       println!("----tick");
                    }
                _ = do_work(&mut s) => {}
                }
            }
        })
        .await
        .unwrap();
    });
}

I expect the second timer to output the following content every second, but it doesn't work:
-----tick
-----tick
-----tick
-----tick
-----tick
-----tick
...

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

This seems to be a slow task poll issue again. Ref: #6315

As for this issue, although it is a multi-thread runtime, there is actually only one worker thread working, and because it is executing the above loop, no one take care of the time driver.

@wathenjiang
Copy link
Contributor

Considering this thread scheduling behavior, should we add an additional time expiration check before reading the state controlled by the driver?

@Darksonn
Copy link
Contributor

This code is blocking the thread because the select! always exists without yielding to the runtime. You must yield to the runtime to have reasonable behavior.

See also #6544.

@Darksonn Darksonn closed this as not planned Won't fix, can't repro, duplicate, stale May 11, 2024
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