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() {