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

"This is a bug" panic after unwind of invalid Runtime::block_on() call #5177

Closed
wabain opened this issue Nov 8, 2022 · 2 comments
Closed
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-runtime Module: tokio/runtime

Comments

@wabain
Copy link
Contributor

wabain commented Nov 8, 2022

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):

fn main() {
    // initially tested on tokio 1.21.2

    let 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.

@wabain wabain added A-tokio Area: The main tokio crate C-bug Category: This is a bug. labels Nov 8, 2022
@Darksonn Darksonn added the M-runtime Module: tokio/runtime label Nov 9, 2022
@Darksonn
Copy link
Contributor

Darksonn commented Nov 9, 2022

Thank you for your report. This is indeed a bug.

@Darksonn
Copy link
Contributor

Darksonn commented Nov 9, 2022

It seems like this has already been fixed on master in #5163.

@Darksonn Darksonn closed this as completed Nov 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-runtime Module: tokio/runtime
Projects
None yet
Development

No branches or pull requests

2 participants