diff --git a/tokio/src/sync/oneshot.rs b/tokio/src/sync/oneshot.rs index a2bf5f9b4fb..0df6037a1c2 100644 --- a/tokio/src/sync/oneshot.rs +++ b/tokio/src/sync/oneshot.rs @@ -672,7 +672,7 @@ impl Receiver { return Err(TryRecvError::Empty); } } else { - panic!("called after complete"); + Err(TryRecvError::Closed) }; self.inner = None; diff --git a/tokio/tests/sync_oneshot.rs b/tokio/tests/sync_oneshot.rs index b1ea46a46ef..1aab810e16e 100644 --- a/tokio/tests/sync_oneshot.rs +++ b/tokio/tests/sync_oneshot.rs @@ -2,6 +2,7 @@ #![cfg(feature = "full")] use tokio::sync::oneshot; +use tokio::sync::oneshot::error::TryRecvError; use tokio_test::*; use std::future::Future; @@ -190,6 +191,17 @@ fn close_after_recv() { rx.close(); } +#[test] +fn try_recv_after_completion() { + let (tx, mut rx) = oneshot::channel::(); + + 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::();