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

macros: define cancellation safety #5525

Merged
merged 3 commits into from Mar 18, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions tokio/src/macros/select.rs
Expand Up @@ -131,6 +131,10 @@
/// correctly even if it is restarted while waiting at an `.await`, then it is
/// cancellation safe.
///
/// Cancellation safety can be defined in the following way: If you have a
/// future that has not yet completed, then it must be a no-op to drop that
/// future and recreate it.
Copy link
Member

Choose a reason for hiding this comment

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

If this definition ought to be very precise, I would probably refrain from defining it through the observable effects of future drop + recreate. For example, I would say the following future is cancellation safe under this definition, but just dropping it halfway through the download without driving to the completion can leak disk space:

async fn download_and_verify() -> Sha256Sum {
    for i in 0..N {
        if !is_chunk_downloaded(i) {
            download_data_chunk(i).await;
        }
    }
    let sha_sum = compute_hash();
    delete_all_data_chunks();
    sha_sum
}

In my opinion, it is not safe to use this future inside the select! macro, please let me know if you agree.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How would you define it? Inserting a drop-and-recreate of your future in the middle of a program does change what it does, so in some sense it is not a no-op, even if the difference is not observable until later.

Copy link
Member

@satakuma satakuma Mar 3, 2023

Choose a reason for hiding this comment

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

True, I agree that it may not be considered as a no-op.

I was going to say that I would define it like this: “a future is cancellation safe if and only if creating it and dropping before completion is a no-op”. Now that I think of it, it does not account for losing a place in a fair queue, for example in Mutex::lock.

Maybe the definition should be more rigorous and state that both create-and-drop and drop-and-recreate should be no-ops (with “no-op” meaning “no observable effects”)? I am not sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I expanded a bit, but I think it's fine as-is.

///
/// Be aware that cancelling something that is not cancellation safe is not
/// necessarily wrong. For example, if you are cancelling a task because the
/// application is shutting down, then you probably don't care that partially
Expand Down