Skip to content

Commit ad6f618

Browse files
authored
runtime: clarify the behavior of Handle::block_on (#7665)
1 parent 0f9ae13 commit ad6f618

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

tokio/src/runtime/handle.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,12 @@ impl Handle {
262262
///
263263
/// # Panics
264264
///
265-
/// This function panics if the provided future panics, if called within an
266-
/// asynchronous execution context, or if a timer future is executed on a runtime that has been
267-
/// shut down.
265+
/// This function will panic if any of the following conditions are met:
266+
/// - The provided future panics.
267+
/// - It is called from within an asynchronous context, such as inside
268+
/// [`Runtime::block_on`], `Handle::block_on`, or from a function annotated
269+
/// with [`tokio::main`].
270+
/// - A timer future is executed on a runtime that has been shut down.
268271
///
269272
/// # Examples
270273
///
@@ -306,6 +309,24 @@ impl Handle {
306309
/// # }
307310
/// ```
308311
///
312+
/// `Handle::block_on` may be combined with [`task::block_in_place`] to
313+
/// re-enter the async context of a multi-thread scheduler runtime:
314+
/// ```
315+
/// # #[cfg(not(target_family = "wasm"))]
316+
/// # {
317+
/// use tokio::task;
318+
/// use tokio::runtime::Handle;
319+
///
320+
/// # async fn docs() {
321+
/// task::block_in_place(move || {
322+
/// Handle::current().block_on(async move {
323+
/// // do something async
324+
/// });
325+
/// });
326+
/// # }
327+
/// # }
328+
/// ```
329+
///
309330
/// [`JoinError`]: struct@crate::task::JoinError
310331
/// [`JoinHandle`]: struct@crate::task::JoinHandle
311332
/// [`Runtime::block_on`]: fn@crate::runtime::Runtime::block_on
@@ -315,6 +336,8 @@ impl Handle {
315336
/// [`tokio::fs`]: crate::fs
316337
/// [`tokio::net`]: crate::net
317338
/// [`tokio::time`]: crate::time
339+
/// [`tokio::main`]: ../attr.main.html
340+
/// [`task::block_in_place`]: crate::task::block_in_place
318341
#[track_caller]
319342
pub fn block_on<F: Future>(&self, future: F) -> F::Output {
320343
let fut_size = mem::size_of::<F>();

0 commit comments

Comments
 (0)