madsim-rs/madsim

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

Closed this issue · 1 comments

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.

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.