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.
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 io APIs in the main tokio crate where the documentation describes how the function may panic due to incorrect context or inputs. Additionally, the documentation for `AsyncFd` was updated to indicate that the functions `new` and `with_intent` can panic. Tests are included to cover each potentially panicking function. The logic to test the location of a panic (which is a little complex), has been moved to a test support module. Refs: tokio-rs#4413
- Loading branch information
Showing
8 changed files
with
228 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
#![warn(rust_2018_idioms)] | ||
#![cfg(feature = "full")] | ||
|
||
use std::os::unix::prelude::{AsRawFd, RawFd}; | ||
use std::task::{Context, Poll}; | ||
use std::{error::Error, pin::Pin}; | ||
use tokio::io::{self, split, unix::AsyncFd, AsyncRead, AsyncWrite, ReadBuf}; | ||
use tokio::runtime::Builder; | ||
|
||
mod support { | ||
pub mod panic; | ||
} | ||
use support::panic::test_panic; | ||
|
||
struct RW; | ||
|
||
impl AsyncRead for RW { | ||
fn poll_read( | ||
self: Pin<&mut Self>, | ||
_cx: &mut Context<'_>, | ||
buf: &mut ReadBuf<'_>, | ||
) -> Poll<io::Result<()>> { | ||
buf.put_slice(&[b'z']); | ||
Poll::Ready(Ok(())) | ||
} | ||
} | ||
|
||
impl AsyncWrite for RW { | ||
fn poll_write( | ||
self: Pin<&mut Self>, | ||
_cx: &mut Context<'_>, | ||
_buf: &[u8], | ||
) -> Poll<Result<usize, io::Error>> { | ||
Poll::Ready(Ok(1)) | ||
} | ||
|
||
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> { | ||
Poll::Ready(Ok(())) | ||
} | ||
|
||
fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> { | ||
Poll::Ready(Ok(())) | ||
} | ||
} | ||
|
||
struct MockFd; | ||
|
||
impl AsRawFd for MockFd { | ||
fn as_raw_fd(&self) -> RawFd { | ||
0 | ||
} | ||
} | ||
|
||
#[test] | ||
fn read_buf_initialize_unfilled_to_panic_caller() -> Result<(), Box<dyn Error>> { | ||
let panic_location_file = test_panic(|| { | ||
let mut buffer = Vec::<u8>::new(); | ||
let mut read_buf = ReadBuf::new(&mut buffer); | ||
|
||
read_buf.initialize_unfilled_to(2); | ||
}); | ||
|
||
// The panic location should be in this file | ||
assert_eq!(&panic_location_file.unwrap(), file!()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn read_buf_advance_panic_caller() -> Result<(), Box<dyn Error>> { | ||
let panic_location_file = test_panic(|| { | ||
let mut buffer = Vec::<u8>::new(); | ||
let mut read_buf = ReadBuf::new(&mut buffer); | ||
|
||
read_buf.advance(2); | ||
}); | ||
|
||
// The panic location should be in this file | ||
assert_eq!(&panic_location_file.unwrap(), file!()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn read_buf_set_filled_panic_caller() -> Result<(), Box<dyn Error>> { | ||
let panic_location_file = test_panic(|| { | ||
let mut buffer = Vec::<u8>::new(); | ||
let mut read_buf = ReadBuf::new(&mut buffer); | ||
|
||
read_buf.set_filled(2); | ||
}); | ||
|
||
// The panic location should be in this file | ||
assert_eq!(&panic_location_file.unwrap(), file!()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn read_buf_put_slice_panic_caller() -> Result<(), Box<dyn Error>> { | ||
let panic_location_file = test_panic(|| { | ||
let mut buffer = Vec::<u8>::new(); | ||
let mut read_buf = ReadBuf::new(&mut buffer); | ||
|
||
let new_slice = [0x40_u8, 0x41_u8]; | ||
|
||
read_buf.put_slice(&new_slice); | ||
}); | ||
|
||
// The panic location should be in this file | ||
assert_eq!(&panic_location_file.unwrap(), file!()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn unsplit_panic_caller() -> Result<(), Box<dyn Error>> { | ||
let panic_location_file = test_panic(|| { | ||
let (r1, _w1) = split(RW); | ||
let (_r2, w2) = split(RW); | ||
r1.unsplit(w2); | ||
}); | ||
|
||
// The panic location should be in this file | ||
assert_eq!(&panic_location_file.unwrap(), file!()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
#[cfg(unix)] | ||
fn async_fd_new_panic_caller() -> Result<(), Box<dyn Error>> { | ||
let panic_location_file = test_panic(|| { | ||
// Runtime without `enable_io` so it has no current timer set. | ||
let rt = Builder::new_current_thread().build().unwrap(); | ||
rt.block_on(async { | ||
let fd = MockFd; | ||
|
||
let _ = AsyncFd::new(fd); | ||
}); | ||
}); | ||
|
||
// The panic location should be in this file | ||
assert_eq!(&panic_location_file.unwrap(), file!()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
#[cfg(unix)] | ||
fn async_fd_with_interest_panic_caller() -> Result<(), Box<dyn Error>> { | ||
use tokio::io::Interest; | ||
|
||
let panic_location_file = test_panic(|| { | ||
// Runtime without `enable_io` so it has no current timer set. | ||
let rt = Builder::new_current_thread().build().unwrap(); | ||
rt.block_on(async { | ||
let fd = MockFd; | ||
|
||
let _ = AsyncFd::with_interest(fd, Interest::READABLE); | ||
}); | ||
}); | ||
|
||
// The panic location should be in this file | ||
assert_eq!(&panic_location_file.unwrap(), file!()); | ||
|
||
Ok(()) | ||
} |
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,34 @@ | ||
use parking_lot::{const_mutex, Mutex}; | ||
use std::panic; | ||
use std::sync::Arc; | ||
|
||
pub 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 | ||
} | ||
} | ||
} |
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