madsim-rs / madsim

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

wrong `tokio::runtime::Runtime::enter` implementation #179

Open TennyZhuang opened 8 months ago

TennyZhuang commented 8 months ago

Related #175

Reproduce

[package]
name = "test-tokio-madsim"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tokio = { version = "0.2", package = "madsim-tokio", features = ["rt", "macros", "rt-multi-thread"] }
fn main() {
    let rt = tokio::runtime::Runtime::new().unwrap();
    let _guard = rt.enter();
    tokio::spawn(async {
        println!("Hello, world!");
    });
}
thread 'main' panicked at /Users/tianyizhuang/.cargo/registry/src/index.crates.io-6f17d22bba15001f/madsim-0.2.23/src/sim/runtime/context.rs:27:44:
there is no reactor running, must be called from the context of a Madsim runtime
stack backtrace:
   0: rust_begin_unwind
             at /rustc/4578435e1695863d921c7763d5a0add98f8e3869/library/std/src/panicking.rs:597:5
   1: core::panicking::panic_fmt
             at /rustc/4578435e1695863d921c7763d5a0add98f8e3869/library/core/src/panicking.rs:72:14
   2: core::panicking::panic_display
             at /rustc/4578435e1695863d921c7763d5a0add98f8e3869/library/core/src/panicking.rs:178:5
   3: core::panicking::panic_str
             at /rustc/4578435e1695863d921c7763d5a0add98f8e3869/library/core/src/panicking.rs:152:5
   4: core::option::expect_failed
             at /rustc/4578435e1695863d921c7763d5a0add98f8e3869/library/core/src/option.rs:1979:5
   5: core::option::Option<T>::expect
             at /rustc/4578435e1695863d921c7763d5a0add98f8e3869/library/core/src/option.rs:888:21
   6: madsim::sim::runtime::context::current_task::{{closure}}
             at /Users/tianyizhuang/.cargo/registry/src/index.crates.io-6f17d22bba15001f/madsim-0.2.23/src/sim/runtime/context.rs:27:22
   7: std::thread::local::LocalKey<T>::try_with
             at /rustc/4578435e1695863d921c7763d5a0add98f8e3869/library/std/src/thread/local.rs:270:16
   8: std::thread::local::LocalKey<T>::with
             at /rustc/4578435e1695863d921c7763d5a0add98f8e3869/library/std/src/thread/local.rs:246:9
   9: madsim::sim::runtime::context::current_task
             at /Users/tianyizhuang/.cargo/registry/src/index.crates.io-6f17d22bba15001f/madsim-0.2.23/src/sim/runtime/context.rs:27:5
  10: madsim::sim::task::Spawner::current
             at /Users/tianyizhuang/.cargo/registry/src/index.crates.io-6f17d22bba15001f/madsim-0.2.23/src/sim/task/mod.rs:577:20
  11: madsim::sim::task::spawn
             at /Users/tianyizhuang/.cargo/registry/src/index.crates.io-6f17d22bba15001f/madsim-0.2.23/src/sim/task/mod.rs:655:5
  12: test_tokio_madsim::main
             at ./src/main.rs:4:5
  13: core::ops::function::FnOnce::call_once
             at /rustc/4578435e1695863d921c7763d5a0add98f8e3869/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
TennyZhuang commented 8 months ago

Where do you set it? Found it

static TASK: RefCell<Option<Arc<TaskInfo>>> = RefCell::new(None);
TennyZhuang commented 8 months ago

Interesting, I found that if we really want to implement that, we have to store a runtime in the thread_local.

wangrunji0408 commented 8 months ago

Right. We need to let it be aware that multiple logical runtimes may coexist at the same time.

TennyZhuang commented 8 months ago

Right. We need to let it be aware that multiple logical runtimes may coexist at the same time.

Seems a large refactor. Is there any workaround?