You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I hit an error where some code had created a current-thread tokio runtime and called Runtime::block_on while already inside an async tokio context. As expected, this triggered a panic with a message explaining that blocking API calls are not allowed inside an async context. However, unless the runtime whose block_on call is dropped during the unwinding, a second panic is triggered when the runtime is dropped. The message for the second panic states it's an internal bug.
fnmain(){// initially tested on tokio 1.21.2let outer_rt = tokio::runtime::Builder::new_multi_thread().build().unwrap();let inner_rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
outer_rt.block_on(async{
_ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
inner_rt.block_on(async{});}));});}
And got these messages:
thread 'main' panicked at 'Cannot start a runtime from within a runtime. This happens because a function (like `block_on`) attempted to block the current thread while the thread is being used to drive asynchronous tasks.', /playground/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/runtime/scheduler/current_thread.rs:516:26
thread 'main' panicked at 'Oh no! We never placed the Core back, this is a bug!', /playground/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/runtime/scheduler/current_thread.rs:213:21
With backtraces
thread 'main' panicked at 'Cannot start a runtime from within a runtime. This happens because a function (like `block_on`) attempted to block the current thread while the thread is being used to drive asynchronous tasks.', /playground/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/runtime/scheduler/current_thread.rs:516:26
stack backtrace:
0: std::panicking::begin_panic
at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/panicking.rs:616:12
1: tokio::runtime::enter::enter
at ./.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/runtime/enter.rs:40:9
2: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}
at ./.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/runtime/scheduler/current_thread.rs:516:26
3: tokio::runtime::scheduler::current_thread::CoreGuard::enter::{{closure}}
at ./.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/runtime/scheduler/current_thread.rs:595:57
4: tokio::macros::scoped_tls::ScopedKey<T>::set
at ./.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/macros/scoped_tls.rs:61:9
5: tokio::runtime::scheduler::current_thread::CoreGuard::enter
at ./.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/runtime/scheduler/current_thread.rs:595:27
6: tokio::runtime::scheduler::current_thread::CoreGuard::block_on
at ./.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/runtime/scheduler/current_thread.rs:515:19
7: tokio::runtime::scheduler::current_thread::CurrentThread::block_on
at ./.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/runtime/scheduler/current_thread.rs:161:24
8: tokio::runtime::Runtime::block_on
at ./.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/runtime/mod.rs:490:46
9: playground::main::{{closure}}::{{closure}}
at ./[src/main.rs:12](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021#):13
10: core::ops::function::FnOnce::call_once
at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/core/src/ops/function.rs:248:5
11: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/core/src/panic/unwind_safe.rs:271:9
12: std::panicking::try::do_call
at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/panicking.rs:492:40
13: __rust_try
14: std::panicking::try
at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/panicking.rs:456:19
15: std::panic::catch_unwind
at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/panic.rs:137:14
16: playground::main::{{closure}}
at ./[src/main.rs:11](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021#):13
17: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/core/src/future/mod.rs:91:19
18: tokio::park::thread::CachedParkThread::block_on::{{closure}}
at ./.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/park/thread.rs:267:54
19: tokio::coop::with_budget::{{closure}}
at ./.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/coop.rs:102:9
20: std::thread::local::LocalKey<T>::try_with
at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/thread/local.rs:445:16
21: std::thread::local::LocalKey<T>::with
at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/thread/local.rs:421:9
22: tokio::coop::with_budget
at ./.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/coop.rs:95:5
23: tokio::coop::budget
at ./.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/coop.rs:72:5
24: tokio::park::thread::CachedParkThread::block_on
at ./.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/park/thread.rs:267:31
25: tokio::runtime::enter::Enter::block_on
at ./.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/runtime/enter.rs:152:13
26: tokio::runtime::scheduler::multi_thread::MultiThread::block_on
at ./.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/runtime/scheduler/multi_thread/mod.rs:79:9
27: tokio::runtime::Runtime::block_on
at ./.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/runtime/mod.rs:492:44
28: playground::main
at ./[src/main.rs:10](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021#):5
29: core::ops::function::FnOnce::call_once
at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/core/src/ops/function.rs:248:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
thread 'main' panicked at 'Oh no! We never placed the Core back, this is a bug!', /playground/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/runtime/scheduler/current_thread.rs:213:21
stack backtrace:
0: std::panicking::begin_panic
at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/panicking.rs:616:12
1: <tokio::runtime::scheduler::current_thread::CurrentThread as core::ops::drop::Drop>::drop
at ./.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.21.2/src/runtime/scheduler/current_thread.rs:213:21
2: core::ptr::drop_in_place<tokio::runtime::scheduler::current_thread::CurrentThread>
at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/core/src/ptr/mod.rs:487:1
3: core::ptr::drop_in_place<tokio::runtime::Kind>
at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/core/src/ptr/mod.rs:487:1
4: core::ptr::drop_in_place<tokio::runtime::Runtime>
at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/core/src/ptr/mod.rs:487:1
5: playground::main
at ./[src/main.rs:15](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021#):1
6: core::ops::function::FnOnce::call_once
at /rustc/897e37553bba8b42751c67658967889d11ecd120/library/core/src/ops/function.rs:248:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
I haven't found a way to reproduce this without API misuse, but ideally I would expect that dropping tokio data structures doesn't trigger a panic, even if panics occurred earlier in processing. Note that the original scenario where I hit this involved multiple threads rather than catch_unwind. In that version there wasn't a reference to the tokio runtime being passed across an explicit AssertUnwindSafe boundary in the user code.
The text was updated successfully, but these errors were encountered:
Version
v1.21.2
Platform
Linux 5.15, Rust Playground
Description
I hit an error where some code had created a current-thread tokio runtime and called
Runtime::block_on
while already inside an async tokio context. As expected, this triggered a panic with a message explaining that blocking API calls are not allowed inside an async context. However, unless the runtime whose block_on call is dropped during the unwinding, a second panic is triggered when the runtime is dropped. The message for the second panic states it's an internal bug.I tried this code (playground):
And got these messages:
With backtraces
I haven't found a way to reproduce this without API misuse, but ideally I would expect that dropping tokio data structures doesn't trigger a panic, even if panics occurred earlier in processing. Note that the original scenario where I hit this involved multiple threads rather than
catch_unwind
. In that version there wasn't a reference to the tokio runtime being passed across an explicitAssertUnwindSafe
boundary in the user code.The text was updated successfully, but these errors were encountered: