From 2af340c0ec72f0033bda65d920ac04ae9e1c0cae Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 8 Jul 2016 19:05:10 -0700 Subject: [PATCH] Check for data in Receiver::try_recv before reporting disconnect --- src/libstd/sync/mpsc/mod.rs | 9 +++++++++ src/libstd/sync/mpsc/sync.rs | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/libstd/sync/mpsc/mod.rs b/src/libstd/sync/mpsc/mod.rs index 34bc210b3c823..d96fd6228e62e 100644 --- a/src/libstd/sync/mpsc/mod.rs +++ b/src/libstd/sync/mpsc/mod.rs @@ -2180,6 +2180,15 @@ mod sync_tests { assert!(rx.recv().is_err()); } + #[test] + fn oneshot_single_thread_try_recv_closed_with_data() { + let (tx, rx) = sync_channel::(1); + tx.send(10).unwrap(); + drop(tx); + assert_eq!(rx.try_recv(), Ok(10)); + assert_eq!(rx.try_recv(), Err(TryRecvError::Disconnected)); + } + #[test] fn oneshot_single_thread_peek_data() { let (tx, rx) = sync_channel::(1); diff --git a/src/libstd/sync/mpsc/sync.rs b/src/libstd/sync/mpsc/sync.rs index f021689acad58..9d13a71ff95ee 100644 --- a/src/libstd/sync/mpsc/sync.rs +++ b/src/libstd/sync/mpsc/sync.rs @@ -309,7 +309,7 @@ impl Packet { let mut guard = self.lock.lock().unwrap(); // Easy cases first - if guard.disconnected { return Err(Disconnected) } + if guard.disconnected && guard.buf.size() == 0 { return Err(Disconnected) } if guard.buf.size() == 0 { return Err(Empty) } // Be sure to wake up neighbors