tokio-rs / tokio

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

tokio: provide an API for controlling non-determinism. #4879

Open carllerche opened 2 years ago

carllerche commented 2 years ago

Is your feature request related to a problem? Please describe.

select! uses randomness to prevent selecting branches in the same order. However, there are times that we do not want non-determism, e.g. using loom.

Describe the solution you'd like

There should be an API for seeding any random number generation as part of the runtime Builder API.

A quick sketch:

Sketch:

// Struct provided by Tokio in `tokio::runtime`

pub struct RngSeed { ... }

runtime::Builder::new_current_thread()
    .rng_seed(RngSeed::from_bytes(....))
    .build()
akoshelev commented 1 year ago

I am curious if it would make sense to pass the PRNG to the macro select! directly rather than seeding it through the runtime. One use case that I have is to be able to use third-party schedulers, like the ones provided by Shuttle framework

Darksonn commented 1 year ago

How would passing a custom rng help with using third-party schedulers?

akoshelev commented 1 year ago

Third-party schedulers do not use Tokio runtime, so it does not seem possible to seed select! rng in this case? That is the case for concurrency tests where Tokio scheduler is replaced to run concurrency tests, while code that is being tested uses tokio::select! macro