Use checked_add for adding time in recv_timeout
This commit is contained in:
parent
7d03617bab
commit
018f8a027d
1 changed files with 18 additions and 6 deletions
|
@ -1321,12 +1321,13 @@ impl<T> Receiver<T> {
|
||||||
// Do an optimistic try_recv to avoid the performance impact of
|
// Do an optimistic try_recv to avoid the performance impact of
|
||||||
// Instant::now() in the full-channel case.
|
// Instant::now() in the full-channel case.
|
||||||
match self.try_recv() {
|
match self.try_recv() {
|
||||||
Ok(result)
|
Ok(result) => Ok(result),
|
||||||
=> Ok(result),
|
Err(TryRecvError::Disconnected) => Err(RecvTimeoutError::Disconnected),
|
||||||
Err(TryRecvError::Disconnected)
|
Err(TryRecvError::Empty) => match Instant::now().checked_add(timeout) {
|
||||||
=> Err(RecvTimeoutError::Disconnected),
|
Some(deadline) => self.recv_deadline(deadline),
|
||||||
Err(TryRecvError::Empty)
|
// So far in the future that it's practically the same as waiting indefinitely.
|
||||||
=> self.recv_deadline(Instant::now() + timeout)
|
None => self.recv().map_err(RecvTimeoutError::from),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2311,6 +2312,17 @@ mod tests {
|
||||||
assert_eq!(recv_count, stress);
|
assert_eq!(recv_count, stress);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn very_long_recv_timeout_wont_panic() {
|
||||||
|
let (tx, rx) = channel::<()>();
|
||||||
|
let join_handle = thread::spawn(move || {
|
||||||
|
rx.recv_timeout(Duration::from_secs(u64::max_value()))
|
||||||
|
});
|
||||||
|
thread::sleep(Duration::from_secs(1));
|
||||||
|
assert!(tx.send(()).is_ok());
|
||||||
|
assert_eq!(join_handle.join().unwrap(), Ok(()));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn recv_a_lot() {
|
fn recv_a_lot() {
|
||||||
// Regression test that we don't run out of stack in scheduler context
|
// Regression test that we don't run out of stack in scheduler context
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue