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

Unexpected 'must be called from the context of Tokio runtime' panic when runtime constructed with tokio::runtime::Builder #1837

Closed
olegnn opened this issue Nov 27, 2019 · 3 comments · Fixed by #1839

Comments

@olegnn
Copy link
Contributor

olegnn commented Nov 27, 2019

Version

└── tokio v0.2.1
└── tokio-macros v0.2.0

Platform

19.0.0 Darwin

Subcrates

Description

Runtime built with tokio::runtime::Builder panics at 'must be called from the context of Tokio runtime', however runtime built with tokio::runtime::Runtime works.

This code

use futures::{
    channel::oneshot,
    future::{ok, Future},
};
use tokio::{runtime, spawn};

async fn spawn_tokio<T, F>(future: F) -> T
where
    F: Future<Output = T> + Send + 'static,
    T: Send + 'static,
{
    let (tx, rx) = oneshot::channel();
    spawn(async move {
        let value = future.await;
        tx.send(value).map(|_| ()).unwrap_or_else(|_| ());
    });
    rx.await.unwrap()
}

fn main() {
    runtime::Builder::new().build().unwrap().block_on(async {
        let value = spawn_tokio(ok::<_, u8>(":C")).await.unwrap();

        println!("{}", value);
    });
}

worked with tokio="0.2.0-alpha.6 but now panics at 'must be called from the context of Tokio runtime'.

Backtrace

   0:        0x1049de455 - backtrace::backtrace::libunwind::trace::h07a5ef923c94885c
                               at /Users/runner/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/backtrace-0.3.40/src/backtrace/libunwind.rs:88
   1:        0x1049de455 - backtrace::backtrace::trace_unsynchronized::h6773477e6a83228b
                               at /Users/runner/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/backtrace-0.3.40/src/backtrace/mod.rs:66
   2:        0x1049de455 - std::sys_common::backtrace::_print_fmt::h05975f5de82bcb58
                               at src/libstd/sys_common/backtrace.rs:84
   3:        0x1049de455 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h595dc767ec160053
                               at src/libstd/sys_common/backtrace.rs:61
   4:        0x1049f65d0 - core::fmt::write::he6ee6e945fb8d123
                               at src/libcore/fmt/mod.rs:1030
   5:        0x1049dc82b - std::io::Write::write_fmt::h42a7f0e416eb2649
                               at src/libstd/io/mod.rs:1412
   6:        0x1049e0193 - std::sys_common::backtrace::_print::hb79574707924042d
                               at src/libstd/sys_common/backtrace.rs:65
   7:        0x1049e0193 - std::sys_common::backtrace::print::h5eba32533abd3aca
                               at src/libstd/sys_common/backtrace.rs:50
   8:        0x1049e0193 - std::panicking::default_hook::{{closure}}::h8d913654d6e6d3fc
                               at src/libstd/panicking.rs:188
   9:        0x1049dfe9a - std::panicking::default_hook::h257558b894a9840b
                               at src/libstd/panicking.rs:205
  10:        0x1049e085b - std::panicking::rust_panic_with_hook::hf61cdf67b2bcdd56
                               at src/libstd/panicking.rs:464
  11:        0x1049fa032 - __rust_probestack
  12:        0x1049373a0 - tokio::runtime::global::spawn::{{closure}}::h4f546c0d8281761a
                               at /Users/oleg/Documents/rust-games/<::std::macros::panic macros>:3
  13:        0x104937aad - std::thread::local::LocalKey<T>::try_with::h45c625ea5a530c26
                               at /rustc/618b01f9fa0a6b4e7e2ce5b3409abe104b80c4a8/src/libstd/thread/local.rs:262
  14:        0x1049376b9 - std::thread::local::LocalKey<T>::with::h4d149e5b8ba9abd7
                               at /rustc/618b01f9fa0a6b4e7e2ce5b3409abe104b80c4a8/src/libstd/thread/local.rs:239
  15:        0x104937148 - tokio::runtime::global::spawn::hf05268675ea439b7
                               at /Users/oleg/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/tokio-0.2.1/src/runtime/global.rs:33
  16:        0x104939c91 - tokio::task::spawn::spawn::hb006ed994a0d5c7f
                               at /Users/oleg/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/tokio-0.2.1/src/task/spawn.rs:52
  17:        0x10493e3e7 - rust_games::main::{{closure}}::{{closure}}::spawn_tokio::{{closure}}::h99faea33bc3a87e7
                               at src/main.rs:211
  18:        0x10493afcf - <std::future::GenFuture<T> as core::future::future::Future>::poll::h0cae4793189573bc
                               at /rustc/618b01f9fa0a6b4e7e2ce5b3409abe104b80c4a8/src/libstd/future.rs:44
  19:        0x10493ae65 - std::future::poll_with_tls_context::hed0f0346fdc4d2a6
                               at /rustc/618b01f9fa0a6b4e7e2ce5b3409abe104b80c4a8/src/libstd/future.rs:100
  20:        0x10493da67 - rust_games::main::{{closure}}::{{closure}}::h17799c4c1e8c977d
                               at src/main.rs:217
  21:        0x10493b24f - <std::future::GenFuture<T> as core::future::future::Future>::poll::h74d01ec5b0f589f1
                               at /rustc/618b01f9fa0a6b4e7e2ce5b3409abe104b80c4a8/src/libstd/future.rs:44
  22:        0x10493ada5 - std::future::poll_with_tls_context::h9239e25522b3be55
                               at /rustc/618b01f9fa0a6b4e7e2ce5b3409abe104b80c4a8/src/libstd/future.rs:100
  23:        0x10493dc79 - rust_games::main::{{closure}}::h86f782c154a00366
                               at src/main.rs:204
  24:        0x10493b134 - <std::future::GenFuture<T> as core::future::future::Future>::poll::h54e9dd2fec6449cd
                               at /rustc/618b01f9fa0a6b4e7e2ce5b3409abe104b80c4a8/src/libstd/future.rs:44
  25:        0x10493d60b - tokio::runtime::shell::Shell::block_on::h8cc343d27069b249
                               at /Users/oleg/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/tokio-0.2.1/src/runtime/shell.rs:47
  26:        0x10493d102 - tokio::runtime::Runtime::block_on::{{closure}}::h8f15b67e99720c5f
                               at /Users/oleg/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/tokio-0.2.1/src/runtime/mod.rs:407
  27:        0x10493bdcb - tokio::runtime::spawner::Spawner::enter::h61ca6f3e25f5bd0f
                               at /Users/oleg/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/tokio-0.2.1/src/runtime/spawner.rs:28
  28:        0x10493c606 - tokio::runtime::handle::Handle::enter::{{closure}}::{{closure}}::hbdd6aa3c883ba91b
                               at /Users/oleg/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/tokio-0.2.1/src/runtime/handle.rs:36
  29:        0x10493850d - tokio::time::clock::Clock::enter::h999c2f39f73453a2
                               at /Users/oleg/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/tokio-0.2.1/src/time/clock.rs:30
  30:        0x104946755 - tokio::runtime::time::variant::with_default::h9c23e38560c9196b
                               at /Users/oleg/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/tokio-0.2.1/src/runtime/time.rs:43
  31:        0x10493c558 - tokio::runtime::handle::Handle::enter::{{closure}}::hb9433dafb3cb304b
                               at /Users/oleg/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/tokio-0.2.1/src/runtime/handle.rs:36
  32:        0x104939fb1 - tokio::runtime::blocking::pool::Spawner::enter::{{closure}}::h3da192fa2a1a4d37
                               at /Users/oleg/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/tokio-0.2.1/src/runtime/blocking/pool.rs:191
  33:        0x1049384a2 - std::thread::local::LocalKey<T>::try_with::hf54ed76ce50af9dc
                               at /rustc/618b01f9fa0a6b4e7e2ce5b3409abe104b80c4a8/src/libstd/thread/local.rs:262
  34:        0x104937659 - std::thread::local::LocalKey<T>::with::h027b1518f1bc208c
                               at /rustc/618b01f9fa0a6b4e7e2ce5b3409abe104b80c4a8/src/libstd/thread/local.rs:239
  35:        0x104939eb3 - tokio::runtime::blocking::pool::Spawner::enter::hbfcd1992f396f9b2
                               at /Users/oleg/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/tokio-0.2.1/src/runtime/blocking/pool.rs:176
  36:        0x10493c4ae - tokio::runtime::handle::Handle::enter::h1a3f41836f958aed
                               at /Users/oleg/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/tokio-0.2.1/src/runtime/handle.rs:33
  37:        0x10493d056 - tokio::runtime::Runtime::block_on::h45183a52da4f951d
                               at /Users/oleg/.cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/tokio-0.2.1/src/runtime/mod.rs:406
  38:        0x104944e90 - rust_games::main::hc505ec4a091ea67c
                               at src/main.rs:202
  39:        0x104946842 - std::rt::lang_start::{{closure}}::hf25cc607826cd752
                               at /rustc/618b01f9fa0a6b4e7e2ce5b3409abe104b80c4a8/src/libstd/rt.rs:61
  40:        0x1049e02b8 - std::rt::lang_start_internal::{{closure}}::hf9b835df1027f270
                               at src/libstd/rt.rs:48
  41:        0x1049e02b8 - std::panicking::try::do_call::h476efa41ded5fad9
                               at src/libstd/panicking.rs:287
  42:        0x1049e258f - __rust_maybe_catch_panic
                               at src/libpanic_unwind/lib.rs:81
  43:        0x1049e0bee - std::panicking::try::hc46d31b79e495e36
                               at src/libstd/panicking.rs:265
  44:        0x1049e0bee - std::panic::catch_unwind::h0de5c79799a13cd3
                               at src/libstd/panic.rs:395
  45:        0x1049e0bee - std::rt::lang_start_internal::h94b5a8af14794158
                               at src/libstd/rt.rs:47
  46:        0x104946822 - std::rt::lang_start::h8fe22a233a4ecd6c
                               at /rustc/618b01f9fa0a6b4e7e2ce5b3409abe104b80c4a8/src/libstd/rt.rs:61
  47:        0x104944ef2 - rust_games::main::hc505ec4a091ea67c

