tokio-rs / tokio

A runtime for writing reliable asynchronous applications with Rust. Provides I/O, networking, scheduling, timers, ...
https://tokio.rs
MIT License
26.59k stars 2.45k forks source link

Interval panics with Duration::MAX #6634

Closed nazar-pc closed 3 months ago

nazar-pc commented 3 months ago

Version 1.37.0

Platform Ubuntu x86-64 with 6.9.4 kernel

Description Tokio timers do not handle overflow gracefully, I hit this multiple times already so decided to finally report it upstream.

I tried this code:

#[tokio::main]
async fn main() {
    tokio::time::interval(std::time::Duration::MAX).tick().await;
}

I expected to see this happen: effectively timer is idle indefinitely.

Instead, this happened:

thread 'main' panicked at library/std/src/time.rs:417:33:
overflow when adding duration to instant

Instant + Duration should be saturating to prevent this issue.

wathenjiang commented 3 months ago

Hi. I believe it has be fixed in 1.38.0. Please see https://github.com/tokio-rs/tokio/releases/tag/tokio-1.38.0 and https://github.com/tokio-rs/tokio/pull/6487.

nazar-pc commented 3 months ago

Indeed, what a coincidence, thanks!

wathenjiang commented 3 months ago

But there is still a possibility that 1.38.0 may panic, it eventually fixed in https://github.com/tokio-rs/tokio/pull/6612.