Skip to content

Commit

Permalink
sync: don't panic in oneshot::try_recv (#3674)
Browse files Browse the repository at this point in the history
  • Loading branch information
xd009642 authored Apr 4, 2021
1 parent b05b9a1 commit f93bc9b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tokio/src/sync/oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ impl<T> Receiver<T> {
return Err(TryRecvError::Empty);
}
} else {
panic!("called after complete");
Err(TryRecvError::Closed)
};

self.inner = None;
Expand Down
12 changes: 12 additions & 0 deletions tokio/tests/sync_oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![cfg(feature = "full")]

use tokio::sync::oneshot;
use tokio::sync::oneshot::error::TryRecvError;
use tokio_test::*;

use std::future::Future;
Expand Down Expand Up @@ -190,6 +191,17 @@ fn close_after_recv() {
rx.close();
}

#[test]
fn try_recv_after_completion() {
let (tx, mut rx) = oneshot::channel::<i32>();

tx.send(17).unwrap();

assert_eq!(17, rx.try_recv().unwrap());
assert_eq!(Err(TryRecvError::Closed), rx.try_recv());
rx.close();
}

#[test]
fn drops_tasks() {
let (mut tx, mut rx) = oneshot::channel::<i32>();
Expand Down

0 comments on commit f93bc9b

Please sign in to comment.