However, if use tokio::runtime::Runtime instead of tokio::runtime::Builder, it works 💁

use futures::{
    channel::oneshot,
    future::{ok, Future},
};
use tokio::{runtime, spawn};

async fn spawn_tokio<T, F>(future: F) -> T
where
    F: Future<Output = T> + Send + 'static,
    T: Send + 'static,
{
    let (tx, rx) = oneshot::channel();
    spawn(async move {
        let value = future.await;
        tx.send(value).map(|_| ()).unwrap_or_else(|_| ());
    });
    rx.await.unwrap()
}

fn main() {
    runtime::Runtime::new().unwrap().block_on(async {
        let value = spawn_tokio(ok::<_, u8>(":)")).await.unwrap();

        println!("{}", value);
    });
}
@driftluo
Copy link
Contributor

driftluo commented Nov 27, 2019

Use any of the following to replace your code:

       #[cfg(feature = "rt-threaded")]
        let ret = Builder::new().threaded_scheduler().enable_all().build();

        #[cfg(all(not(feature = "rt-threaded"), feature = "rt-core"))]
        let ret = Builder::new().basic_scheduler().enable_all().build();

        #[cfg(not(feature = "rt-core"))]
        let ret = Builder::new().enable_all().build();

@olegnn
Copy link
Contributor Author

olegnn commented Nov 27, 2019

@driftluo thanks! If it's correct behaviour, then, I suggest, panic message should be changed to something like must be called from the context of Tokio runtime with task scheduler, or even better, if we are trying to run task from runtime with no scheduler: called from Tokio runtime without task scheduler.

@olegnn
Copy link
Contributor Author

olegnn commented Nov 27, 2019

@driftluo third variant doesn't work.

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

Successfully merging a pull request may close this issue.

2 participants