Skip to content

Commit cbdba8b

Browse files
authored
net: add track_caller to public APIs (#4805)
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 net APIs in the main tokio crate where the documentation describes how the function may panic due to incorrect context or inputs. Not all panic cases can have #[track_caller] applied fully as the callstack passes through a closure which isn't yet supported by the annotation (e.g. net functions called from outside a tokio runtime). Additionally, the documentation was updated to indicate additional cases in which the public net functions may panic (the same as the io functions). Tests are included to cover each potentially panicking function. Refs: #4413
1 parent 6f048ca commit cbdba8b

File tree

10 files changed

+212
-6
lines changed

10 files changed

+212
-6
lines changed

tokio/src/io/poll_evented.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ impl<E: Source> PollEvented<E> {
7777
/// The runtime is usually set implicitly when this function is called
7878
/// from a future driven by a tokio runtime, otherwise runtime can be set
7979
/// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function.
80+
#[track_caller]
8081
#[cfg_attr(feature = "signal", allow(unused))]
8182
pub(crate) fn new(io: E) -> io::Result<Self> {
8283
PollEvented::new_with_interest(io, Interest::READABLE | Interest::WRITABLE)
@@ -97,6 +98,7 @@ impl<E: Source> PollEvented<E> {
9798
/// a future driven by a tokio runtime, otherwise runtime can be set
9899
/// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter)
99100
/// function.
101+
#[track_caller]
100102
#[cfg_attr(feature = "signal", allow(unused))]
101103
pub(crate) fn new_with_interest(io: E, interest: Interest) -> io::Result<Self> {
102104
Self::new_with_interest_and_handle(io, interest, Handle::current())

tokio/src/net/tcp/listener.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,13 @@ impl TcpListener {
221221
///
222222
/// # Panics
223223
///
224-
/// This function panics if thread-local runtime is not set.
224+
/// This function panics if it is not called from within a runtime with
225+
/// IO enabled.
225226
///
226227
/// The runtime is usually set implicitly when this function is called
227228
/// from a future driven by a tokio runtime, otherwise runtime can be set
228229
/// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function.
230+
#[track_caller]
229231
pub fn from_std(listener: net::TcpListener) -> io::Result<TcpListener> {
230232
let io = mio::net::TcpListener::from_std(listener);
231233
let io = PollEvented::new(io)?;

tokio/src/net/tcp/stream.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,13 @@ impl TcpStream {
186186
///
187187
/// # Panics
188188
///
189-
/// This function panics if thread-local runtime is not set.
189+
/// This function panics if it is not called from within a runtime with
190+
/// IO enabled.
190191
///
191192
/// The runtime is usually set implicitly when this function is called
192193
/// from a future driven by a tokio runtime, otherwise runtime can be set
193194
/// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function.
195+
#[track_caller]
194196
pub fn from_std(stream: std::net::TcpStream) -> io::Result<TcpStream> {
195197
let io = mio::net::TcpStream::from_std(stream);
196198
let io = PollEvented::new(io)?;

tokio/src/net/udp.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ impl UdpSocket {
170170
UdpSocket::new(sys)
171171
}
172172

173+
#[track_caller]
173174
fn new(socket: mio::net::UdpSocket) -> io::Result<UdpSocket> {
174175
let io = PollEvented::new(socket)?;
175176
Ok(UdpSocket { io })
@@ -210,6 +211,7 @@ impl UdpSocket {
210211
/// # Ok(())
211212
/// # }
212213
/// ```
214+
#[track_caller]
213215
pub fn from_std(socket: net::UdpSocket) -> io::Result<UdpSocket> {
214216
let io = mio::net::UdpSocket::from_std(socket);
215217
UdpSocket::new(io)

tokio/src/net/unix/datagram/socket.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,8 @@ impl UnixDatagram {
430430
///
431431
/// # Panics
432432
///
433-
/// This function panics if thread-local runtime is not set.
433+
/// This function panics if it is not called from within a runtime with
434+
/// IO enabled.
434435
///
435436
/// The runtime is usually set implicitly when this function is called
436437
/// from a future driven by a Tokio runtime, otherwise runtime can be set
@@ -457,6 +458,7 @@ impl UnixDatagram {
457458
/// # Ok(())
458459
/// # }
459460
/// ```
461+
#[track_caller]
460462
pub fn from_std(datagram: net::UnixDatagram) -> io::Result<UnixDatagram> {
461463
let socket = mio::net::UnixDatagram::from_std(datagram);
462464
let io = PollEvented::new(socket)?;

tokio/src/net/unix/listener.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ impl UnixListener {
5454
///
5555
/// # Panics
5656
///
57-
/// This function panics if thread-local runtime is not set.
57+
/// This function panics if it is not called from within a runtime with
58+
/// IO enabled.
5859
///
5960
/// The runtime is usually set implicitly when this function is called
6061
/// from a future driven by a tokio runtime, otherwise runtime can be set
6162
/// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function.
63+
#[track_caller]
6264
pub fn bind<P>(path: P) -> io::Result<UnixListener>
6365
where
6466
P: AsRef<Path>,
@@ -77,11 +79,13 @@ impl UnixListener {
7779
///
7880
/// # Panics
7981
///
80-
/// This function panics if thread-local runtime is not set.
82+
/// This function panics if it is not called from within a runtime with
83+
/// IO enabled.
8184
///
8285
/// The runtime is usually set implicitly when this function is called
8386
/// from a future driven by a tokio runtime, otherwise runtime can be set
8487
/// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function.
88+
#[track_caller]
8589
pub fn from_std(listener: net::UnixListener) -> io::Result<UnixListener> {
8690
let listener = mio::net::UnixListener::from_std(listener);
8791
let io = PollEvented::new(listener)?;

tokio/src/net/unix/stream.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,11 +699,13 @@ impl UnixStream {
699699
///
700700
/// # Panics
701701
///
702-
/// This function panics if thread-local runtime is not set.
702+
/// This function panics if it is not called from within a runtime with
703+
/// IO enabled.
703704
///
704705
/// The runtime is usually set implicitly when this function is called
705706
/// from a future driven by a tokio runtime, otherwise runtime can be set
706707
/// explicitly with [`Runtime::enter`](crate::runtime::Runtime::enter) function.
708+
#[track_caller]
707709
pub fn from_std(stream: net::UnixStream) -> io::Result<UnixStream> {
708710
let stream = mio::net::UnixStream::from_std(stream);
709711
let io = PollEvented::new(stream)?;

tokio/src/net/windows/named_pipe.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,6 +2020,7 @@ impl ServerOptions {
20202020
/// let builder = ServerOptions::new().max_instances(255);
20212021
/// # Ok(()) }
20222022
/// ```
2023+
#[track_caller]
20232024
pub fn max_instances(&mut self, instances: usize) -> &mut Self {
20242025
assert!(instances < 255, "cannot specify more than 254 instances");
20252026
self.max_instances = instances as DWORD;

tokio/src/runtime/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub(crate) fn current() -> Handle {
2424
}
2525

2626
cfg_io_driver! {
27+
#[track_caller]
2728
pub(crate) fn io_handle() -> crate::runtime::driver::IoHandle {
2829
match CONTEXT.try_with(|ctx| {
2930
let ctx = ctx.borrow();

tokio/tests/net_panic.rs

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
#![warn(rust_2018_idioms)]
2+
#![cfg(all(feature = "full", not(target_os = "wasi")))]
3+
4+
use std::error::Error;
5+
use tokio::net::{TcpListener, TcpStream};
6+
use tokio::runtime::{Builder, Runtime};
7+
8+
mod support {
9+
pub mod panic;
10+
}
11+
use support::panic::test_panic;
12+
13+
#[test]
14+
fn udp_socket_from_std_panic_caller() -> Result<(), Box<dyn Error>> {
15+
use std::net::SocketAddr;
16+
use tokio::net::UdpSocket;
17+
18+
let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
19+
let std_sock = std::net::UdpSocket::bind(addr).unwrap();
20+
std_sock.set_nonblocking(true).unwrap();
21+
22+
let panic_location_file = test_panic(|| {
23+
let rt = runtime_without_io();
24+
rt.block_on(async {
25+
let _sock = UdpSocket::from_std(std_sock);
26+
});
27+
});
28+
29+
// The panic location should be in this file
30+
assert_eq!(&panic_location_file.unwrap(), file!());
31+
32+
Ok(())
33+
}
34+
35+
#[test]
36+
fn tcp_listener_from_std_panic_caller() -> Result<(), Box<dyn Error>> {
37+
let std_listener = std::net::TcpListener::bind("127.0.0.1:8080").unwrap();
38+
std_listener.set_nonblocking(true).unwrap();
39+
40+
let panic_location_file = test_panic(|| {
41+
let rt = runtime_without_io();
42+
rt.block_on(async {
43+
let _ = TcpListener::from_std(std_listener);
44+
});
45+
});
46+
47+
// The panic location should be in this file
48+
assert_eq!(&panic_location_file.unwrap(), file!());
49+
50+
Ok(())
51+
}
52+
53+
#[test]
54+
fn tcp_stream_from_std_panic_caller() -> Result<(), Box<dyn Error>> {
55+
let std_listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
56+
57+
let std_stream = std::net::TcpStream::connect(std_listener.local_addr().unwrap()).unwrap();
58+
std_stream.set_nonblocking(true).unwrap();
59+
60+
let panic_location_file = test_panic(|| {
61+
let rt = runtime_without_io();
62+
rt.block_on(async {
63+
let _ = TcpStream::from_std(std_stream);
64+
});
65+
});
66+
67+
// The panic location should be in this file
68+
assert_eq!(&panic_location_file.unwrap(), file!());
69+
70+
Ok(())
71+
}
72+
73+
#[test]
74+
#[cfg(unix)]
75+
fn unix_listener_bind_panic_caller() -> Result<(), Box<dyn Error>> {
76+
use tokio::net::UnixListener;
77+
78+
let dir = tempfile::tempdir().unwrap();
79+
let sock_path = dir.path().join("socket");
80+
81+
let panic_location_file = test_panic(|| {
82+
let rt = runtime_without_io();
83+
rt.block_on(async {
84+
let _ = UnixListener::bind(&sock_path);
85+
});
86+
});
87+
88+
// The panic location should be in this file
89+
assert_eq!(&panic_location_file.unwrap(), file!());
90+
91+
Ok(())
92+
}
93+
94+
#[test]
95+
#[cfg(unix)]
96+
fn unix_listener_from_std_panic_caller() -> Result<(), Box<dyn Error>> {
97+
use tokio::net::UnixListener;
98+
99+
let dir = tempfile::tempdir().unwrap();
100+
let sock_path = dir.path().join("socket");
101+
let std_listener = std::os::unix::net::UnixListener::bind(&sock_path).unwrap();
102+
103+
let panic_location_file = test_panic(|| {
104+
let rt = runtime_without_io();
105+
rt.block_on(async {
106+
let _ = UnixListener::from_std(std_listener);
107+
});
108+
});
109+
110+
// The panic location should be in this file
111+
assert_eq!(&panic_location_file.unwrap(), file!());
112+
113+
Ok(())
114+
}
115+
116+
#[test]
117+
#[cfg(unix)]
118+
fn unix_stream_from_std_panic_caller() -> Result<(), Box<dyn Error>> {
119+
use tokio::net::UnixStream;
120+
121+
let dir = tempfile::tempdir().unwrap();
122+
let sock_path = dir.path().join("socket");
123+
let _std_listener = std::os::unix::net::UnixListener::bind(&sock_path).unwrap();
124+
let std_stream = std::os::unix::net::UnixStream::connect(&sock_path).unwrap();
125+
126+
let panic_location_file = test_panic(|| {
127+
let rt = runtime_without_io();
128+
rt.block_on(async {
129+
let _ = UnixStream::from_std(std_stream);
130+
});
131+
});
132+
133+
// The panic location should be in this file
134+
assert_eq!(&panic_location_file.unwrap(), file!());
135+
136+
Ok(())
137+
}
138+
139+
#[test]
140+
#[cfg(unix)]
141+
fn unix_datagram_from_std_panic_caller() -> Result<(), Box<dyn Error>> {
142+
use std::os::unix::net::UnixDatagram as StdUDS;
143+
use tokio::net::UnixDatagram;
144+
145+
let dir = tempfile::tempdir().unwrap();
146+
let sock_path = dir.path().join("socket");
147+
148+
// Bind the socket to a filesystem path
149+
// /let socket_path = tmp.path().join("socket");
150+
let std_socket = StdUDS::bind(&sock_path).unwrap();
151+
std_socket.set_nonblocking(true).unwrap();
152+
153+
let panic_location_file = test_panic(move || {
154+
let rt = runtime_without_io();
155+
rt.block_on(async {
156+
let _ = UnixDatagram::from_std(std_socket);
157+
});
158+
});
159+
160+
// The panic location should be in this file
161+
assert_eq!(&panic_location_file.unwrap(), file!());
162+
163+
Ok(())
164+
}
165+
166+
#[test]
167+
#[cfg(windows)]
168+
fn server_options_max_instances_panic_caller() -> Result<(), Box<dyn Error>> {
169+
use tokio::net::windows::named_pipe::ServerOptions;
170+
171+
let panic_location_file = test_panic(move || {
172+
let rt = runtime_without_io();
173+
rt.block_on(async {
174+
let mut options = ServerOptions::new();
175+
options.max_instances(255);
176+
});
177+
});
178+
179+
// The panic location should be in this file
180+
assert_eq!(&panic_location_file.unwrap(), file!());
181+
182+
Ok(())
183+
}
184+
185+
// Runtime without `enable_io` so it has no IO driver set.
186+
fn runtime_without_io() -> Runtime {
187+
Builder::new_current_thread().build().unwrap()
188+
}

0 commit comments

Comments
 (0)