-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
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 non-unstable public APIs in Tokio core where the documentation describes how the function may panic due to incorrect context or inputs. Since each internal function needs to be annotated down to the actual panic, it makes sense to start in Tokio core functionality. Tests are needed to ensure that all the annotations remain in place in case internal refactoring occurs. The test installs a panic hook to extract the file location from the `PanicInfo` struct and clone it up to the outer scope to check that the panic was indeed reported from within the test file. The downside to this approach is that the panic hook is global while set and so we need a lot of extra functionality to effectively serialize the tests so that only a single panic can occur at a time. The annotation of `block_on` was removed as it did not work. It appears to be impossible to correctly chain track caller when the call stack to the panic passes through clojures, as the track caller annotation can only be applied to functions. Also, the panic message itself is very descriptive.
- Loading branch information
Showing
6 changed files
with
114 additions
and
5 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
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,105 @@ | ||
#![warn(rust_2018_idioms)] | ||
#![cfg(feature = "full")] | ||
|
||
use futures::future; | ||
use parking_lot::{const_mutex, Mutex}; | ||
use std::error::Error; | ||
use std::panic; | ||
use std::sync::Arc; | ||
use tokio::runtime::{Builder, Handle, Runtime}; | ||
|
||
fn test_panic<Func: FnOnce() + panic::UnwindSafe>(func: Func) -> Option<String> { | ||
static PANIC_MUTEX: Mutex<()> = const_mutex(()); | ||
|
||
{ | ||
let _guard = PANIC_MUTEX.lock(); | ||
let panic_file: Arc<Mutex<Option<String>>> = Arc::new(Mutex::new(None)); | ||
|
||
let prev_hook = panic::take_hook(); | ||
{ | ||
let panic_file = panic_file.clone(); | ||
panic::set_hook(Box::new(move |panic_info| { | ||
let panic_location = panic_info.location().unwrap(); | ||
panic_file | ||
.lock() | ||
.clone_from(&Some(panic_location.file().to_string())); | ||
})); | ||
} | ||
|
||
let result = panic::catch_unwind(func); | ||
// Return to the previously set panic hook (maybe default) so that we get nice error | ||
// messages in the tests. | ||
panic::set_hook(prev_hook); | ||
|
||
if result.is_err() { | ||
panic_file.lock().clone() | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn current_handle_panic_caller() -> Result<(), Box<dyn Error>> { | ||
let panic_location_file = test_panic(|| { | ||
let _ = Handle::current(); | ||
}); | ||
|
||
// The panic location should be in this file | ||
assert_eq!(&panic_location_file.unwrap(), file!()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn into_panic_panic_caller() -> Result<(), Box<dyn Error>> { | ||
let panic_location_file = test_panic(move || { | ||
let rt = basic(); | ||
rt.block_on(async { | ||
let handle = tokio::spawn(future::pending::<()>()); | ||
|
||
handle.abort(); | ||
|
||
let err = handle.await.unwrap_err(); | ||
assert!(!&err.is_panic()); | ||
|
||
let _ = err.into_panic(); | ||
}); | ||
}); | ||
|
||
// The panic location should be in this file | ||
assert_eq!(&panic_location_file.unwrap(), file!()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn builder_worker_threads_panic_caller() -> Result<(), Box<dyn Error>> { | ||
let panic_location_file = test_panic(|| { | ||
let _ = Builder::new_multi_thread().worker_threads(0).build(); | ||
}); | ||
|
||
// The panic location should be in this file | ||
assert_eq!(&panic_location_file.unwrap(), file!()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn builder_max_blocking_threads_panic_caller() -> Result<(), Box<dyn Error>> { | ||
let panic_location_file = test_panic(|| { | ||
let _ = Builder::new_multi_thread().max_blocking_threads(0).build(); | ||
}); | ||
|
||
// The panic location should be in this file | ||
assert_eq!(&panic_location_file.unwrap(), file!()); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn basic() -> Runtime { | ||
tokio::runtime::Builder::new_current_thread() | ||
.enable_all() | ||
.build() | ||
.unwrap() | ||
} |