-
Notifications
You must be signed in to change notification settings - Fork 477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
channel: Panic in .recv_timeout(Duration::MAX) #795
Comments
It would be ideal if there were a method like Instant::saturating_add, but unfortunately it does not exist, so for now it seems we have to wait a few decades or wait forever. |
There's checked add, so I'm thinking something like self.recv_deadline(match Instant::now().checked_add(timeout) {
Some(deadline) => deadline,
None => Instant::now() + Duration::from_secs(86400 * 365 * 30) // hack from tokio-rs
}) |
Sounds good to me. |
798: channel: Do not panic on very large timeout r=taiki-e a=taiki-e Fixes #795 Co-authored-by: Taiki Endo <[email protected]>
Fixed in crossbeam-channel 0.5.3. |
I was planning to make a PR xD thanks for fixing it, this was quite the footgun 👍 |
I fixed this in the PR to std with: pub fn recv_timeout(&self, timeout: Duration) -> Result<T, RecvTimeoutError> {
- self.recv_deadline(Instant::now() + timeout)
+ match Instant::now().checked_add(timeout) {
+ Some(deadline) => self.recv_deadline(deadline),
+ // So far in the future that it's practically the same as waiting indefinitely.
+ None => self.recv().map_err(RecvTimeoutError::from),
+ }
} |
@ibraheemdev I agree that is also reasonable approach. |
953: Wait forever on very large timeout r=ibraheemdev a=taiki-e Adopts the same approach as the standard library: #795 (comment) r? `@ibraheemdev` bors d=ibraheemdev Co-authored-by: Taiki Endo <[email protected]>
953: Wait forever on very large timeout r=ibraheemdev a=taiki-e Adopts the same approach as the standard library: #795 (comment) r? `@ibraheemdev` bors d=ibraheemdev Co-authored-by: Taiki Endo <[email protected]>
https://github.com/crossbeam-rs/crossbeam/blob/master/crossbeam-channel/src/channel.rs#L863
Calling
recv_timeout()
withDuration::MAX
causes panic due toInstant
overflow.This happens quite naturally when the duration comes from some calculation, meaning "wait forever".
I don't think it should crash, insted wait the longest possible time.
The text was updated successfully, but these errors were encountered: