tokio icon indicating copy to clipboard operation
tokio copied to clipboard

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

Open zhuxiujia opened this issue 4 years ago • 10 comments

#[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.

zhuxiujia avatar Dec 20 '19 03:12 zhuxiujia

async_std and tokio are separate runtimes.

sfackler avatar Dec 20 '19 03:12 sfackler

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.

carllerche avatar Dec 20 '19 04:12 carllerche

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

zhuxiujia avatar Jan 15 '20 14:01 zhuxiujia

this issue has any update?

Silentdoer avatar Jan 01 '21 06:01 Silentdoer

this issue has any update?

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

zhuxiujia avatar Jan 01 '21 06:01 zhuxiujia

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

Darksonn avatar Jan 01 '21 11:01 Darksonn

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

b-naber avatar May 17 '22 15:05 b-naber

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.

hawkw avatar May 17 '22 17:05 hawkw

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

zhuxiujia avatar May 19 '22 08:05 zhuxiujia

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.

Darksonn avatar May 19 '22 08:05 Darksonn