madsim-rs / madsim

Magical Deterministic Simulator for distributed systems in Rust.
Apache License 2.0
662 stars 48 forks source link

`tokio::time::sleep(Duration::ZERO)` is not simulated properly #198

Closed tomasol closed 6 months ago

tomasol commented 6 months ago

Having two tasks, t1 sleeps in a loop with a non-zero duration, t2 with Duration::ZERO, I observe that t2 always wins and t1 does not make any progress. Switching t2 to yield_now instead of sleep has no effect. IRL it looks like sleep(Duration::ZERO) always yields for at least 1 ms.

wangrunji0408 commented 6 months ago

Thanks for your feedback. tokio::time::sleep(Duration::ZERO) is an no-op in the current simulation. yield_now simply yields back to the scheduler and does not move time forward. Therefore, in your case, task2 is in a spin loop and the simulation is stuck.

I'll fix sleep and make it behave consistent with tokio.