-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add track_caller to public APIs (#4413) #4772
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8e1cf59
add track_caller to public APIs (#4413)
hds cddbcb9
fix race condition and remove lazy_static
hds 0196c31
add await to future::pending
hds aaf73a8
use future::pending directly in tokio::spawn
hds f406d0e
Remove "test_" prefix from test functions
hds e1fe84a
Merge remote-tracking branch 'origin/master' into track-caller
hds File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, does this function have two sections describing its panics?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Funny you should ask... the first Panic section (as far as I can tell) isn't actually true. Calling
worker_threads
for acurrent_thread
runtime doesn't panic, at least not immediately and not after scheduling a task on the runtime either.I was going to submit an issue for this to get some guidance on the preferred behaviour (fix docs or fix code).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created issue for it not panicking in the way described by the first Panic section: #4773