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::timeout does not always return an Error when the timeout is reached or exceeded. #5119

Closed
rthomas opened this issue Oct 24, 2022 · 4 comments
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-time Module: tokio/time

Comments

@rthomas
Copy link
Contributor

rthomas commented Oct 24, 2022

Version
1.21.2

Platform
Darwin Ryans-Air 21.6.0 Darwin Kernel Version 21.6.0: Mon Aug 22 20:20:07 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T8110 arm64

Description
tokio::time::timeout does not always return an Error when the timeout is reached.

When calling long-running sync code in an async block, or async functions that do not yield, then timeout will not return an error, even if the timeout has been exceeded.

use std::time::Instant;
use tokio::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("start");

    tokio::time::timeout(Duration::from_secs(2), async {
        expensive_sync_fn();
        other_async_fn().await;
    })
    .await?;

    println!("done");
    Ok(())
}

fn expensive_sync_fn() {
    let start = Instant::now();
    loop {
        if Instant::now().duration_since(start).as_secs() > 3 {
            break;
        }
    }
}

async fn other_async_fn() {
    println!("other async fn.");
    // Uncommenting this will cause the timeout to return a deadline exceeded error.
    // tokio::time::sleep(Duration::from_millis(10)).await;
}

What I expected to happen is that timeout would return an error due to the timeout being exceeded, even if it could not interrupt the current execution, on completion of the block it should check and return the error.

If this is not possible, then the documentation should be amended to state this behaviour.

@rthomas rthomas added A-tokio Area: The main tokio crate C-bug Category: This is a bug. labels Oct 24, 2022
@Darksonn Darksonn added the M-time Module: tokio/time label Oct 24, 2022
@Darksonn
Copy link
Contributor

With the current behavior, timeout(fut) is cancel-safe when fut is, but if we made this change, that would no longer be the case.

@rthomas
Copy link
Contributor Author

rthomas commented Oct 24, 2022

Thanks @Darksonn I may raise a PR against the docs to highlight this case, as it was unexpected when I was using timeout.

@Darksonn
Copy link
Contributor

Yes, please open a PR.

rthomas added a commit to rthomas/tokio that referenced this issue Oct 26, 2022
@rthomas
Copy link
Contributor Author

rthomas commented Nov 29, 2022

Closing due to referenced PR.

@rthomas rthomas closed this as completed Nov 29, 2022
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

2 participants