Document use of future::timeout in combination with channels
- Dominant language
- Rust
- Stars
- 4.1k
- Forks
- 339
- PR merge metrics
- No merged PRs in 30d
Description
[As shared here](https://www.reddit.com/r/rust/comments/doos98/asyncstd_v09911_released/f5pn0wb/), channels could potentially deadlock if the capacity isn't large enough:
```rust
let (s, r) = channel(1);
s.send(1).await;
s.send(2).await; // this will hang indefinitely
```
Instead we should probably mention that by using `future::timeout` this can be prevented from hanging indefinitely:
```rust
let (s, r) = channel(1);
future::timeout(s.send(1), Duration::from_secs(1).await);
future::timeout(s.send(2), Duration::from_secs(1).await);
```
Perhaps we could even mention in the docs that folks should probably default to something spacious such as `256` or `1024` messages in case of doubt. In general we probably shouldn't assume people have intuition about channels, and nudging them towards some safer default choices (because unlike with unbounded channels they have to choose) wouldn't be a bad idea probably (:
Contributor guide
Assessment
This issue has not been assessed yet.