Skip to content
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

Add drop guard for cancellation token #3839

Merged
merged 3 commits into from
Jun 28, 2021

Conversation

MikailBag
Copy link
Contributor

Motivation

Usually, cancellation is implemented as dropping a future. However, this does not work when a future internally spawns background tasks.

Solution

A new method drop_guard is added. It returns a special guard which cancels the token unless disarmed.
Intended usage:

async fn my_simple_fut() {
    let token = CancellationToken::new();
    let _g = token.clone().drop_guard();
    tokio::task::spawn(async move {
         some_lib::do_work(..., token);
    });
    // some other work here.
}

@Darksonn Darksonn added A-tokio-util Area: The tokio-util crate M-sync Module: tokio/sync labels Jun 11, 2021
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides the doc-comment, this seems fine to me.

Comment on lines 281 to 282
/// Returns a `DropGuard` for this token.
pub fn drop_guard(self) -> DropGuard {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation could be improved.

@Darksonn
Copy link
Contributor

Any updates on this?

Comment on lines 281 to 283
/// Returns a `DropGuard` for this token.
/// This guard will cancel this token (and all its children)
/// on drop unless disarmed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newlines in doc-comments do not translate to newlines in the generated documentation unless there is an empty line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, i just dislike long lines :)

Do you think splitting first sentence into a separate paragraph makes sense?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

Generally, we wrap documentation at 80 chars to avoid long lines.

@Darksonn Darksonn merged commit d35ff70 into tokio-rs:master Jun 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio-util Area: The tokio-util crate M-sync Module: tokio/sync
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants