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

async fn and async block not support task::current().id(),how to get Coroutines id? #1996

Closed
zhuxiujia opened this issue Dec 20, 2019 · 10 comments · Fixed by #5171
Closed

async fn and async block not support task::current().id(),how to get Coroutines id? #1996

zhuxiujia opened this issue Dec 20, 2019 · 10 comments · Fixed by #5171
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-runtime Module: tokio/runtime

Comments

@zhuxiujia
Copy link

#[tokio::main]
async fn main() {
   // application comes here
   println!("id = {:?}", task::current().id());
}
thread 'main' panicked at '`task::current()` called outside the context of a task', src/libcore/option.rs:1190:5
stack backtrace:
   0: std::sys_common::at_exit_imp::push
   1: core::fmt::ArgumentV1::show_usize
   2: std::io::Write::write_fmt
   3: std::panicking::default_hook::{{closure}}
   4: std::panicking::default_hook
   5: <std::panicking::begin_panic::PanicPayload<A> as core::panic::BoxMeUp>::get
   6: std::panicking::continue_panic_fmt
   7: std::panicking::try::do_call
   8: std::panicking::begin_panic
   9: std::panicking::begin_panic
  10: core::option::Option<T>::expect
             at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libcore/option.rs:345
  11: async_std::task::current::current
             at /Users/zhuxiujie/.cargo/registry/src/github.com-1ecc6299db9ec823/async-std-1.2.0/src/task/current.rs:26
  12: rbatis::main::{{closure}}
             at src/main.rs:68
  13: <std::future::GenFuture<T> as core::future::future::Future>::poll::{{closure}}
             at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libstd/future.rs:43
  14: std::future::set_task_context
             at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libstd/future.rs:79
  15: <std::future::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libstd/future.rs:43
  16: tokio::runtime::enter::Enter::block_on
             at /Users/zhuxiujie/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.4/src/runtime/enter.rs:100
  17: tokio::runtime::thread_pool::ThreadPool::block_on::{{closure}}
             at /Users/zhuxiujie/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.4/src/runtime/thread_pool/mod.rs:102
  18: tokio::runtime::global::with_state::{{closure}}
             at /Users/zhuxiujie/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.4/src/runtime/global.rs:100
  19: std::thread::local::LocalKey<T>::try_with
             at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libstd/thread/local.rs:262
  20: std::thread::local::LocalKey<T>::with
             at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libstd/thread/local.rs:239
  21: tokio::runtime::global::with_state
             at /Users/zhuxiujie/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.4/src/runtime/global.rs:83
  22: tokio::runtime::global::with_thread_pool
             at /Users/zhuxiujie/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.4/src/runtime/global.rs:75
  23: tokio::runtime::thread_pool::spawner::Spawner::enter
             at /Users/zhuxiujie/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.4/src/runtime/thread_pool/spawner.rs:44
  24: tokio::runtime::thread_pool::ThreadPool::block_on
             at /Users/zhuxiujie/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.4/src/runtime/thread_pool/mod.rs:100
  25: tokio::runtime::Runtime::block_on::{{closure}}
             at /Users/zhuxiujie/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.4/src/runtime/mod.rs:421
  26: tokio::runtime::global::with_state::{{closure}}
             at /Users/zhuxiujie/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.4/src/runtime/global.rs:100
  27: std::thread::local::LocalKey<T>::try_with
             at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libstd/thread/local.rs:262
  28: std::thread::local::LocalKey<T>::with
             at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libstd/thread/local.rs:239
  29: tokio::runtime::global::with_state
             at /Users/zhuxiujie/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.4/src/runtime/global.rs:83
  30: tokio::runtime::global::with_thread_pool
             at /Users/zhuxiujie/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.4/src/runtime/global.rs:75
  31: tokio::runtime::thread_pool::spawner::Spawner::enter
             at /Users/zhuxiujie/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.4/src/runtime/thread_pool/spawner.rs:44
  32: tokio::runtime::spawner::Spawner::enter
             at /Users/zhuxiujie/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.4/src/runtime/spawner.rs:32
  33: tokio::runtime::handle::Handle::enter::{{closure}}::{{closure}}
             at /Users/zhuxiujie/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.4/src/runtime/handle.rs:36
  34: tokio::time::clock::Clock::enter
             at /Users/zhuxiujie/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.4/src/time/clock.rs:30
  35: tokio::runtime::time::variant::with_default
             at /Users/zhuxiujie/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.4/src/runtime/time.rs:43
  36: tokio::runtime::handle::Handle::enter::{{closure}}
             at /Users/zhuxiujie/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.4/src/runtime/handle.rs:36
  37: tokio::runtime::blocking::pool::Spawner::enter::{{closure}}
             at /Users/zhuxiujie/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.4/src/runtime/blocking/pool.rs:191
  38: std::thread::local::LocalKey<T>::try_with
             at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libstd/thread/local.rs:262
  39: std::thread::local::LocalKey<T>::with
             at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libstd/thread/local.rs:239
  40: tokio::runtime::blocking::pool::Spawner::enter
             at /Users/zhuxiujie/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.4/src/runtime/blocking/pool.rs:176
  41: tokio::runtime::handle::Handle::enter
             at /Users/zhuxiujie/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.4/src/runtime/handle.rs:33
  42: tokio::runtime::Runtime::block_on
             at /Users/zhuxiujie/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.4/src/runtime/mod.rs:416
  43: rbatis::main
             at src/main.rs:65
  44: std::rt::lang_start::{{closure}}
             at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libstd/rt.rs:64
  45: std::panicking::try::do_call
  46: panic_unwind::dwarf::eh::read_encoded_pointer
  47: <std::panicking::begin_panic::PanicPayload<A> as core::panic::BoxMeUp>::get
  48: std::rt::lang_start
             at /rustc/4560ea788cb760f0a34127156c78e2552949f734/src/libstd/rt.rs:64
  49: rbatis::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
@sfackler
Copy link
Contributor

async_std and tokio are separate runtimes.

@carllerche
Copy link
Member

We probably should add a task identifier. It would be fairly simple I think. I am not sure what API we would want to use though.

@zhuxiujia
Copy link
Author

We probably should add a task identifier. It would be fairly simple I think. I am not sure what API we would want to use though.

Will the new version be added?
just like async_std

@Darksonn Darksonn added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-runtime Module: tokio/runtime labels Jul 25, 2020
@Silentdoer
Copy link

this issue has any update?

@zhuxiujia
Copy link
Author

this issue has any update?

There seems to be no response,But task ids are important for tracking tasks

@Darksonn
Copy link
Contributor

Darksonn commented Jan 1, 2021

No, nobody has taken the time to work on it.

@b-naber
Copy link
Contributor

b-naber commented May 17, 2022

Think this can be closed. Task does have an Id field now, which can be accessed through JoinHandle.

@hawkw
Copy link
Member

hawkw commented May 17, 2022

Think this can be closed. Task does have an Id field now, which can be accessed through JoinHandle.

Currently, the JoinHandle only allows accessing the task ID from outside of the task, but we could certainly add an API for accessing the current task's ID from within that task.

@zhuxiujia
Copy link
Author

Think this can be closed. Task does have an Id field now, which can be accessed through JoinHandle.

Currently, the JoinHandle only allows accessing the task ID from outside of the task, but we could certainly add an API for accessing the current task's ID from within that task.

I agree with you

@Darksonn
Copy link
Contributor

I'm fine with adding a method to query the current task's id. If anyone wants to submit a PR, that would be great! Let us know on our discord server if you need help with implementing it.

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-feature-request Category: A feature request. M-runtime Module: tokio/runtime
Projects
None yet
7 participants