-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
sync: fix CancellationToken
failing to cancel the ready futures
#7462
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
sync: fix CancellationToken
failing to cancel the ready futures
#7462
Conversation
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.
Overall looks good to me.
When token is already cancelled, provided future should not be polled.
12e1569
to
0f71693
Compare
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.
Thanks!
Casual observer here, but I noticed that here is an explicit comment here about the bias of this polling-order: tokio/tokio-util/src/sync/cancellation_token.rs Lines 274 to 283 in 0a3fe46
And I think this bias was explicitly requested here: #6618 (comment) As an additional random thought, if the problem is related to calling |
This is a good finding, I didn't noticed this, and I think we should also change this comment because:
I'm also open for keeping the existing biased behavior for better performance of happy path, but writing a warning about this behavior and suggesting a recommended approach to deal with this. |
I tried to implement this and check it against the newly added test. I posted my changes to https://github.com/stepantubanov/tokio/pull/1/files in case you want to go that way. |
sync: fix and document cancellation bias
@lucab Thanks, seems like a good way to preserve the bias while correcting the behavior for this scenario. |
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.
@stepantubanov @lucab Thank you both for your contributions, this is a better solution that I didn't realize before.
CancellationToken::run_until_cancelled
CancellationToken
failing to cancel the ready futures
When token is already cancelled, provided future should not be polled.
Motivation
Example: We have a loop where we get something from a stream but want to exit in case of shutdown:
With current logic, as long as the stream is yielding new items, the loop is not terminating even after the token is cancelled, which seems like a bug to me.
Solution
I've changed the order of
if
branches to make it check the token before polling the future.