Skip to content

Commit

Permalink
sync: fix panic in broadcast::Receiver drop (#3434)
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzbilgener authored Jan 20, 2021
1 parent cc0911a commit 7d5b12c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tokio/src/sync/broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ impl<T> Drop for Receiver<T> {

drop(tail);

while self.next != until {
while self.next < until {
match self.recv_ref(None) {
Ok(_) => {}
// The channel is closed
Expand Down
27 changes: 27 additions & 0 deletions tokio/src/sync/tests/loom_broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,30 @@ fn drop_rx() {
assert_ok!(th2.join());
});
}

#[test]
fn drop_multiple_rx_with_overflow() {
loom::model(move || {
// It is essential to have multiple senders and receivers in this test case.
let (tx, mut rx) = broadcast::channel(1);
let _rx2 = tx.subscribe();

let _ = tx.send(());
let tx2 = tx.clone();
let th1 = thread::spawn(move || {
block_on(async {
for _ in 0..100 {
let _ = tx2.send(());
}
});
});
let _ = tx.send(());

let th2 = thread::spawn(move || {
block_on(async { while let Ok(_) = rx.recv().await {} });
});

assert_ok!(th1.join());
assert_ok!(th2.join());
});
}

0 comments on commit 7d5b12c

Please sign in to comment.