From 634fcac2d9a26cb30fe14ec12e7e76da00d30214 Mon Sep 17 00:00:00 2001 From: Diggory Blake Date: Sun, 18 Jul 2021 21:46:35 +0100 Subject: [PATCH 1/2] runtime: drop future when polling cancelled future (#3965) --- tokio/src/runtime/task/harness.rs | 2 +- tokio/tests/task_abort.rs | 95 +++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/tokio/src/runtime/task/harness.rs b/tokio/src/runtime/task/harness.rs index 7f1c4e4cb0c..9f0b1071130 100644 --- a/tokio/src/runtime/task/harness.rs +++ b/tokio/src/runtime/task/harness.rs @@ -420,7 +420,7 @@ fn poll_future( cx: Context<'_>, ) -> PollFuture { if snapshot.is_cancelled() { - PollFuture::Complete(Err(JoinError::cancelled()), snapshot.is_join_interested()) + PollFuture::Complete(Err(cancel_task(core)), snapshot.is_join_interested()) } else { let res = panic::catch_unwind(panic::AssertUnwindSafe(|| { struct Guard<'a, T: Future> { diff --git a/tokio/tests/task_abort.rs b/tokio/tests/task_abort.rs index c524dc287d1..8f621683faa 100644 --- a/tokio/tests/task_abort.rs +++ b/tokio/tests/task_abort.rs @@ -1,6 +1,7 @@ #![warn(rust_2018_idioms)] #![cfg(feature = "full")] +use std::sync::Arc; use std::thread::sleep; use std::time::Duration; @@ -138,3 +139,97 @@ fn remote_abort_local_set_3929() { rt.block_on(local); jh2.join().unwrap(); } + +/// Checks that a suspended task can be aborted even if the `JoinHandle` is immediately dropped. +/// issue #3964: . +#[test] +fn test_abort_wakes_task_3964() { + let rt = tokio::runtime::Builder::new_current_thread() + .enable_time() + .build() + .unwrap(); + + rt.block_on(async move { + let notify_dropped = Arc::new(()); + let weak_notify_dropped = Arc::downgrade(¬ify_dropped); + + let handle = tokio::spawn(async move { + // Make sure the Arc is moved into the task + let _notify_dropped = notify_dropped; + println!("task started"); + tokio::time::sleep(std::time::Duration::new(100, 0)).await + }); + + // wait for task to sleep. + tokio::time::sleep(std::time::Duration::from_millis(10)).await; + + handle.abort(); + drop(handle); + + // wait for task to abort. + tokio::time::sleep(std::time::Duration::from_millis(10)).await; + + // Check that the Arc has been dropped. + assert!(weak_notify_dropped.upgrade().is_none()); + }); +} + +struct PanicOnDrop; + +impl Drop for PanicOnDrop { + fn drop(&mut self) { + panic!("Well what did you expect would happen..."); + } +} + +/// Checks that aborting a task whose destructor panics does not allow the +/// panic to escape the task. +#[test] +fn test_abort_task_that_panics_on_drop_contained() { + let rt = tokio::runtime::Builder::new_current_thread() + .enable_time() + .build() + .unwrap(); + + rt.block_on(async move { + let handle = tokio::spawn(async move { + // Make sure the Arc is moved into the task + let _panic_dropped = PanicOnDrop; + println!("task started"); + tokio::time::sleep(std::time::Duration::new(100, 0)).await + }); + + // wait for task to sleep. + tokio::time::sleep(std::time::Duration::from_millis(10)).await; + + handle.abort(); + drop(handle); + + // wait for task to abort. + tokio::time::sleep(std::time::Duration::from_millis(10)).await; + }); +} + +/// Checks that aborting a task whose destructor panics has the expected result. +#[test] +fn test_abort_task_that_panics_on_drop_returned() { + let rt = tokio::runtime::Builder::new_current_thread() + .enable_time() + .build() + .unwrap(); + + rt.block_on(async move { + let handle = tokio::spawn(async move { + // Make sure the Arc is moved into the task + let _panic_dropped = PanicOnDrop; + println!("task started"); + tokio::time::sleep(std::time::Duration::new(100, 0)).await + }); + + // wait for task to sleep. + tokio::time::sleep(std::time::Duration::from_millis(10)).await; + + handle.abort(); + assert!(handle.await.unwrap_err().is_panic()); + }); +} From 714704253443787cc0c9a395b6d94947bcf26687 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Mon, 19 Jul 2021 10:32:01 -0700 Subject: [PATCH 2/2] chore: prepare Tokio v1.8.2 release --- tokio/CHANGELOG.md | 8 ++++++++ tokio/Cargo.toml | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/tokio/CHANGELOG.md b/tokio/CHANGELOG.md index b2d256c9ecb..a96d27500ed 100644 --- a/tokio/CHANGELOG.md +++ b/tokio/CHANGELOG.md @@ -1,3 +1,11 @@ +# 1.8.2 (July 19, 2021) + +Fixes a missed edge case from 1.8.1. + +### Fixed + +- runtime: drop canceled future on next poll (#3965) + # 1.8.1 (July 6, 2021) Forward ports 1.5.1 fixes. diff --git a/tokio/Cargo.toml b/tokio/Cargo.toml index 2945b6a387b..bd7937f1b9e 100644 --- a/tokio/Cargo.toml +++ b/tokio/Cargo.toml @@ -7,12 +7,12 @@ name = "tokio" # - README.md # - Update CHANGELOG.md. # - Create "v1.0.x" git tag. -version = "1.8.1" +version = "1.8.2" edition = "2018" authors = ["Tokio Contributors "] license = "MIT" readme = "README.md" -documentation = "https://docs.rs/tokio/1.8.1/tokio/" +documentation = "https://docs.rs/tokio/1.8.2/tokio/" repository = "https://github.com/tokio-rs/tokio" homepage = "https://tokio.rs" description = """