-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Closed
Labels
A-tokioArea: The main tokio crateArea: The main tokio crateC-bugCategory: This is a bug.Category: This is a bug.M-taskModule: tokio/taskModule: tokio/task
Description
Version
1.21.1
Platform
$ uname -a
Linux <privacy snip> 5.15.0-46-generic #49~20.04.1-Ubuntu SMP Thu Aug 4 19:15:44 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Description
Tracing is not implemented properly for local tasks. Consider the following complete example:
async fn run() {
for _ in 0..10 {
tokio::task::spawn_local(async { println!("anon future"); }).await.unwrap();
}
}
#[tokio::main(flavor="current_thread")]
async fn main() {
console_subscriber::init();
let local_set = tokio::task::LocalSet::new();
local_set.run_until(run()).await;
// program needs to remain live for tokio-console to work properly
loop {
tokio::time::sleep(std::time::Duration::from_secs_f32(60.0)).await;
}
}
(Buildable project: tokio-spawn-local-repro.zip, run with RUSTFLAGS="--cfg tokio_unstable" cargo run
)
tokio-console
produces the following output:
From my investigation it appears that the problem stems from a lacking application of #[track_caller]
in tokio/src/task/local.rs
:
#[track_caller]
pub(super) fn spawn_local_inner<F>(future: F, name: Option<&str>) -> JoinHandle<F::Output>
where F: Future + 'static,
F::Output: 'static
{
CURRENT.with(|maybe_cx| { // line 317
match maybe_cx.get() {
None => panic!("`spawn_local` called from outside of a `task::LocalSet`"),
Some(cx) => cx.spawn(future, name)
}
})
}
Notice that the closure has no #[track_caller]
attribute, hence breaking the chain leading up to the call of Location::caller
in Contex::spawn
, meaning all locations will refer to the spawn_local_inner
function body. #[track_caller]
attribute cannot be currently applied to closures (it's an unstable feature), so another approach is necessary.
Metadata
Metadata
Assignees
Labels
A-tokioArea: The main tokio crateArea: The main tokio crateC-bugCategory: This is a bug.Category: This is a bug.M-taskModule: tokio/taskModule: tokio/task