forked from tokio-rs/tokio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
task: add track_caller to public APIs
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: tokio-rs#4413
- Loading branch information
Showing
4 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#![warn(rust_2018_idioms)] | ||
#![cfg(all(feature = "full", not(target_os = "wasi")))] | ||
|
||
use futures::future; | ||
use std::error::Error; | ||
use tokio::{runtime::Builder, spawn, task}; | ||
|
||
mod support { | ||
pub mod panic; | ||
} | ||
use support::panic::test_panic; | ||
|
||
#[test] | ||
fn local_set_block_on_panic_caller() -> Result<(), Box<dyn Error>> { | ||
let panic_location_file = test_panic(|| { | ||
let rt = Builder::new_current_thread().enable_all().build().unwrap(); | ||
let local = task::LocalSet::new(); | ||
|
||
rt.block_on(async { | ||
local.block_on(&rt, future::pending::<()>()); | ||
}); | ||
}); | ||
|
||
// The panic location should be in this file | ||
assert_eq!(&panic_location_file.unwrap(), file!()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn spawn_panic_caller() -> Result<(), Box<dyn Error>> { | ||
let panic_location_file = test_panic(|| { | ||
spawn(future::pending::<()>()); | ||
}); | ||
|
||
// The panic location should be in this file | ||
assert_eq!(&panic_location_file.unwrap(), file!()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn local_key_sync_scope_panic_caller() -> Result<(), Box<dyn Error>> { | ||
tokio::task_local! { | ||
static NUMBER: u32; | ||
} | ||
|
||
let panic_location_file = test_panic(|| { | ||
NUMBER.sync_scope(1, || { | ||
NUMBER.with(|_| { | ||
let _ = NUMBER.sync_scope(1, || {}); | ||
}); | ||
}); | ||
}); | ||
|
||
// The panic location should be in this file | ||
assert_eq!(&panic_location_file.unwrap(), file!()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn local_key_with_panic_caller() -> Result<(), Box<dyn Error>> { | ||
tokio::task_local! { | ||
static NUMBER: u32; | ||
} | ||
|
||
let panic_location_file = test_panic(|| { | ||
NUMBER.with(|_| {}); | ||
}); | ||
|
||
// The panic location should be in this file | ||
assert_eq!(&panic_location_file.unwrap(), file!()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn local_key_get_panic_caller() -> Result<(), Box<dyn Error>> { | ||
tokio::task_local! { | ||
static NUMBER: u32; | ||
} | ||
|
||
let panic_location_file = test_panic(|| { | ||
NUMBER.get(); | ||
}); | ||
|
||
// The panic location should be in this file | ||
assert_eq!(&panic_location_file.unwrap(), file!()); | ||
|
||
Ok(()) | ||
} |