Skip to content

Commit

Permalink
fix: correct usage of EventListener for scenarios that involve multip…
Browse files Browse the repository at this point in the history
…le wakes

This resolves a potential bug that has not yet been observed with Stopper. It would only be encountered if the Event were notified before the atomic boolean was stored. Although the usage of atomics should make guard against this, the future logic still should take into consideration the possibility that the future was erroneously woken.

I misunderstood the contract for EventListener, which turns out to be that each EventListener is only good for one wake, after which it needs to be replaced with a new listener. I thought that repeatedly polling the listener would repeatedly wake it on Event notification.

refs: smol-rs/event-listener#124
  • Loading branch information
jbr committed Mar 24, 2024
1 parent bbff3a1 commit 8dba61c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/future_stopper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ impl<F: Future> Future for FutureStopper<F> {
}

match Pin::new(&mut *this.event_listener).poll(cx) {
Poll::Ready(()) => continue,
Poll::Ready(()) => {
*this.event_listener = this.stopper.0.event.listen();
continue;
}
Poll::Pending => {
return match this.future.poll(cx) {
Poll::Ready(output) => Poll::Ready(Some(output)),
Poll::Pending => Poll::Pending,
}
return this.future.poll(cx).map(Some);
}
};
}
Expand Down
5 changes: 4 additions & 1 deletion src/stopped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ impl Future for Stopped {
}

match Pin::new(&mut *event_listener).poll(cx) {
Poll::Ready(()) => continue,
Poll::Ready(()) => {
*event_listener = stopper.0.event.listen();
continue;
}
Poll::Pending => return Poll::Pending,
};
}
Expand Down
5 changes: 4 additions & 1 deletion src/stream_stopper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ impl<S: Stream> Stream for StreamStopper<S> {
}

match Pin::new(&mut *this.event_listener).poll(cx) {
Poll::Ready(()) => continue,
Poll::Ready(()) => {
*this.event_listener = this.stopper.0.event.listen();
continue;
}
Poll::Pending => return this.stream.poll_next(cx),
};
}
Expand Down

0 comments on commit 8dba61c

Please sign in to comment.