/rust/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.47.1/src/runtime/handle.rs
Line | Count | Source (jump to first uncovered line) |
1 | | #[cfg(tokio_unstable)] |
2 | | use crate::runtime; |
3 | | use crate::runtime::{context, scheduler, RuntimeFlavor, RuntimeMetrics}; |
4 | | |
5 | | /// Handle to the runtime. |
6 | | /// |
7 | | /// The handle is internally reference-counted and can be freely cloned. A handle can be |
8 | | /// obtained using the [`Runtime::handle`] method. |
9 | | /// |
10 | | /// [`Runtime::handle`]: crate::runtime::Runtime::handle() |
11 | | #[derive(Debug, Clone)] |
12 | | // When the `rt` feature is *not* enabled, this type is still defined, but not |
13 | | // included in the public API. |
14 | | pub struct Handle { |
15 | | pub(crate) inner: scheduler::Handle, |
16 | | } |
17 | | |
18 | | use crate::runtime::task::JoinHandle; |
19 | | use crate::runtime::BOX_FUTURE_THRESHOLD; |
20 | | use crate::util::error::{CONTEXT_MISSING_ERROR, THREAD_LOCAL_DESTROYED_ERROR}; |
21 | | use crate::util::trace::SpawnMeta; |
22 | | |
23 | | use std::future::Future; |
24 | | use std::marker::PhantomData; |
25 | | use std::{error, fmt, mem}; |
26 | | |
27 | | /// Runtime context guard. |
28 | | /// |
29 | | /// Returned by [`Runtime::enter`] and [`Handle::enter`], the context guard exits |
30 | | /// the runtime context on drop. |
31 | | /// |
32 | | /// [`Runtime::enter`]: fn@crate::runtime::Runtime::enter |
33 | | #[derive(Debug)] |
34 | | #[must_use = "Creating and dropping a guard does nothing"] |
35 | | pub struct EnterGuard<'a> { |
36 | | _guard: context::SetCurrentGuard, |
37 | | _handle_lifetime: PhantomData<&'a Handle>, |
38 | | } |
39 | | |
40 | | impl Handle { |
41 | | /// Enters the runtime context. This allows you to construct types that must |
42 | | /// have an executor available on creation such as [`Sleep`] or |
43 | | /// [`TcpStream`]. It will also allow you to call methods such as |
44 | | /// [`tokio::spawn`] and [`Handle::current`] without panicking. |
45 | | /// |
46 | | /// # Panics |
47 | | /// |
48 | | /// When calling `Handle::enter` multiple times, the returned guards |
49 | | /// **must** be dropped in the reverse order that they were acquired. |
50 | | /// Failure to do so will result in a panic and possible memory leaks. |
51 | | /// |
52 | | /// # Examples |
53 | | /// |
54 | | /// ``` |
55 | | /// use tokio::runtime::Runtime; |
56 | | /// |
57 | | /// let rt = Runtime::new().unwrap(); |
58 | | /// |
59 | | /// let _guard = rt.enter(); |
60 | | /// tokio::spawn(async { |
61 | | /// println!("Hello world!"); |
62 | | /// }); |
63 | | /// ``` |
64 | | /// |
65 | | /// Do **not** do the following, this shows a scenario that will result in a |
66 | | /// panic and possible memory leak. |
67 | | /// |
68 | | /// ```should_panic |
69 | | /// use tokio::runtime::Runtime; |
70 | | /// |
71 | | /// let rt1 = Runtime::new().unwrap(); |
72 | | /// let rt2 = Runtime::new().unwrap(); |
73 | | /// |
74 | | /// let enter1 = rt1.enter(); |
75 | | /// let enter2 = rt2.enter(); |
76 | | /// |
77 | | /// drop(enter1); |
78 | | /// drop(enter2); |
79 | | /// ``` |
80 | | /// |
81 | | /// [`Sleep`]: struct@crate::time::Sleep |
82 | | /// [`TcpStream`]: struct@crate::net::TcpStream |
83 | | /// [`tokio::spawn`]: fn@crate::spawn |
84 | 580k | pub fn enter(&self) -> EnterGuard<'_> { |
85 | 580k | EnterGuard { |
86 | 580k | _guard: match context::try_set_current(&self.inner) { |
87 | 580k | Some(guard) => guard, |
88 | 0 | None => panic!("{}", crate::util::error::THREAD_LOCAL_DESTROYED_ERROR), |
89 | | }, |
90 | 580k | _handle_lifetime: PhantomData, |
91 | 580k | } |
92 | 580k | } |
93 | | |
94 | | /// Returns a `Handle` view over the currently running `Runtime`. |
95 | | /// |
96 | | /// # Panics |
97 | | /// |
98 | | /// This will panic if called outside the context of a Tokio runtime. That means that you must |
99 | | /// call this on one of the threads **being run by the runtime**, or from a thread with an active |
100 | | /// `EnterGuard`. Calling this from within a thread created by `std::thread::spawn` (for example) |
101 | | /// will cause a panic unless that thread has an active `EnterGuard`. |
102 | | /// |
103 | | /// # Examples |
104 | | /// |
105 | | /// This can be used to obtain the handle of the surrounding runtime from an async |
106 | | /// block or function running on that runtime. |
107 | | /// |
108 | | /// ``` |
109 | | /// # use std::thread; |
110 | | /// # use tokio::runtime::Runtime; |
111 | | /// # fn dox() { |
112 | | /// # let rt = Runtime::new().unwrap(); |
113 | | /// # rt.spawn(async { |
114 | | /// use tokio::runtime::Handle; |
115 | | /// |
116 | | /// // Inside an async block or function. |
117 | | /// let handle = Handle::current(); |
118 | | /// handle.spawn(async { |
119 | | /// println!("now running in the existing Runtime"); |
120 | | /// }); |
121 | | /// |
122 | | /// # let handle = |
123 | | /// thread::spawn(move || { |
124 | | /// // Notice that the handle is created outside of this thread and then moved in |
125 | | /// handle.spawn(async { /* ... */ }); |
126 | | /// // This next line would cause a panic because we haven't entered the runtime |
127 | | /// // and created an EnterGuard |
128 | | /// // let handle2 = Handle::current(); // panic |
129 | | /// // So we create a guard here with Handle::enter(); |
130 | | /// let _guard = handle.enter(); |
131 | | /// // Now we can call Handle::current(); |
132 | | /// let handle2 = Handle::current(); |
133 | | /// }); |
134 | | /// # handle.join().unwrap(); |
135 | | /// # }); |
136 | | /// # } |
137 | | /// ``` |
138 | | #[track_caller] |
139 | 546k | pub fn current() -> Self { |
140 | 546k | Handle { |
141 | 546k | inner: scheduler::Handle::current(), |
142 | 546k | } |
143 | 546k | } |
144 | | |
145 | | /// Returns a Handle view over the currently running Runtime |
146 | | /// |
147 | | /// Returns an error if no Runtime has been started |
148 | | /// |
149 | | /// Contrary to `current`, this never panics |
150 | 0 | pub fn try_current() -> Result<Self, TryCurrentError> { |
151 | 0 | context::with_current(|inner| Handle { |
152 | 0 | inner: inner.clone(), |
153 | 0 | }) |
154 | 0 | } |
155 | | |
156 | | /// Spawns a future onto the Tokio runtime. |
157 | | /// |
158 | | /// This spawns the given future onto the runtime's executor, usually a |
159 | | /// thread pool. The thread pool is then responsible for polling the future |
160 | | /// until it completes. |
161 | | /// |
162 | | /// The provided future will start running in the background immediately |
163 | | /// when `spawn` is called, even if you don't await the returned |
164 | | /// `JoinHandle`. |
165 | | /// |
166 | | /// See [module level][mod] documentation for more details. |
167 | | /// |
168 | | /// [mod]: index.html |
169 | | /// |
170 | | /// # Examples |
171 | | /// |
172 | | /// ``` |
173 | | /// use tokio::runtime::Runtime; |
174 | | /// |
175 | | /// # fn dox() { |
176 | | /// // Create the runtime |
177 | | /// let rt = Runtime::new().unwrap(); |
178 | | /// // Get a handle from this runtime |
179 | | /// let handle = rt.handle(); |
180 | | /// |
181 | | /// // Spawn a future onto the runtime using the handle |
182 | | /// handle.spawn(async { |
183 | | /// println!("now running on a worker thread"); |
184 | | /// }); |
185 | | /// # } |
186 | | /// ``` |
187 | | #[track_caller] |
188 | 0 | pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output> |
189 | 0 | where |
190 | 0 | F: Future + Send + 'static, |
191 | 0 | F::Output: Send + 'static, |
192 | 0 | { |
193 | 0 | let fut_size = mem::size_of::<F>(); |
194 | 0 | if fut_size > BOX_FUTURE_THRESHOLD { |
195 | 0 | self.spawn_named(Box::pin(future), SpawnMeta::new_unnamed(fut_size)) |
196 | | } else { |
197 | 0 | self.spawn_named(future, SpawnMeta::new_unnamed(fut_size)) |
198 | | } |
199 | 0 | } |
200 | | |
201 | | /// Runs the provided function on an executor dedicated to blocking |
202 | | /// operations. |
203 | | /// |
204 | | /// # Examples |
205 | | /// |
206 | | /// ``` |
207 | | /// use tokio::runtime::Runtime; |
208 | | /// |
209 | | /// # fn dox() { |
210 | | /// // Create the runtime |
211 | | /// let rt = Runtime::new().unwrap(); |
212 | | /// // Get a handle from this runtime |
213 | | /// let handle = rt.handle(); |
214 | | /// |
215 | | /// // Spawn a blocking function onto the runtime using the handle |
216 | | /// handle.spawn_blocking(|| { |
217 | | /// println!("now running on a worker thread"); |
218 | | /// }); |
219 | | /// # } |
220 | | #[track_caller] |
221 | 546k | pub fn spawn_blocking<F, R>(&self, func: F) -> JoinHandle<R> |
222 | 546k | where |
223 | 546k | F: FnOnce() -> R + Send + 'static, |
224 | 546k | R: Send + 'static, |
225 | 546k | { |
226 | 546k | self.inner.blocking_spawner().spawn_blocking(self, func) |
227 | 546k | } Unexecuted instantiation: <tokio::runtime::handle::Handle>::spawn_blocking::<<tokio::fs::read_dir::ReadDir>::poll_next_entry::{closure#0}, (alloc::collections::vec_deque::VecDeque<core::result::Result<tokio::fs::read_dir::DirEntry, std::io::error::Error>>, std::fs::ReadDir, bool)> <tokio::runtime::handle::Handle>::spawn_blocking::<<tokio::runtime::scheduler::multi_thread::worker::Launch>::launch::{closure#0}, ()> Line | Count | Source | 221 | 546k | pub fn spawn_blocking<F, R>(&self, func: F) -> JoinHandle<R> | 222 | 546k | where | 223 | 546k | F: FnOnce() -> R + Send + 'static, | 224 | 546k | R: Send + 'static, | 225 | 546k | { | 226 | 546k | self.inner.blocking_spawner().spawn_blocking(self, func) | 227 | 546k | } |
Unexecuted instantiation: <tokio::runtime::handle::Handle>::spawn_blocking::<<tokio::io::blocking::Blocking<std::io::stdio::Stdin> as tokio::io::async_read::AsyncRead>::poll_read::{closure#0}, (core::result::Result<usize, std::io::error::Error>, tokio::io::blocking::Buf, std::io::stdio::Stdin)> Unexecuted instantiation: <tokio::runtime::handle::Handle>::spawn_blocking::<<tokio::fs::file::File as tokio::io::async_seek::AsyncSeek>::start_seek::{closure#0}, (tokio::fs::file::Operation, tokio::io::blocking::Buf)> Unexecuted instantiation: <tokio::runtime::handle::Handle>::spawn_blocking::<<tokio::fs::file::File as tokio::io::async_read::AsyncRead>::poll_read::{closure#0}, (tokio::fs::file::Operation, tokio::io::blocking::Buf)> Unexecuted instantiation: <tokio::runtime::handle::Handle>::spawn_blocking::<<tokio::io::blocking::Blocking<std::io::stdio::Stderr> as tokio::io::async_write::AsyncWrite>::poll_flush::{closure#0}, (core::result::Result<usize, std::io::error::Error>, tokio::io::blocking::Buf, std::io::stdio::Stderr)> Unexecuted instantiation: <tokio::runtime::handle::Handle>::spawn_blocking::<<tokio::io::blocking::Blocking<std::io::stdio::Stderr> as tokio::io::async_write::AsyncWrite>::poll_write::{closure#0}, (core::result::Result<usize, std::io::error::Error>, tokio::io::blocking::Buf, std::io::stdio::Stderr)> Unexecuted instantiation: <tokio::runtime::handle::Handle>::spawn_blocking::<<tokio::io::blocking::Blocking<std::io::stdio::Stdout> as tokio::io::async_write::AsyncWrite>::poll_flush::{closure#0}, (core::result::Result<usize, std::io::error::Error>, tokio::io::blocking::Buf, std::io::stdio::Stdout)> Unexecuted instantiation: <tokio::runtime::handle::Handle>::spawn_blocking::<<tokio::io::blocking::Blocking<std::io::stdio::Stdout> as tokio::io::async_write::AsyncWrite>::poll_write::{closure#0}, (core::result::Result<usize, std::io::error::Error>, tokio::io::blocking::Buf, std::io::stdio::Stdout)> Unexecuted instantiation: <tokio::runtime::handle::Handle>::spawn_blocking::<<str as tokio::net::addr::sealed::ToSocketAddrsPriv>::to_socket_addrs::{closure#0}, core::result::Result<alloc::vec::into_iter::IntoIter<core::net::socket_addr::SocketAddr>, std::io::error::Error>> Unexecuted instantiation: <tokio::runtime::handle::Handle>::spawn_blocking::<<(&str, u16) as tokio::net::addr::sealed::ToSocketAddrsPriv>::to_socket_addrs::{closure#0}, core::result::Result<alloc::vec::into_iter::IntoIter<core::net::socket_addr::SocketAddr>, std::io::error::Error>> |
228 | | |
229 | | /// Runs a future to completion on this `Handle`'s associated `Runtime`. |
230 | | /// |
231 | | /// This runs the given future on the current thread, blocking until it is |
232 | | /// complete, and yielding its resolved result. Any tasks or timers which |
233 | | /// the future spawns internally will be executed on the runtime. |
234 | | /// |
235 | | /// When this is used on a `current_thread` runtime, only the |
236 | | /// [`Runtime::block_on`] method can drive the IO and timer drivers, but the |
237 | | /// `Handle::block_on` method cannot drive them. This means that, when using |
238 | | /// this method on a `current_thread` runtime, anything that relies on IO or |
239 | | /// timers will not work unless there is another thread currently calling |
240 | | /// [`Runtime::block_on`] on the same runtime. |
241 | | /// |
242 | | /// # If the runtime has been shut down |
243 | | /// |
244 | | /// If the `Handle`'s associated `Runtime` has been shut down (through |
245 | | /// [`Runtime::shutdown_background`], [`Runtime::shutdown_timeout`], or by |
246 | | /// dropping it) and `Handle::block_on` is used it might return an error or |
247 | | /// panic. Specifically IO resources will return an error and timers will |
248 | | /// panic. Runtime independent futures will run as normal. |
249 | | /// |
250 | | /// # Panics |
251 | | /// |
252 | | /// This function panics if the provided future panics, if called within an |
253 | | /// asynchronous execution context, or if a timer future is executed on a runtime that has been |
254 | | /// shut down. |
255 | | /// |
256 | | /// # Examples |
257 | | /// |
258 | | /// ``` |
259 | | /// use tokio::runtime::Runtime; |
260 | | /// |
261 | | /// // Create the runtime |
262 | | /// let rt = Runtime::new().unwrap(); |
263 | | /// |
264 | | /// // Get a handle from this runtime |
265 | | /// let handle = rt.handle(); |
266 | | /// |
267 | | /// // Execute the future, blocking the current thread until completion |
268 | | /// handle.block_on(async { |
269 | | /// println!("hello"); |
270 | | /// }); |
271 | | /// ``` |
272 | | /// |
273 | | /// Or using `Handle::current`: |
274 | | /// |
275 | | /// ``` |
276 | | /// use tokio::runtime::Handle; |
277 | | /// |
278 | | /// #[tokio::main] |
279 | | /// async fn main () { |
280 | | /// let handle = Handle::current(); |
281 | | /// std::thread::spawn(move || { |
282 | | /// // Using Handle::block_on to run async code in the new thread. |
283 | | /// handle.block_on(async { |
284 | | /// println!("hello"); |
285 | | /// }); |
286 | | /// }); |
287 | | /// } |
288 | | /// ``` |
289 | | /// |
290 | | /// [`JoinError`]: struct@crate::task::JoinError |
291 | | /// [`JoinHandle`]: struct@crate::task::JoinHandle |
292 | | /// [`Runtime::block_on`]: fn@crate::runtime::Runtime::block_on |
293 | | /// [`Runtime::shutdown_background`]: fn@crate::runtime::Runtime::shutdown_background |
294 | | /// [`Runtime::shutdown_timeout`]: fn@crate::runtime::Runtime::shutdown_timeout |
295 | | /// [`spawn_blocking`]: crate::task::spawn_blocking |
296 | | /// [`tokio::fs`]: crate::fs |
297 | | /// [`tokio::net`]: crate::net |
298 | | /// [`tokio::time`]: crate::time |
299 | | #[track_caller] |
300 | 0 | pub fn block_on<F: Future>(&self, future: F) -> F::Output { |
301 | 0 | let fut_size = mem::size_of::<F>(); |
302 | 0 | if fut_size > BOX_FUTURE_THRESHOLD { |
303 | 0 | self.block_on_inner(Box::pin(future), SpawnMeta::new_unnamed(fut_size)) |
304 | | } else { |
305 | 0 | self.block_on_inner(future, SpawnMeta::new_unnamed(fut_size)) |
306 | | } |
307 | 0 | } |
308 | | |
309 | | #[track_caller] |
310 | 0 | fn block_on_inner<F: Future>(&self, future: F, _meta: SpawnMeta<'_>) -> F::Output { |
311 | 0 | #[cfg(all( |
312 | 0 | tokio_unstable, |
313 | 0 | tokio_taskdump, |
314 | 0 | feature = "rt", |
315 | 0 | target_os = "linux", |
316 | 0 | any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64") |
317 | 0 | ))] |
318 | 0 | let future = super::task::trace::Trace::root(future); |
319 | 0 |
|
320 | 0 | #[cfg(all(tokio_unstable, feature = "tracing"))] |
321 | 0 | let future = |
322 | 0 | crate::util::trace::task(future, "block_on", _meta, super::task::Id::next().as_u64()); |
323 | 0 |
|
324 | 0 | // Enter the runtime context. This sets the current driver handles and |
325 | 0 | // prevents blocking an existing runtime. |
326 | 0 | context::enter_runtime(&self.inner, true, |blocking| { |
327 | 0 | blocking.block_on(future).expect("failed to park thread") |
328 | 0 | }) |
329 | 0 | } |
330 | | |
331 | | #[track_caller] |
332 | 0 | pub(crate) fn spawn_named<F>(&self, future: F, meta: SpawnMeta<'_>) -> JoinHandle<F::Output> |
333 | 0 | where |
334 | 0 | F: Future + Send + 'static, |
335 | 0 | F::Output: Send + 'static, |
336 | 0 | { |
337 | 0 | let id = crate::runtime::task::Id::next(); |
338 | 0 | #[cfg(all( |
339 | 0 | tokio_unstable, |
340 | 0 | tokio_taskdump, |
341 | 0 | feature = "rt", |
342 | 0 | target_os = "linux", |
343 | 0 | any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64") |
344 | 0 | ))] |
345 | 0 | let future = super::task::trace::Trace::root(future); |
346 | 0 | #[cfg(all(tokio_unstable, feature = "tracing"))] |
347 | 0 | let future = crate::util::trace::task(future, "task", meta, id.as_u64()); |
348 | 0 | self.inner.spawn(future, id, meta.spawned_at) |
349 | 0 | } |
350 | | |
351 | | #[track_caller] |
352 | | #[allow(dead_code)] |
353 | 0 | pub(crate) unsafe fn spawn_local_named<F>( |
354 | 0 | &self, |
355 | 0 | future: F, |
356 | 0 | meta: SpawnMeta<'_>, |
357 | 0 | ) -> JoinHandle<F::Output> |
358 | 0 | where |
359 | 0 | F: Future + 'static, |
360 | 0 | F::Output: 'static, |
361 | 0 | { |
362 | 0 | let id = crate::runtime::task::Id::next(); |
363 | 0 | #[cfg(all( |
364 | 0 | tokio_unstable, |
365 | 0 | tokio_taskdump, |
366 | 0 | feature = "rt", |
367 | 0 | target_os = "linux", |
368 | 0 | any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64") |
369 | 0 | ))] |
370 | 0 | let future = super::task::trace::Trace::root(future); |
371 | 0 | #[cfg(all(tokio_unstable, feature = "tracing"))] |
372 | 0 | let future = crate::util::trace::task(future, "task", meta, id.as_u64()); |
373 | 0 | self.inner.spawn_local(future, id, meta.spawned_at) |
374 | 0 | } |
375 | | |
376 | | /// Returns the flavor of the current `Runtime`. |
377 | | /// |
378 | | /// # Examples |
379 | | /// |
380 | | /// ``` |
381 | | /// use tokio::runtime::{Handle, RuntimeFlavor}; |
382 | | /// |
383 | | /// #[tokio::main(flavor = "current_thread")] |
384 | | /// async fn main() { |
385 | | /// assert_eq!(RuntimeFlavor::CurrentThread, Handle::current().runtime_flavor()); |
386 | | /// } |
387 | | /// ``` |
388 | | /// |
389 | | /// ``` |
390 | | /// use tokio::runtime::{Handle, RuntimeFlavor}; |
391 | | /// |
392 | | /// #[tokio::main(flavor = "multi_thread", worker_threads = 4)] |
393 | | /// async fn main() { |
394 | | /// assert_eq!(RuntimeFlavor::MultiThread, Handle::current().runtime_flavor()); |
395 | | /// } |
396 | | /// ``` |
397 | 0 | pub fn runtime_flavor(&self) -> RuntimeFlavor { |
398 | 0 | match self.inner { |
399 | 0 | scheduler::Handle::CurrentThread(_) => RuntimeFlavor::CurrentThread, |
400 | | #[cfg(feature = "rt-multi-thread")] |
401 | 0 | scheduler::Handle::MultiThread(_) => RuntimeFlavor::MultiThread, |
402 | | } |
403 | 0 | } |
404 | | |
405 | | cfg_unstable! { |
406 | | /// Returns the [`Id`] of the current `Runtime`. |
407 | | /// |
408 | | /// # Examples |
409 | | /// |
410 | | /// ``` |
411 | | /// use tokio::runtime::Handle; |
412 | | /// |
413 | | /// #[tokio::main(flavor = "current_thread")] |
414 | | /// async fn main() { |
415 | | /// println!("Current runtime id: {}", Handle::current().id()); |
416 | | /// } |
417 | | /// ``` |
418 | | /// |
419 | | /// **Note**: This is an [unstable API][unstable]. The public API of this type |
420 | | /// may break in 1.x releases. See [the documentation on unstable |
421 | | /// features][unstable] for details. |
422 | | /// |
423 | | /// [unstable]: crate#unstable-features |
424 | | /// [`Id`]: struct@crate::runtime::Id |
425 | | pub fn id(&self) -> runtime::Id { |
426 | | let owned_id = match &self.inner { |
427 | | scheduler::Handle::CurrentThread(handle) => handle.owned_id(), |
428 | | #[cfg(feature = "rt-multi-thread")] |
429 | | scheduler::Handle::MultiThread(handle) => handle.owned_id(), |
430 | | }; |
431 | | owned_id.into() |
432 | | } |
433 | | } |
434 | | |
435 | | /// Returns a view that lets you get information about how the runtime |
436 | | /// is performing. |
437 | 0 | pub fn metrics(&self) -> RuntimeMetrics { |
438 | 0 | RuntimeMetrics::new(self.clone()) |
439 | 0 | } |
440 | | } |
441 | | |
442 | | impl std::panic::UnwindSafe for Handle {} |
443 | | |
444 | | impl std::panic::RefUnwindSafe for Handle {} |
445 | | |
446 | | cfg_taskdump! { |
447 | | impl Handle { |
448 | | /// Captures a snapshot of the runtime's state. |
449 | | /// |
450 | | /// If you only want to capture a snapshot of a single future's state, you can use |
451 | | /// [`Trace::capture`][crate::runtime::dump::Trace]. |
452 | | /// |
453 | | /// This functionality is experimental, and comes with a number of |
454 | | /// requirements and limitations. |
455 | | /// |
456 | | /// # Examples |
457 | | /// |
458 | | /// This can be used to get call traces of each task in the runtime. |
459 | | /// Calls to `Handle::dump` should usually be enclosed in a |
460 | | /// [timeout][crate::time::timeout], so that dumping does not escalate a |
461 | | /// single blocked runtime thread into an entirely blocked runtime. |
462 | | /// |
463 | | /// ``` |
464 | | /// # use tokio::runtime::Runtime; |
465 | | /// # fn dox() { |
466 | | /// # let rt = Runtime::new().unwrap(); |
467 | | /// # rt.spawn(async { |
468 | | /// use tokio::runtime::Handle; |
469 | | /// use tokio::time::{timeout, Duration}; |
470 | | /// |
471 | | /// // Inside an async block or function. |
472 | | /// let handle = Handle::current(); |
473 | | /// if let Ok(dump) = timeout(Duration::from_secs(2), handle.dump()).await { |
474 | | /// for (i, task) in dump.tasks().iter().enumerate() { |
475 | | /// let trace = task.trace(); |
476 | | /// println!("TASK {i}:"); |
477 | | /// println!("{trace}\n"); |
478 | | /// } |
479 | | /// } |
480 | | /// # }); |
481 | | /// # } |
482 | | /// ``` |
483 | | /// |
484 | | /// This produces highly detailed traces of tasks; e.g.: |
485 | | /// |
486 | | /// ```plain |
487 | | /// TASK 0: |
488 | | /// ╼ dump::main::{{closure}}::a::{{closure}} at /tokio/examples/dump.rs:18:20 |
489 | | /// └╼ dump::main::{{closure}}::b::{{closure}} at /tokio/examples/dump.rs:23:20 |
490 | | /// └╼ dump::main::{{closure}}::c::{{closure}} at /tokio/examples/dump.rs:28:24 |
491 | | /// └╼ tokio::sync::barrier::Barrier::wait::{{closure}} at /tokio/tokio/src/sync/barrier.rs:129:10 |
492 | | /// └╼ <tokio::util::trace::InstrumentedAsyncOp<F> as core::future::future::Future>::poll at /tokio/tokio/src/util/trace.rs:77:46 |
493 | | /// └╼ tokio::sync::barrier::Barrier::wait_internal::{{closure}} at /tokio/tokio/src/sync/barrier.rs:183:36 |
494 | | /// └╼ tokio::sync::watch::Receiver<T>::changed::{{closure}} at /tokio/tokio/src/sync/watch.rs:604:55 |
495 | | /// └╼ tokio::sync::watch::changed_impl::{{closure}} at /tokio/tokio/src/sync/watch.rs:755:18 |
496 | | /// └╼ <tokio::sync::notify::Notified as core::future::future::Future>::poll at /tokio/tokio/src/sync/notify.rs:1103:9 |
497 | | /// └╼ tokio::sync::notify::Notified::poll_notified at /tokio/tokio/src/sync/notify.rs:996:32 |
498 | | /// ``` |
499 | | /// |
500 | | /// # Requirements |
501 | | /// |
502 | | /// ## Debug Info Must Be Available |
503 | | /// |
504 | | /// To produce task traces, the application must **not** be compiled |
505 | | /// with `split debuginfo`. On Linux, including `debuginfo` within the |
506 | | /// application binary is the (correct) default. You can further ensure |
507 | | /// this behavior with the following directive in your `Cargo.toml`: |
508 | | /// |
509 | | /// ```toml |
510 | | /// [profile.*] |
511 | | /// split-debuginfo = "off" |
512 | | /// ``` |
513 | | /// |
514 | | /// ## Unstable Features |
515 | | /// |
516 | | /// This functionality is **unstable**, and requires both the |
517 | | /// `tokio_unstable` and `tokio_taskdump` `cfg` flags to be set. |
518 | | /// |
519 | | /// You can do this by setting the `RUSTFLAGS` environment variable |
520 | | /// before invoking `cargo`; e.g.: |
521 | | /// ```bash |
522 | | /// RUSTFLAGS="--cfg tokio_unstable --cfg tokio_taskdump" cargo run --example dump |
523 | | /// ``` |
524 | | /// |
525 | | /// Or by [configuring][cargo-config] `rustflags` in |
526 | | /// `.cargo/config.toml`: |
527 | | /// ```text |
528 | | /// [build] |
529 | | /// rustflags = ["--cfg", "tokio_unstable", "--cfg", "tokio_taskdump"] |
530 | | /// ``` |
531 | | /// |
532 | | /// [cargo-config]: |
533 | | /// https://doc.rust-lang.org/cargo/reference/config.html |
534 | | /// |
535 | | /// ## Platform Requirements |
536 | | /// |
537 | | /// Task dumps are supported on Linux atop `aarch64`, `x86` and `x86_64`. |
538 | | /// |
539 | | /// ## Current Thread Runtime Requirements |
540 | | /// |
541 | | /// On the `current_thread` runtime, task dumps may only be requested |
542 | | /// from *within* the context of the runtime being dumped. Do not, for |
543 | | /// example, await `Handle::dump()` on a different runtime. |
544 | | /// |
545 | | /// # Limitations |
546 | | /// |
547 | | /// ## Performance |
548 | | /// |
549 | | /// Although enabling the `tokio_taskdump` feature imposes virtually no |
550 | | /// additional runtime overhead, actually calling `Handle::dump` is |
551 | | /// expensive. The runtime must synchronize and pause its workers, then |
552 | | /// re-poll every task in a special tracing mode. Avoid requesting dumps |
553 | | /// often. |
554 | | /// |
555 | | /// ## Local Executors |
556 | | /// |
557 | | /// Tasks managed by local executors (e.g., `FuturesUnordered` and |
558 | | /// [`LocalSet`][crate::task::LocalSet]) may not appear in task dumps. |
559 | | /// |
560 | | /// ## Non-Termination When Workers Are Blocked |
561 | | /// |
562 | | /// The future produced by `Handle::dump` may never produce `Ready` if |
563 | | /// another runtime worker is blocked for more than 250ms. This may |
564 | | /// occur if a dump is requested during shutdown, or if another runtime |
565 | | /// worker is infinite looping or synchronously deadlocked. For these |
566 | | /// reasons, task dumping should usually be paired with an explicit |
567 | | /// [timeout][crate::time::timeout]. |
568 | | pub async fn dump(&self) -> crate::runtime::Dump { |
569 | | match &self.inner { |
570 | | scheduler::Handle::CurrentThread(handle) => handle.dump(), |
571 | | #[cfg(all(feature = "rt-multi-thread", not(target_os = "wasi")))] |
572 | | scheduler::Handle::MultiThread(handle) => { |
573 | | // perform the trace in a separate thread so that the |
574 | | // trace itself does not appear in the taskdump. |
575 | | let handle = handle.clone(); |
576 | | spawn_thread(async { |
577 | | let handle = handle; |
578 | | handle.dump().await |
579 | | }).await |
580 | | }, |
581 | | } |
582 | | } |
583 | | |
584 | | /// Produces `true` if the current task is being traced for a dump; |
585 | | /// otherwise false. This function is only public for integration |
586 | | /// testing purposes. Do not rely on it. |
587 | | #[doc(hidden)] |
588 | | pub fn is_tracing() -> bool { |
589 | | super::task::trace::Context::is_tracing() |
590 | | } |
591 | | } |
592 | | |
593 | | cfg_rt_multi_thread! { |
594 | | /// Spawn a new thread and asynchronously await on its result. |
595 | | async fn spawn_thread<F>(f: F) -> <F as Future>::Output |
596 | | where |
597 | | F: Future + Send + 'static, |
598 | | <F as Future>::Output: Send + 'static |
599 | | { |
600 | | let (tx, rx) = crate::sync::oneshot::channel(); |
601 | | crate::loom::thread::spawn(|| { |
602 | | let rt = crate::runtime::Builder::new_current_thread().build().unwrap(); |
603 | | rt.block_on(async { |
604 | | let _ = tx.send(f.await); |
605 | | }); |
606 | | }); |
607 | | rx.await.unwrap() |
608 | | } |
609 | | } |
610 | | } |
611 | | |
612 | | /// Error returned by `try_current` when no Runtime has been started |
613 | | #[derive(Debug)] |
614 | | pub struct TryCurrentError { |
615 | | kind: TryCurrentErrorKind, |
616 | | } |
617 | | |
618 | | impl TryCurrentError { |
619 | 0 | pub(crate) fn new_no_context() -> Self { |
620 | 0 | Self { |
621 | 0 | kind: TryCurrentErrorKind::NoContext, |
622 | 0 | } |
623 | 0 | } |
624 | | |
625 | 0 | pub(crate) fn new_thread_local_destroyed() -> Self { |
626 | 0 | Self { |
627 | 0 | kind: TryCurrentErrorKind::ThreadLocalDestroyed, |
628 | 0 | } |
629 | 0 | } |
630 | | |
631 | | /// Returns true if the call failed because there is currently no runtime in |
632 | | /// the Tokio context. |
633 | 0 | pub fn is_missing_context(&self) -> bool { |
634 | 0 | matches!(self.kind, TryCurrentErrorKind::NoContext) |
635 | 0 | } |
636 | | |
637 | | /// Returns true if the call failed because the Tokio context thread-local |
638 | | /// had been destroyed. This can usually only happen if in the destructor of |
639 | | /// other thread-locals. |
640 | 0 | pub fn is_thread_local_destroyed(&self) -> bool { |
641 | 0 | matches!(self.kind, TryCurrentErrorKind::ThreadLocalDestroyed) |
642 | 0 | } |
643 | | } |
644 | | |
645 | | enum TryCurrentErrorKind { |
646 | | NoContext, |
647 | | ThreadLocalDestroyed, |
648 | | } |
649 | | |
650 | | impl fmt::Debug for TryCurrentErrorKind { |
651 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
652 | 0 | match self { |
653 | 0 | TryCurrentErrorKind::NoContext => f.write_str("NoContext"), |
654 | 0 | TryCurrentErrorKind::ThreadLocalDestroyed => f.write_str("ThreadLocalDestroyed"), |
655 | | } |
656 | 0 | } |
657 | | } |
658 | | |
659 | | impl fmt::Display for TryCurrentError { |
660 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
661 | | use TryCurrentErrorKind as E; |
662 | 0 | match self.kind { |
663 | 0 | E::NoContext => f.write_str(CONTEXT_MISSING_ERROR), |
664 | 0 | E::ThreadLocalDestroyed => f.write_str(THREAD_LOCAL_DESTROYED_ERROR), |
665 | | } |
666 | 0 | } |
667 | | } |
668 | | |
669 | | impl error::Error for TryCurrentError {} |