From dd3a5e9f461896c760a7c17d4448b2bcad0f4ade Mon Sep 17 00:00:00 2001 From: SparkyPotato Date: Mon, 13 Mar 2023 21:35:18 +0530 Subject: [PATCH 1/5] task: make `JoinHandle::abort_handle` public --- tokio/src/runtime/task/join.rs | 36 +++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/tokio/src/runtime/task/join.rs b/tokio/src/runtime/task/join.rs index 11c4b9ba311..60e26cc39d8 100644 --- a/tokio/src/runtime/task/join.rs +++ b/tokio/src/runtime/task/join.rs @@ -252,7 +252,41 @@ impl JoinHandle { } /// Returns a new `AbortHandle` that can be used to remotely abort this task. - pub(crate) fn abort_handle(&self) -> super::AbortHandle { + /// + /// Awaiting a task cancelled by the `AbortHandle` might complete as usual if the task was + /// already completed at the time it was cancelled, but most likely it + /// will fail with a [cancelled] `JoinError`. + /// + /// ```rust + /// use tokio::{time, task}; + /// + /// #[tokio::main] + /// async fn main() { + /// let mut handles = Vec::new(); + /// + /// handles.push(tokio::spawn(async { + /// time::sleep(time::Duration::from_secs(10)).await; + /// true + /// })); + /// + /// handles.push(tokio::spawn(async { + /// time::sleep(time::Duration::from_secs(10)).await; + /// false + /// })); + /// + /// let abort_handles: Vec = handles.iter().map(|h| h.abort_handle()).collect(); + /// + /// for handle in abort_handles { + /// handle.abort(); + /// } + /// + /// for handle in handles { + /// assert!(handle.await.unwrap_err().is_cancelled()); + /// } + /// } + /// ``` + /// [cancelled]: method@super::error::JoinError::is_cancelled + pub fn abort_handle(&self) -> super::AbortHandle { self.raw.ref_inc(); super::AbortHandle::new(self.raw) } From fd2b09ec2ea74d1ca1efcd345733a95a5928157e Mon Sep 17 00:00:00 2001 From: SparkyPotato Date: Wed, 15 Mar 2023 18:56:43 +0530 Subject: [PATCH 2/5] decouple doc-tests from timing --- tokio/src/runtime/task/join.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tokio/src/runtime/task/join.rs b/tokio/src/runtime/task/join.rs index 60e26cc39d8..6c4e600b12c 100644 --- a/tokio/src/runtime/task/join.rs +++ b/tokio/src/runtime/task/join.rs @@ -184,6 +184,7 @@ impl JoinHandle { /// /// #[tokio::main] /// async fn main() { + /// # tokio::time::pause(); /// let mut handles = Vec::new(); /// /// handles.push(tokio::spawn(async { @@ -262,6 +263,7 @@ impl JoinHandle { /// /// #[tokio::main] /// async fn main() { + /// # tokio::time::pause(); /// let mut handles = Vec::new(); /// /// handles.push(tokio::spawn(async { From befccf0e8567cdc7196177b39a29db3ff091aa91 Mon Sep 17 00:00:00 2001 From: SparkyPotato Date: Wed, 15 Mar 2023 21:02:40 +0530 Subject: [PATCH 3/5] fix current_thread runtime error --- tokio/src/runtime/task/join.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tokio/src/runtime/task/join.rs b/tokio/src/runtime/task/join.rs index 6c4e600b12c..400154abc7c 100644 --- a/tokio/src/runtime/task/join.rs +++ b/tokio/src/runtime/task/join.rs @@ -182,8 +182,8 @@ impl JoinHandle { /// ```rust /// use tokio::time; /// - /// #[tokio::main] - /// async fn main() { + /// # #[tokio::main(flavor = "current_thread")] + /// # async fn main() { /// # tokio::time::pause(); /// let mut handles = Vec::new(); /// @@ -204,7 +204,7 @@ impl JoinHandle { /// for handle in handles { /// assert!(handle.await.unwrap_err().is_cancelled()); /// } - /// } + /// # } /// ``` /// [cancelled]: method@super::error::JoinError::is_cancelled pub fn abort(&self) { @@ -261,9 +261,9 @@ impl JoinHandle { /// ```rust /// use tokio::{time, task}; /// - /// #[tokio::main] - /// async fn main() { - /// # tokio::time::pause(); + /// # #[tokio::main(flavor = "current_thread")] + /// # async fn main() { + /// # time::pause(); /// let mut handles = Vec::new(); /// /// handles.push(tokio::spawn(async { @@ -285,7 +285,7 @@ impl JoinHandle { /// for handle in handles { /// assert!(handle.await.unwrap_err().is_cancelled()); /// } - /// } + /// # } /// ``` /// [cancelled]: method@super::error::JoinError::is_cancelled pub fn abort_handle(&self) -> super::AbortHandle { From c8a53e22063a2475617dc25a67a10db54a04492b Mon Sep 17 00:00:00 2001 From: SparkyPotato Date: Thu, 16 Mar 2023 15:39:30 +0530 Subject: [PATCH 4/5] fix doc indentation --- tokio/src/runtime/task/join.rs | 72 +++++++++++++++++----------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/tokio/src/runtime/task/join.rs b/tokio/src/runtime/task/join.rs index 400154abc7c..ef758704932 100644 --- a/tokio/src/runtime/task/join.rs +++ b/tokio/src/runtime/task/join.rs @@ -184,26 +184,26 @@ impl JoinHandle { /// /// # #[tokio::main(flavor = "current_thread")] /// # async fn main() { - /// # tokio::time::pause(); - /// let mut handles = Vec::new(); - /// - /// handles.push(tokio::spawn(async { - /// time::sleep(time::Duration::from_secs(10)).await; - /// true - /// })); - /// - /// handles.push(tokio::spawn(async { - /// time::sleep(time::Duration::from_secs(10)).await; - /// false - /// })); - /// - /// for handle in &handles { - /// handle.abort(); - /// } - /// - /// for handle in handles { - /// assert!(handle.await.unwrap_err().is_cancelled()); - /// } + /// # tokio::time::pause(); + /// let mut handles = Vec::new(); + /// + /// handles.push(tokio::spawn(async { + /// time::sleep(time::Duration::from_secs(10)).await; + /// true + /// })); + /// + /// handles.push(tokio::spawn(async { + /// time::sleep(time::Duration::from_secs(10)).await; + /// false + /// })); + /// + /// for handle in &handles { + /// handle.abort(); + /// } + /// + /// for handle in handles { + /// assert!(handle.await.unwrap_err().is_cancelled()); + /// } /// # } /// ``` /// [cancelled]: method@super::error::JoinError::is_cancelled @@ -264,27 +264,27 @@ impl JoinHandle { /// # #[tokio::main(flavor = "current_thread")] /// # async fn main() { /// # time::pause(); - /// let mut handles = Vec::new(); + /// let mut handles = Vec::new(); /// - /// handles.push(tokio::spawn(async { - /// time::sleep(time::Duration::from_secs(10)).await; - /// true - /// })); + /// handles.push(tokio::spawn(async { + /// time::sleep(time::Duration::from_secs(10)).await; + /// true + /// })); /// - /// handles.push(tokio::spawn(async { - /// time::sleep(time::Duration::from_secs(10)).await; - /// false - /// })); + /// handles.push(tokio::spawn(async { + /// time::sleep(time::Duration::from_secs(10)).await; + /// false + /// })); /// - /// let abort_handles: Vec = handles.iter().map(|h| h.abort_handle()).collect(); + /// let abort_handles: Vec = handles.iter().map(|h| h.abort_handle()).collect(); /// - /// for handle in abort_handles { - /// handle.abort(); - /// } + /// for handle in abort_handles { + /// handle.abort(); + /// } /// - /// for handle in handles { - /// assert!(handle.await.unwrap_err().is_cancelled()); - /// } + /// for handle in handles { + /// assert!(handle.await.unwrap_err().is_cancelled()); + /// } /// # } /// ``` /// [cancelled]: method@super::error::JoinError::is_cancelled From 0b1ba5292bdc1bba8115438510fa795de2fa7d07 Mon Sep 17 00:00:00 2001 From: SparkyPotato Date: Thu, 16 Mar 2023 16:24:21 +0530 Subject: [PATCH 5/5] replace `time::pause()` with `start_paused` --- tokio/src/runtime/task/join.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tokio/src/runtime/task/join.rs b/tokio/src/runtime/task/join.rs index ef758704932..11c6bcdb182 100644 --- a/tokio/src/runtime/task/join.rs +++ b/tokio/src/runtime/task/join.rs @@ -182,9 +182,8 @@ impl JoinHandle { /// ```rust /// use tokio::time; /// - /// # #[tokio::main(flavor = "current_thread")] + /// # #[tokio::main(flavor = "current_thread", start_paused = true)] /// # async fn main() { - /// # tokio::time::pause(); /// let mut handles = Vec::new(); /// /// handles.push(tokio::spawn(async { @@ -221,9 +220,8 @@ impl JoinHandle { /// ```rust /// use tokio::time; /// - /// # #[tokio::main(flavor = "current_thread")] + /// # #[tokio::main(flavor = "current_thread", start_paused = true)] /// # async fn main() { - /// # time::pause(); /// let handle1 = tokio::spawn(async { /// // do some stuff here /// }); @@ -261,9 +259,8 @@ impl JoinHandle { /// ```rust /// use tokio::{time, task}; /// - /// # #[tokio::main(flavor = "current_thread")] + /// # #[tokio::main(flavor = "current_thread", start_paused = true)] /// # async fn main() { - /// # time::pause(); /// let mut handles = Vec::new(); /// /// handles.push(tokio::spawn(async {