From 2ef49fa57a2a4a2ac90f3d622298711faae28930 Mon Sep 17 00:00:00 2001 From: Zahari Dichev Date: Tue, 22 Sep 2020 11:30:47 +0300 Subject: [PATCH] Always expect `Pending` when polling the `Notified` Signed-off-by: Zahari Dichev --- tokio/src/sync/mpsc/chan.rs | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/tokio/src/sync/mpsc/chan.rs b/tokio/src/sync/mpsc/chan.rs index 540d8d8e264..af4f67a7fb9 100644 --- a/tokio/src/sync/mpsc/chan.rs +++ b/tokio/src/sync/mpsc/chan.rs @@ -8,7 +8,7 @@ use crate::sync::Notify; use std::fmt; use std::process; -use std::sync::atomic::Ordering::{AcqRel, Relaxed, SeqCst}; +use std::sync::atomic::Ordering::{AcqRel, Relaxed}; use std::task::Poll::{Pending, Ready}; use std::task::{Context, Poll}; @@ -223,19 +223,14 @@ where let notified = self.inner.notify_rx_closed.notified(); pin!(notified); - // Polling this for first time will register the waiter and - // return `Pending` or return `Ready` right away. If `Ready` - // is returned we are done - let aquired_lost_notification = - crate::future::poll_fn(|cx| match Pin::new(&mut notified).poll(cx) { - Poll::Ready(()) => Poll::Ready(true), - Poll::Pending => Poll::Ready(false), - }) - .await; - - if aquired_lost_notification { - return; - } + // Polling the future once is guaranteed to return `Pending` as `watch` + // only notifies using `notify_waiters`. + crate::future::poll_fn(|cx| { + let res = Pin::new(&mut notified).poll(cx); + assert!(!res.is_ready()); + Poll::Ready(()) + }) + .await; if self.inner.semaphore.closed() { return;