| 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 | pub fn enter(&self) -> EnterGuard<'_> { |
| 85 | EnterGuard { |
| 86 | _guard: match context::try_set_current(&self.inner) { |
| 87 | Some(guard) => guard, |
| 88 | None => panic!("{}" , crate::util::error::THREAD_LOCAL_DESTROYED_ERROR), |
| 89 | }, |
| 90 | _handle_lifetime: PhantomData, |
| 91 | } |
| 92 | } |
| 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 | pub fn current() -> Self { |
| 140 | Handle { |
| 141 | inner: scheduler::Handle::current(), |
| 142 | } |
| 143 | } |
| 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 | pub fn try_current() -> Result<Self, TryCurrentError> { |
| 151 | context::with_current(|inner| Handle { |
| 152 | inner: inner.clone(), |
| 153 | }) |
| 154 | } |
| 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 | pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output> |
| 189 | where |
| 190 | F: Future + Send + 'static, |
| 191 | F::Output: Send + 'static, |
| 192 | { |
| 193 | let fut_size = mem::size_of::<F>(); |
| 194 | if fut_size > BOX_FUTURE_THRESHOLD { |
| 195 | self.spawn_named(Box::pin(future), SpawnMeta::new_unnamed(fut_size)) |
| 196 | } else { |
| 197 | self.spawn_named(future, SpawnMeta::new_unnamed(fut_size)) |
| 198 | } |
| 199 | } |
| 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 | pub fn spawn_blocking<F, R>(&self, func: F) -> JoinHandle<R> |
| 222 | where |
| 223 | F: FnOnce() -> R + Send + 'static, |
| 224 | R: Send + 'static, |
| 225 | { |
| 226 | self.inner.blocking_spawner().spawn_blocking(self, func) |
| 227 | } |
| 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 | pub fn block_on<F: Future>(&self, future: F) -> F::Output { |
| 301 | let fut_size = mem::size_of::<F>(); |
| 302 | if fut_size > BOX_FUTURE_THRESHOLD { |
| 303 | self.block_on_inner(Box::pin(future), SpawnMeta::new_unnamed(fut_size)) |
| 304 | } else { |
| 305 | self.block_on_inner(future, SpawnMeta::new_unnamed(fut_size)) |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | #[track_caller ] |
| 310 | fn block_on_inner<F: Future>(&self, future: F, _meta: SpawnMeta<'_>) -> F::Output { |
| 311 | #[cfg (all( |
| 312 | tokio_unstable, |
| 313 | tokio_taskdump, |
| 314 | feature = "rt" , |
| 315 | target_os = "linux" , |
| 316 | any(target_arch = "aarch64" , target_arch = "x86" , target_arch = "x86_64" ) |
| 317 | ))] |
| 318 | let future = super::task::trace::Trace::root(future); |
| 319 | |
| 320 | #[cfg (all(tokio_unstable, feature = "tracing" ))] |
| 321 | let future = |
| 322 | crate::util::trace::task(future, "block_on" , _meta, super::task::Id::next().as_u64()); |
| 323 | |
| 324 | // Enter the runtime context. This sets the current driver handles and |
| 325 | // prevents blocking an existing runtime. |
| 326 | context::enter_runtime(&self.inner, true, |blocking| { |
| 327 | blocking.block_on(future).expect("failed to park thread" ) |
| 328 | }) |
| 329 | } |
| 330 | |
| 331 | #[track_caller ] |
| 332 | pub(crate) fn spawn_named<F>(&self, future: F, _meta: SpawnMeta<'_>) -> JoinHandle<F::Output> |
| 333 | where |
| 334 | F: Future + Send + 'static, |
| 335 | F::Output: Send + 'static, |
| 336 | { |
| 337 | let id = crate::runtime::task::Id::next(); |
| 338 | #[cfg (all( |
| 339 | tokio_unstable, |
| 340 | tokio_taskdump, |
| 341 | feature = "rt" , |
| 342 | target_os = "linux" , |
| 343 | any(target_arch = "aarch64" , target_arch = "x86" , target_arch = "x86_64" ) |
| 344 | ))] |
| 345 | let future = super::task::trace::Trace::root(future); |
| 346 | #[cfg (all(tokio_unstable, feature = "tracing" ))] |
| 347 | let future = crate::util::trace::task(future, "task" , _meta, id.as_u64()); |
| 348 | self.inner.spawn(future, id) |
| 349 | } |
| 350 | |
| 351 | #[track_caller ] |
| 352 | #[allow (dead_code)] |
| 353 | pub(crate) unsafe fn spawn_local_named<F>( |
| 354 | &self, |
| 355 | future: F, |
| 356 | _meta: SpawnMeta<'_>, |
| 357 | ) -> JoinHandle<F::Output> |
| 358 | where |
| 359 | F: Future + 'static, |
| 360 | F::Output: 'static, |
| 361 | { |
| 362 | let id = crate::runtime::task::Id::next(); |
| 363 | #[cfg (all( |
| 364 | tokio_unstable, |
| 365 | tokio_taskdump, |
| 366 | feature = "rt" , |
| 367 | target_os = "linux" , |
| 368 | any(target_arch = "aarch64" , target_arch = "x86" , target_arch = "x86_64" ) |
| 369 | ))] |
| 370 | let future = super::task::trace::Trace::root(future); |
| 371 | #[cfg (all(tokio_unstable, feature = "tracing" ))] |
| 372 | let future = crate::util::trace::task(future, "task" , _meta, id.as_u64()); |
| 373 | self.inner.spawn_local(future, id) |
| 374 | } |
| 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 | pub fn runtime_flavor(&self) -> RuntimeFlavor { |
| 398 | match self.inner { |
| 399 | scheduler::Handle::CurrentThread(_) => RuntimeFlavor::CurrentThread, |
| 400 | #[cfg (feature = "rt-multi-thread" )] |
| 401 | scheduler::Handle::MultiThread(_) => RuntimeFlavor::MultiThread, |
| 402 | #[cfg (all(tokio_unstable, feature = "rt-multi-thread" ))] |
| 403 | scheduler::Handle::MultiThreadAlt(_) => RuntimeFlavor::MultiThreadAlt, |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | cfg_unstable! { |
| 408 | /// Returns the [`Id`] of the current `Runtime`. |
| 409 | /// |
| 410 | /// # Examples |
| 411 | /// |
| 412 | /// ``` |
| 413 | /// use tokio::runtime::Handle; |
| 414 | /// |
| 415 | /// #[tokio::main(flavor = "current_thread")] |
| 416 | /// async fn main() { |
| 417 | /// println!("Current runtime id: {}", Handle::current().id()); |
| 418 | /// } |
| 419 | /// ``` |
| 420 | /// |
| 421 | /// **Note**: This is an [unstable API][unstable]. The public API of this type |
| 422 | /// may break in 1.x releases. See [the documentation on unstable |
| 423 | /// features][unstable] for details. |
| 424 | /// |
| 425 | /// [unstable]: crate#unstable-features |
| 426 | /// [`Id`]: struct@crate::runtime::Id |
| 427 | pub fn id(&self) -> runtime::Id { |
| 428 | let owned_id = match &self.inner { |
| 429 | scheduler::Handle::CurrentThread(handle) => handle.owned_id(), |
| 430 | #[cfg (feature = "rt-multi-thread" )] |
| 431 | scheduler::Handle::MultiThread(handle) => handle.owned_id(), |
| 432 | #[cfg (all(tokio_unstable, feature = "rt-multi-thread" ))] |
| 433 | scheduler::Handle::MultiThreadAlt(handle) => handle.owned_id(), |
| 434 | }; |
| 435 | owned_id.into() |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /// Returns a view that lets you get information about how the runtime |
| 440 | /// is performing. |
| 441 | pub fn metrics(&self) -> RuntimeMetrics { |
| 442 | RuntimeMetrics::new(self.clone()) |
| 443 | } |
| 444 | } |
| 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 | #[cfg (all(tokio_unstable, feature = "rt-multi-thread" , not(target_os = "wasi" )))] |
| 582 | scheduler::Handle::MultiThreadAlt(_) => panic!("task dump not implemented for this runtime flavor" ), |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | /// Produces `true` if the current task is being traced for a dump; |
| 587 | /// otherwise false. This function is only public for integration |
| 588 | /// testing purposes. Do not rely on it. |
| 589 | #[doc (hidden)] |
| 590 | pub fn is_tracing() -> bool { |
| 591 | super::task::trace::Context::is_tracing() |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | cfg_rt_multi_thread! { |
| 596 | /// Spawn a new thread and asynchronously await on its result. |
| 597 | async fn spawn_thread<F>(f: F) -> <F as Future>::Output |
| 598 | where |
| 599 | F: Future + Send + 'static, |
| 600 | <F as Future>::Output: Send + 'static |
| 601 | { |
| 602 | let (tx, rx) = crate::sync::oneshot::channel(); |
| 603 | crate::loom::thread::spawn(|| { |
| 604 | let rt = crate::runtime::Builder::new_current_thread().build().unwrap(); |
| 605 | rt.block_on(async { |
| 606 | let _ = tx.send(f.await); |
| 607 | }); |
| 608 | }); |
| 609 | rx.await.unwrap() |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | /// Error returned by `try_current` when no Runtime has been started |
| 615 | #[derive (Debug)] |
| 616 | pub struct TryCurrentError { |
| 617 | kind: TryCurrentErrorKind, |
| 618 | } |
| 619 | |
| 620 | impl TryCurrentError { |
| 621 | pub(crate) fn new_no_context() -> Self { |
| 622 | Self { |
| 623 | kind: TryCurrentErrorKind::NoContext, |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | pub(crate) fn new_thread_local_destroyed() -> Self { |
| 628 | Self { |
| 629 | kind: TryCurrentErrorKind::ThreadLocalDestroyed, |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | /// Returns true if the call failed because there is currently no runtime in |
| 634 | /// the Tokio context. |
| 635 | pub fn is_missing_context(&self) -> bool { |
| 636 | matches!(self.kind, TryCurrentErrorKind::NoContext) |
| 637 | } |
| 638 | |
| 639 | /// Returns true if the call failed because the Tokio context thread-local |
| 640 | /// had been destroyed. This can usually only happen if in the destructor of |
| 641 | /// other thread-locals. |
| 642 | pub fn is_thread_local_destroyed(&self) -> bool { |
| 643 | matches!(self.kind, TryCurrentErrorKind::ThreadLocalDestroyed) |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | enum TryCurrentErrorKind { |
| 648 | NoContext, |
| 649 | ThreadLocalDestroyed, |
| 650 | } |
| 651 | |
| 652 | impl fmt::Debug for TryCurrentErrorKind { |
| 653 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 654 | match self { |
| 655 | TryCurrentErrorKind::NoContext => f.write_str(data:"NoContext" ), |
| 656 | TryCurrentErrorKind::ThreadLocalDestroyed => f.write_str(data:"ThreadLocalDestroyed" ), |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | impl fmt::Display for TryCurrentError { |
| 662 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 663 | use TryCurrentErrorKind as E; |
| 664 | match self.kind { |
| 665 | E::NoContext => f.write_str(CONTEXT_MISSING_ERROR), |
| 666 | E::ThreadLocalDestroyed => f.write_str(THREAD_LOCAL_DESTROYED_ERROR), |
| 667 | } |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | impl error::Error for TryCurrentError {} |
| 672 | |