-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
task: add AbortHandle
type for cancelling tasks in a JoinSet
#4530
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8992cdc
rt: add internal `AbortHandle` type
hawkw 48dbc38
rt: add abort handle to task combination tests
hawkw f18bb86
rt: make `AbortHandle::abort` consume `self`
hawkw 87fe715
rt: expose `JoinHandle` with tokio-unstable
hawkw 2a61f48
task: add `AbortHandle` to `JoinSet`
hawkw 87d4ed0
fix missing `tokio_unstable` docs
hawkw 107f78c
fix wasm tests i guess
hawkw 9dac5b6
+rustfmt
hawkw b6dd2fd
Update tokio/src/runtime/task/join.rs
hawkw d77c9be
cleanup feature flags, add abort module
hawkw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use crate::runtime::task::RawTask; | ||
use std::fmt; | ||
use std::panic::{RefUnwindSafe, UnwindSafe}; | ||
|
||
/// An owned permission to abort a spawned task, without awaiting its completion. | ||
/// | ||
/// Unlike a [`JoinHandle`], an `AbortHandle` does *not* represent the | ||
/// permission to await the task's completion, only to terminate it. | ||
/// | ||
/// The task may be aborted by calling the [`AbortHandle::abort`] method. | ||
/// Dropping an `AbortHandle` releases the permission to terminate the task | ||
/// --- it does *not* abort the task. | ||
/// | ||
/// **Note**: This is an [unstable API][unstable]. The public API of this type | ||
/// may break in 1.x releases. See [the documentation on unstable | ||
/// features][unstable] for details. | ||
/// | ||
/// [unstable]: crate#unstable-features | ||
/// [`JoinHandle`]: crate::task::JoinHandle | ||
#[cfg_attr(docsrs, doc(cfg(all(feature = "rt", tokio_unstable))))] | ||
#[cfg_attr(not(tokio_unstable), allow(unreachable_pub))] | ||
pub struct AbortHandle { | ||
raw: Option<RawTask>, | ||
} | ||
|
||
impl AbortHandle { | ||
pub(super) fn new(raw: Option<RawTask>) -> Self { | ||
Self { raw } | ||
} | ||
|
||
/// Abort the task associated with the handle. | ||
/// | ||
/// Awaiting a cancelled task 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`. | ||
/// | ||
/// If the task was already cancelled, such as by [`JoinHandle::abort`], | ||
/// this method will do nothing. | ||
/// | ||
/// [cancelled]: method@super::error::JoinError::is_cancelled | ||
// the `AbortHandle` type is only publicly exposed when `tokio_unstable` is | ||
// enabled, but it is still defined for testing purposes. | ||
#[cfg_attr(not(tokio_unstable), allow(unreachable_pub))] | ||
pub fn abort(self) { | ||
if let Some(raw) = self.raw { | ||
raw.remote_abort(); | ||
} | ||
} | ||
} | ||
|
||
unsafe impl Send for AbortHandle {} | ||
unsafe impl Sync for AbortHandle {} | ||
|
||
impl UnwindSafe for AbortHandle {} | ||
impl RefUnwindSafe for AbortHandle {} | ||
|
||
impl fmt::Debug for AbortHandle { | ||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
fmt.debug_struct("AbortHandle").finish() | ||
} | ||
} | ||
|
||
impl Drop for AbortHandle { | ||
fn drop(&mut self) { | ||
if let Some(raw) = self.raw.take() { | ||
raw.drop_abort_handle(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,44 @@ fn create_drop2() { | |
handle.assert_dropped(); | ||
} | ||
|
||
#[test] | ||
fn drop_abort_handle1() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests could be marked as |
||
let (ad, handle) = AssertDrop::new(); | ||
let (notified, join) = unowned( | ||
async { | ||
drop(ad); | ||
unreachable!() | ||
}, | ||
NoopSchedule, | ||
); | ||
let abort = join.abort_handle(); | ||
drop(join); | ||
handle.assert_not_dropped(); | ||
drop(notified); | ||
handle.assert_not_dropped(); | ||
drop(abort); | ||
handle.assert_dropped(); | ||
} | ||
|
||
#[test] | ||
fn drop_abort_handle2() { | ||
let (ad, handle) = AssertDrop::new(); | ||
let (notified, join) = unowned( | ||
async { | ||
drop(ad); | ||
unreachable!() | ||
}, | ||
NoopSchedule, | ||
); | ||
let abort = join.abort_handle(); | ||
drop(notified); | ||
handle.assert_not_dropped(); | ||
drop(abort); | ||
handle.assert_not_dropped(); | ||
drop(join); | ||
handle.assert_dropped(); | ||
} | ||
|
||
// Shutting down through Notified works | ||
#[test] | ||
fn create_shutdown1() { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this being enabled w/
test
? This is not a public API, so it seems ok, but I am not following the thread.Also, in #4518, I added a
cfg_unstable!
macro.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason it's enabled with
cfg(test)
is because I addedAbortHandle
to thetask_combinations.rs
tasks, where it's kind of tightly-coupled with the rest of the test. That could be cfg flagged, but it would mean filling the test code with a bunch of#[cfg(tokio_unstable)]
attributes --- unlike thetask.rs
tests, we can't just stick#[cfg(tokio_unstable)]
on a couple of unit tests and be done.