-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Rewrite tokio-util::sync::CancellationToken #4652
Rewrite tokio-util::sync::CancellationToken #4652
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 this looks good.
I'm struggling to understand the point of impl<'a> Future for WaitForCancellationFuture<'a> {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
let mut this = self.project();
if this.cancellation_token.is_cancelled() {
return Poll::Ready(());
}
this.future.as_mut().poll(cx)
}
} As far as I can tell this would preserve the following properties:
Would anyone be able to provide a more in-depth explanation? Thanks in advance. |
@Ten0 Because once future returns Poll::Ready it usually cannot be polled again, otherwise it might panic. Previous implementation always return Poll::Ready(()) if the token is akready cancelled and changing this might be a breaking change. |
Wouldn't property 2 "We can't get a poll return Ready then have is_cancelled = false because it's set before" guarantee that it would still return ready immediately again without re polling the inner future? |
You are right, you can indeed rewrite it and remove the loop. |
I remember having had this discussion already, and there were reasons. @Darksonn might have to pitch in, she explained it to me back then |
I think the reason was that |
Regarding the order: would you mind explaining how it would be possible to first get |
I can't see anywhere that it's susceptible to spurious wake ups though (unless those are explicitly triggered by the tree implem). Thanks! :)
My original message was saying the alternate implementation seems to preserve the properties that this still can't happen (for the reasons in the comment in the code). I'm not sure what you're referring to. |
Ah OK never mind then, I must have misunderstood your original comment then :) To my understanding |
When said information is a boolean that can only go from false to true that might be enough. But I get the will for consistency. |
The implementation of |
Alright, thanks! Might be worth mentioning those in the documentation then (did I miss them?). |
Fixes leak from #4639.
Motivation
CancellationToken uses a lot of unsafe code and seems to be error-prone.
Solution
@Darksonn suggested a rewrite.
After a discussion on discord this is my attempt to combine and polish our ideas.
A thorough review will be necessary.
State