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

JoinHandle::abort() panics with "scheduler context missing" when using basic scheduler #3662

Closed
gh2o opened this issue Mar 30, 2021 · 4 comments · Fixed by #3672
Closed

JoinHandle::abort() panics with "scheduler context missing" when using basic scheduler #3662

gh2o opened this issue Mar 30, 2021 · 4 comments · Fixed by #3672
Assignees
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-task Module: tokio/task

Comments

@gh2o
Copy link

gh2o commented Mar 30, 2021

Version
tokio v1.4.0
tokio-macros v1.1.0 (proc-macro)

Platform
Linux mtgav 5.10.4-arch2-1 #1 SMP PREEMPT Fri, 01 Jan 2021 05:29:53 +0000 x86_64 GNU/Linux

Description
When using the basic scheduler/current_thread, JoinHandle::abort() panics if called from a spawn_blocking thread. Same bug as #3157 but with the basic scheduler or flavor = "current_thread". It can be worked around by doing the abort within a tokio::spawn(), but if this limitation is intentional it should be added the documentation.

Code snippet:

use std::time::Duration;

#[tokio::main(flavor = "current_thread")]
async fn main() {
    let j = tokio::spawn(tokio::time::sleep(Duration::from_secs(3)));
    let _ = tokio::task::spawn_blocking(move || {
        std::thread::sleep(Duration::from_secs(1));
        j.abort();
    }).await;
}

Resulting panic:

thread 'tokio-runtime-worker' panicked at 'scheduler context missing', /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/basic_scheduler.rs:376:31
stack backtrace:
   0: rust_begin_unwind
             at /rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca/library/std/src/panicking.rs:495:5
   1: core::panicking::panic_fmt
             at /rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca/library/core/src/panicking.rs:92:14
   2: core::option::expect_failed
             at /rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca/library/core/src/option.rs:1260:5
   3: core::option::Option<T>::expect
             at /rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca/library/core/src/option.rs:349:21
   4: tokio::runtime::basic_scheduler::<impl tokio::runtime::task::Schedule for alloc::sync::Arc<tokio::runtime::basic_scheduler::Shared>>::release::{{closure}}
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/basic_scheduler.rs:376:22
   5: tokio::macros::scoped_tls::ScopedKey<T>::with
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/macros/scoped_tls.rs:72:13
   6: tokio::runtime::basic_scheduler::<impl tokio::runtime::task::Schedule for alloc::sync::Arc<tokio::runtime::basic_scheduler::Shared>>::release
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/basic_scheduler.rs:375:9
   7: tokio::runtime::task::core::Scheduler<S>::release::{{closure}}
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/task/core.rs:197:36
   8: tokio::loom::std::unsafe_cell::UnsafeCell<T>::with
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/loom/std/unsafe_cell.rs:10:9
   9: tokio::runtime::task::core::Scheduler<S>::release
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/task/core.rs:193:9
  10: tokio::runtime::task::harness::SchedulerView<S>::transition_to_terminal
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/task/harness.rs:221:33
  11: tokio::runtime::task::harness::Harness<T,S>::complete
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/task/harness.rs:186:12
  12: tokio::runtime::task::harness::Harness<T,S>::shutdown
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/task/harness.rs:164:9
  13: tokio::runtime::task::raw::shutdown
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/task/raw.rs:130:5
  14: tokio::runtime::task::raw::RawTask::shutdown
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/task/raw.rs:90:18
  15: tokio::runtime::task::join::JoinHandle<T>::abort
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/task/join.rs:195:13
  16: joinhandle_abort::main::{{closure}}::{{closure}}
             at ./src/main.rs:8:9
  17: <tokio::runtime::blocking::task::BlockingTask<T> as core::future::future::Future>::poll
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/blocking/task.rs:42:21
  18: tokio::runtime::task::core::CoreStage<T>::poll::{{closure}}
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/task/core.rs:235:17
  19: tokio::loom::std::unsafe_cell::UnsafeCell<T>::with_mut
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/loom/std/unsafe_cell.rs:14:9
  20: tokio::runtime::task::core::CoreStage<T>::poll
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/task/core.rs:225:13
  21: tokio::runtime::task::harness::poll_future::{{closure}}
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/task/harness.rs:422:23
  22: <std::panic::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca/library/std/src/panic.rs:322:9
  23: std::panicking::try::do_call
             at /rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca/library/std/src/panicking.rs:381:40
  24: __rust_try
  25: std::panicking::try
             at /rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca/library/std/src/panicking.rs:345:19
  26: std::panic::catch_unwind
             at /rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca/library/std/src/panic.rs:396:14
  27: tokio::runtime::task::harness::poll_future
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/task/harness.rs:409:19
  28: tokio::runtime::task::harness::Harness<T,S>::poll_inner
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/task/harness.rs:89:9
  29: tokio::runtime::task::harness::Harness<T,S>::poll
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/task/harness.rs:59:15
  30: tokio::runtime::task::raw::poll
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/task/raw.rs:104:5
  31: tokio::runtime::task::raw::RawTask::poll
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/task/raw.rs:66:18
  32: tokio::runtime::task::Notified<S>::run
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/task/mod.rs:171:9
  33: tokio::runtime::blocking::pool::Inner::run
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/blocking/pool.rs:278:17
  34: tokio::runtime::blocking::pool::Spawner::spawn_thread::{{closure}}
             at /home/gavin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/blocking/pool.rs:258:17
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
@gh2o gh2o added A-tokio Area: The main tokio crate C-bug Category: This is a bug. labels Mar 30, 2021
@Darksonn Darksonn added the M-task Module: tokio/task label Mar 30, 2021
@Darksonn
Copy link
Contributor

No, this isn't intentional. Does it also fail if called from std::thread::spawn?

@gh2o
Copy link
Author

gh2o commented Mar 30, 2021

Yes, that crashes as well:

use std::time::Duration;

#[tokio::main(flavor = "current_thread")]
async fn main() {
    let j = tokio::spawn(tokio::time::sleep(Duration::from_secs(3)));
    let t = std::thread::spawn(move || {
        std::thread::sleep(Duration::from_secs(1));
        j.abort();
    });
    tokio::time::sleep(Duration::from_secs(2)).await;
    assert!(t.join().is_err());
}

@carllerche
Copy link
Member

Hey, would you be able to check if #3672 fixes your issue?

@gh2o
Copy link
Author

gh2o commented Apr 8, 2021

@carllerche @udoprog Yes, it looks like it's fixed now. Thanks y'all!

carllerche pushed a commit that referenced this issue Apr 8, 2021
…3672)

When aborting a task registered with a current-thread scheduler from off runtime, the tasks may not
be immediately unlinked from the runtime. Instead, send a message to the runtime, notifying it to
remove the aborted task.

Fixes #3662
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-task Module: tokio/task
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants