Skip to content

Commit 5288e1e

Browse files
authored
task: add track_caller to public APIs (#4848)
Functions that may panic can be annotated with `#[track_caller]` so that in the event of a panic, the function where the user called the panicking function is shown instead of the file and line within Tokio source. This change adds `#[track_caller]` to all the public APIs in the task module of the tokio crate where the documentation describes how the function may panic due to incorrect context or inputs. In cases where `#[track_caller]` does not work, it has been left out. For example, it currently does not work on async functions, blocks, or closures. So any call stack that passes through one of these before reaching the actual panic is not able to show the calling site outside of tokio as the panic location. The following functions have call stacks that pass through closures: * `task::block_in_place` * `task::local::spawn_local` Tests are included to cover each potentially panicking function. The following functions already had `#[track_caller]` applied everywhere it was needed and only tests have been added: * `task::spawn` * `task::LocalKey::sync_scope` Refs: #4413
1 parent 56be528 commit 5288e1e

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

tokio/src/runtime/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ cfg_rt! {
475475
/// ```
476476
///
477477
/// [handle]: fn@Handle::block_on
478+
#[track_caller]
478479
pub fn block_on<F: Future>(&self, future: F) -> F::Output {
479480
#[cfg(all(tokio_unstable, feature = "tracing"))]
480481
let future = crate::util::trace::task(future, "block_on", None, task::Id::next().as_u64());

tokio/src/task/local.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ impl LocalSet {
487487
/// [`Runtime::block_on`]: method@crate::runtime::Runtime::block_on
488488
/// [in-place blocking]: fn@crate::task::block_in_place
489489
/// [`spawn_blocking`]: fn@crate::task::spawn_blocking
490+
#[track_caller]
490491
#[cfg(feature = "rt")]
491492
#[cfg_attr(docsrs, doc(cfg(feature = "rt")))]
492493
pub fn block_on<F>(&self, rt: &crate::runtime::Runtime, future: F) -> F::Output

tokio/src/task/task_local.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ impl<T: Copy + 'static> LocalKey<T> {
287287
/// # Panics
288288
///
289289
/// This function will panic if the task local doesn't have a value set.
290+
#[track_caller]
290291
pub fn get(&'static self) -> T {
291292
self.with(|v| *v)
292293
}
@@ -425,6 +426,7 @@ enum ScopeInnerErr {
425426
}
426427

427428
impl ScopeInnerErr {
429+
#[track_caller]
428430
fn panic(&self) -> ! {
429431
match self {
430432
Self::BorrowError => panic!("cannot enter a task-local scope while the task-local storage is borrowed"),

tokio/tests/task_panic.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#![warn(rust_2018_idioms)]
2+
#![cfg(all(feature = "full", not(target_os = "wasi")))]
3+
4+
use futures::future;
5+
use std::error::Error;
6+
use tokio::{runtime::Builder, spawn, task};
7+
8+
mod support {
9+
pub mod panic;
10+
}
11+
use support::panic::test_panic;
12+
13+
#[test]
14+
fn local_set_block_on_panic_caller() -> Result<(), Box<dyn Error>> {
15+
let panic_location_file = test_panic(|| {
16+
let rt = Builder::new_current_thread().enable_all().build().unwrap();
17+
let local = task::LocalSet::new();
18+
19+
rt.block_on(async {
20+
local.block_on(&rt, future::pending::<()>());
21+
});
22+
});
23+
24+
// The panic location should be in this file
25+
assert_eq!(&panic_location_file.unwrap(), file!());
26+
27+
Ok(())
28+
}
29+
30+
#[test]
31+
fn spawn_panic_caller() -> Result<(), Box<dyn Error>> {
32+
let panic_location_file = test_panic(|| {
33+
spawn(future::pending::<()>());
34+
});
35+
36+
// The panic location should be in this file
37+
assert_eq!(&panic_location_file.unwrap(), file!());
38+
39+
Ok(())
40+
}
41+
42+
#[test]
43+
fn local_key_sync_scope_panic_caller() -> Result<(), Box<dyn Error>> {
44+
tokio::task_local! {
45+
static NUMBER: u32;
46+
}
47+
48+
let panic_location_file = test_panic(|| {
49+
NUMBER.sync_scope(1, || {
50+
NUMBER.with(|_| {
51+
let _ = NUMBER.sync_scope(1, || {});
52+
});
53+
});
54+
});
55+
56+
// The panic location should be in this file
57+
assert_eq!(&panic_location_file.unwrap(), file!());
58+
59+
Ok(())
60+
}
61+
62+
#[test]
63+
fn local_key_with_panic_caller() -> Result<(), Box<dyn Error>> {
64+
tokio::task_local! {
65+
static NUMBER: u32;
66+
}
67+
68+
let panic_location_file = test_panic(|| {
69+
NUMBER.with(|_| {});
70+
});
71+
72+
// The panic location should be in this file
73+
assert_eq!(&panic_location_file.unwrap(), file!());
74+
75+
Ok(())
76+
}
77+
78+
#[test]
79+
fn local_key_get_panic_caller() -> Result<(), Box<dyn Error>> {
80+
tokio::task_local! {
81+
static NUMBER: u32;
82+
}
83+
84+
let panic_location_file = test_panic(|| {
85+
NUMBER.get();
86+
});
87+
88+
// The panic location should be in this file
89+
assert_eq!(&panic_location_file.unwrap(), file!());
90+
91+
Ok(())
92+
}

0 commit comments

Comments
 (0)