From 126ce89bb40f4d3ea2a174372694276e1b135985 Mon Sep 17 00:00:00 2001 From: Aaron Schweiger Date: Fri, 7 Jun 2024 03:17:25 -0400 Subject: [PATCH] task: implement `Clone` for `AbortHandle` (#6621) --- tokio/src/runtime/task/abort.rs | 8 ++++++++ tokio/src/runtime/tests/task.rs | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/tokio/src/runtime/task/abort.rs b/tokio/src/runtime/task/abort.rs index 2745b5602..899f54894 100644 --- a/tokio/src/runtime/task/abort.rs +++ b/tokio/src/runtime/task/abort.rs @@ -102,3 +102,11 @@ impl Drop for AbortHandle { self.raw.drop_abort_handle(); } } + +impl Clone for AbortHandle { + /// Returns a cloned `AbortHandle` that can be used to remotely abort this task. + fn clone(&self) -> Self { + self.raw.ref_inc(); + Self::new(self.raw) + } +} diff --git a/tokio/src/runtime/tests/task.rs b/tokio/src/runtime/tests/task.rs index a0604505c..fc1e40890 100644 --- a/tokio/src/runtime/tests/task.rs +++ b/tokio/src/runtime/tests/task.rs @@ -119,6 +119,29 @@ fn drop_abort_handle2() { handle.assert_dropped(); } +#[test] +fn drop_abort_handle_clone() { + let (ad, handle) = AssertDrop::new(); + let (notified, join) = unowned( + async { + drop(ad); + unreachable!() + }, + NoopSchedule, + Id::next(), + ); + let abort = join.abort_handle(); + let abort_clone = abort.clone(); + drop(join); + handle.assert_not_dropped(); + drop(notified); + handle.assert_not_dropped(); + drop(abort); + handle.assert_not_dropped(); + drop(abort_clone); + handle.assert_dropped(); +} + // Shutting down through Notified works #[test] fn create_shutdown1() {