-
Notifications
You must be signed in to change notification settings - Fork 27
notification: Replace async block with poll_recv #510
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
Changes from 2 commits
81e83bc
a8625fc
412afb0
22eaf39
b2f3794
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -203,24 +203,21 @@ impl Stream for Connection { | |
| loop { | ||
| let notification = match this.next_notification.take() { | ||
| Some(notification) => Some(notification), | ||
| None => { | ||
| let future = async { | ||
| tokio::select! { | ||
| notification = this.async_rx.recv() => notification, | ||
| notification = this.sync_rx.recv() => notification, | ||
| } | ||
| }; | ||
| futures::pin_mut!(future); | ||
|
|
||
| match future.poll_unpin(cx) { | ||
| Poll::Pending => None, | ||
| None => match this.async_rx.poll_recv(cx) { | ||
|
||
| Poll::Ready(Some(notification)) => Some(notification), | ||
| Poll::Ready(None) => | ||
| return Poll::Ready(Some(ConnectionEvent::CloseConnection { | ||
| notify: NotifyProtocol::Yes, | ||
| })), | ||
| Poll::Pending => match this.sync_rx.poll_recv(cx) { | ||
| Poll::Ready(Some(notification)) => Some(notification), | ||
| Poll::Ready(None) => | ||
| return Poll::Ready(Some(ConnectionEvent::CloseConnection { | ||
| notify: NotifyProtocol::Yes, | ||
| })), | ||
| Poll::Ready(Some(notification)) => Some(notification), | ||
| } | ||
| } | ||
| Poll::Pending => None, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| let Some(notification) = notification else { | ||
|
|
||
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.
https://docs.rs/tokio/1.49.0/tokio/sync/mpsc/struct.Receiver.html#cancel-safety
But this is cancel safe. So, I don't get your argument?
The wake up call should still be registered and the entire future be called when there was an event?
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.
Yep, the receivers should be cancel safe. The issue here is with the
let future = async { }. The context waker is registered by the innerrecvcalls inside the temporary future. The future would later on be dropped iffuture.poll_unpinreturnsPoll::Pending. Then, when thesync_rxgot a new notification, it would wake the waker corresponding to the dropped future, causing the poll_next to stall.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.
But the waker is for the entire task and not just the future. So, the waker just wakes up the entire task and not some particular future.
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.
I've dug a bit into tokio to figure this out, indeed I'm mistaken with the "stalled connection" because I assumed
recvworked similarly toreserve(the initial issue we noticed in webrtc):The tokio's bounded Receiver uses a waiting list of "context waker" via a wrapper over Semaphore implementation
I assumed that
sync_rx.recv()would call into the semaphoreacquireor similar to place the context waker into the linked list (obtaining an Acquire)sync_rx.recv()future would get dropped immediately, the waker would be removed on Drop from the linked listHowever, the semaphore is only used for capacity. When we call into recv, the Receiver stores the waker into a separate variable:
So regardless if the temporary
futuregets dropped, we'll still wake the proper waker under the hood. This PR justs turns into a tiny optimization to not create and drop a dedicatedasync block:D