Skip to content
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

sync: fix mpsc bug related to closing the channel #3215

Merged
merged 2 commits into from Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions tokio/src/sync/semaphore_ll.rs
Expand Up @@ -917,6 +917,10 @@ impl Waiter {
let mut curr = WaiterState(self.state.load(Acquire));

loop {
if curr.is_closed() {
return 0;
}

if !curr.is_queued() {
assert_eq!(0, curr.permits_to_acquire());
}
Expand Down
24 changes: 24 additions & 0 deletions tokio/tests/sync_mpsc.rs
Expand Up @@ -512,3 +512,27 @@ fn ready_close_cancel_bounded() {

assert!(recv.is_woken());
}

#[tokio::test]
async fn permit_available_not_acquired_close() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it might be nice to have a comment here explaining what this test reproduces?

use futures::future::poll_fn;

let (mut tx1, mut rx) = mpsc::channel::<()>(1);
let mut tx2 = tx1.clone();

{
let mut ready = task::spawn(poll_fn(|cx| tx1.poll_ready(cx)));
assert_ready_ok!(ready.poll());
}

let mut ready = task::spawn(poll_fn(|cx| tx2.poll_ready(cx)));
assert_pending!(ready.poll());

rx.close();

drop(tx1);
assert!(ready.is_woken());

drop(tx2);
assert!(rx.recv().await.is_none());
}