summaryrefslogtreecommitdiffstats
path: root/tokio/src/runtime
diff options
context:
space:
mode:
authorSunjay Varma <sunjay@users.noreply.github.com>2020-05-14 12:28:56 -0400
committerGitHub <noreply@github.com>2020-05-14 12:28:56 -0400
commita343b1d18002ce3aa306e48c6538bd9e48103e9a (patch)
treeb59bb13a6de0c2d8bd9352e4bf3b9e7ec0bc3ea1 /tokio/src/runtime
parentb44ab273597d3757b5b50eed95c4f8890fa54e42 (diff)
Clarifying that Handle::current must be called on a thread managed by tokio (#2493)
Diffstat (limited to 'tokio/src/runtime')
-rw-r--r--tokio/src/runtime/handle.rs18
1 files changed, 15 insertions, 3 deletions
diff --git a/tokio/src/runtime/handle.rs b/tokio/src/runtime/handle.rs
index c356f053..0716a7fa 100644
--- a/tokio/src/runtime/handle.rs
+++ b/tokio/src/runtime/handle.rs
@@ -76,11 +76,13 @@ impl Handle {
context::enter(self.clone(), f)
}
- /// Returns a Handle view over the currently running Runtime
+ /// Returns a `Handle` view over the currently running `Runtime`
///
/// # Panic
///
- /// This will panic if called outside the context of a Tokio runtime.
+ /// This will panic if called outside the context of a Tokio runtime. That means that you must
+ /// call this on one of the threads **being run by the runtime**. Calling this from within a
+ /// thread created by `std::thread::spawn` (for example) will cause a panic.
///
/// # Examples
///
@@ -88,6 +90,7 @@ impl Handle {
/// block or function running on that runtime.
///
/// ```
+ /// # use std::thread;
/// # use tokio::runtime::Runtime;
/// # fn dox() {
/// # let rt = Runtime::new().unwrap();
@@ -98,7 +101,16 @@ impl Handle {
/// let handle = Handle::current();
/// handle.spawn(async {
/// println!("now running in the existing Runtime");
- /// })
+ /// });
+ ///
+ /// # let handle =
+ /// thread::spawn(move || {
+ /// // Notice that the handle is created outside of this thread and then moved in
+ /// handle.block_on(async { /* ... */ })
+ /// // This next line would cause a panic
+ /// // let handle2 = Handle::current();
+ /// });
+ /// # handle.join().unwrap();
/// # });
/// # }
/// ```