Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
TennyZhuang opened this issue Oct 27, 2023 · 4 comments
Open

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

TennyZhuang opened this issue Oct 27, 2023 · 4 comments

Comments

@TennyZhuang
Copy link
Contributor

TennyZhuang commented Oct 27, 2023

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
Copy link
Contributor Author

TennyZhuang commented Oct 27, 2023

Where do you set it? Found it

static TASK: RefCell<Option<Arc<TaskInfo>>> = RefCell::new(None);

@TennyZhuang
Copy link
Contributor Author

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

@wangrunji0408
Copy link
Member

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

@TennyZhuang
Copy link
Contributor Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants