async-rs / async-rs/async-std

Document use of future::timeout in combination with channels

Open
#411 1 comment 3 reactions 0 assignees View on GitHub
documentation
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.