Skip to content
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

signal: add track_caller to public APIs #4806

Merged
merged 1 commit into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tokio/src/signal/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,12 @@ pub struct Signal {
/// * If the previous initialization of this specific signal failed.
/// * If the signal is one of
/// [`signal_hook::FORBIDDEN`](fn@signal_hook_registry::register#panics)
///
/// # Panics
///
/// This function panics if there is no current reactor set, or if the `rt`
/// feature flag is not enabled.
#[track_caller]
pub fn signal(kind: SignalKind) -> io::Result<Signal> {
let rx = signal_with_handle(kind, &Handle::current())?;

Expand Down
2 changes: 2 additions & 0 deletions tokio/src/signal/unix/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ cfg_rt! {
/// # Panics
///
/// This function panics if there is no current signal driver set.
#[track_caller]
pub(super) fn current() -> Self {
crate::runtime::context::signal_handle().expect(
"there is no signal driver running, must be called from the context of Tokio runtime",
Expand All @@ -197,6 +198,7 @@ cfg_not_rt! {
/// # Panics
///
/// This function panics if there is no current signal driver set.
#[track_caller]
pub(super) fn current() -> Self {
panic!(
"there is no signal driver running, must be called from the context of Tokio runtime or with\
Expand Down
29 changes: 29 additions & 0 deletions tokio/tests/signal_panic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#![warn(rust_2018_idioms)]
#![cfg(feature = "full")]
#![cfg(unix)]

use std::error::Error;
use tokio::runtime::Builder;
use tokio::signal::unix::{signal, SignalKind};

mod support {
pub mod panic;
}
use support::panic::test_panic;

#[test]
fn signal_panic_caller() -> Result<(), Box<dyn Error>> {
let panic_location_file = test_panic(|| {
let rt = Builder::new_current_thread().build().unwrap();

rt.block_on(async {
let kind = SignalKind::from_raw(-1);
let _ = signal(kind);
});
});

// The panic location should be in this file
assert_eq!(&panic_location_file.unwrap(), file!());

Ok(())
}