Replies: 1 comment 1 reply
-
The waker could change in weird situations like if a user starts polling your future on one runtime and then moves it to another. You can use https://doc.rust-lang.org/stable/std/task/struct.Waker.html#method.will_wake to check if your previously saved waker needs to be changed out. Something like: if self.waker.as_ref().map_or(true, |waker| !waker.will_wake(cx.waker()) {
self.waker = Some(cx.waker.clone());
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to write a future that has a cancellation handle that can be used from another thread to cancel the future, which will
wake
the future to do one finalpoll
.To avoid overhead of synchronizing, I'd like to avoid updating the Waker each time that the future is polled.
I'm aware that Rust documents that "on multiple calls to poll, only the from the Context passed to the most recent call should be scheduled to receive a wakeup".
However, since we are only ever running on tokio runtime, I'm wondering if tokio runtime offers additional guarantees around the waker such that it would be okay to wake the task using the
Waker
from the firstpoll
. That way, we would avoid having to sychronize and update the waker on each poll.Based on some experiments, it seems that tokio always seems to use the same Waker to wake the task which means that it should be okay to not update the waker? If not, when does tokio change the Waker?
Beta Was this translation helpful? Give feedback.
All reactions