skip to content

With Rust's std::sync::mpsc, why can a receive loop hang after every worker finishes, and what do send, recv, try_recv and recv_timeout report on disconnection?

level: middleimportance: must knowfreq 65%

answer

  1. who still holds a Sender
  2. clones count like the original
  3. drop(tx) before the loop
  4. Empty vs Disconnected, Timeout vs Disconnected

basics

~20 s

A std Receiver only sees disconnection when every Sender, including the original kept in main, is dropped; until then recv blocks. recv then returns RecvError, try_recv and recv_timeout distinguish empty from disconnected, and send returns SendError with the value.

solid answer

~30 s

The receiving side closes only when the last `Sender` or `SyncSender` drops. Workers drop their clones when their closures return, but if `main` keeps the original `tx`, `for m in rx` drains the results and then waits for ever; the fix is `drop(tx)` before the loop. Queued messages are still delivered after disconnection. Then `recv` returns `Err(RecvError)`, `try_recv` returns `TryRecvError::Empty` while connected and `Disconnected` after, and `recv_timeout` returns `RecvTimeoutError::Timeout` or `Disconnected`. In the other direction, once the `Receiver` is dropped, `send` returns `Err(SendError(value))`, handing the value back, so a producer should stop rather than `unwrap()` into a panic.

code

rust · 25 lines
rust
use std::sync::mpsc::{self, RecvTimeoutError, TryRecvError};
use std::thread;
use std::time::Duration;

fn main() {
    let (tx, rx) = mpsc::channel::<u32>();
    let mut workers = Vec::new();
    for id in 0..3 {
        let tx = tx.clone(); // one Sender per resize worker
        workers.push(thread::spawn(move || tx.send(id).unwrap()));
    }
    for w in workers {
        w.join().unwrap();
    }
    let got = rx.try_iter().count(); // takes what is queued, never blocks
    println!("drained {got}");
    // main still owns the original `tx`, so the channel is empty, not closed
    assert_eq!(rx.try_recv(), Err(TryRecvError::Empty));
    let r = rx.recv_timeout(Duration::from_millis(50));
    assert_eq!(r, Err(RecvTimeoutError::Timeout));
    drop(tx); // the last Sender goes away
    assert_eq!(rx.try_recv(), Err(TryRecvError::Disconnected));
    assert!(rx.recv().is_err()); // RecvError: no more messages, ever
    println!("closed");
}

go deeper

for a junior

Remember that clones and the original Sender all count, and that drop(tx) before the receive loop prevents the hang.

for a middle

Explain the error variants of recv, try_recv, recv_timeout and send, and that buffered messages are delivered before RecvError.

for a senior

Show you trace which handle keeps a channel open, avoid busy loops on Disconnected, and stop producers cleanly on SendError instead of unwrapping.

for a principal

Make drop-based shutdown part of the pipeline's contract, so ending input closes every stage in turn without extra stop signals.

## When a std channel counts as closed A `std::sync::mpsc` channel has two kinds of handle: any number of `Sender<T>` (or `SyncSender<T>`) values and exactly one `Receiver<T>`. **Disconnection** is defined by drops, never by an explicit close call: - The **receiving side is disconnected** when the last sending handle has been dropped. `Sender::clone` makes a new handle that counts exactly like the original; the docs warn that "all senders (including the original) need to be dropped" for `recv` to stop blocking. - The **sending side is disconnected** when the `Receiver` has been dropped. A worker's clone usually drops when its `move` closure returns, so workers clean up after themselves. The handle that does not is the **original `tx`** that the setup code kept, typically in `main`. ## Why the receive loop hangs The classic shape, from the image pipeline's resize stage: 1. `main` creates `(tx, rx)` and, for each of three resize workers, does `let tx = tx.clone();` and moves the clone into `thread::spawn`. 2. Each worker sends its results and returns, dropping its clone. 3. `main` runs `for img in rx { ... }`. The loop prints every result and then blocks for ever. Nothing is wrong with the workers: `main` still owns the original `tx`, so from the channel's point of view a producer is alive and more messages might come. Fixes, in order of preference: - **`drop(tx);`** right after spawning the workers, before the loop. - Move the original into the last worker instead of cloning it. - Create the clones inside a block or function so the original goes out of scope before receiving. Diagnosing it in production is a matter of noticing a consumer thread parked in `recv` while every producer has exited; a thread dump shows the consumer waiting, and a review finds the surviving handle. A handle can also hide inside a struct, a `Vec` of senders, or a closure stored somewhere, which keeps the channel open just as surely. ## What each operation reports | Call | Empty but connected | Other half gone | |---|---|---| | `Receiver::recv()` | blocks | `Err(RecvError)` once the queue is drained | | `Receiver::try_recv()` | `Err(TryRecvError::Empty)` | `Err(TryRecvError::Disconnected)` | | `Receiver::recv_timeout(d)` | `Err(RecvTimeoutError::Timeout)` after `d` | `Err(RecvTimeoutError::Disconnected)` | | `Sender::send(v)` / `SyncSender::send(v)` | succeeds (a bounded send may block for space) | `Err(SendError(v))`, value handed back | | `SyncSender::try_send(v)` | `Err(TrySendError::Full(v))` when full | `Err(TrySendError::Disconnected(v))` | Two details interviewers probe: 1. **Queued messages survive disconnection.** `recv` keeps returning buffered messages after the last `Sender` is dropped and reports `Err` only when the queue is empty. Nothing sent before the drop is lost. 2. **`send` returning `Ok` proves little.** The docs say an `Ok` "does *not* mean that the data will be received": the `Receiver` can hang up immediately afterwards. Only an `Err` is definite, and it returns the value so the producer can log it, retry elsewhere or drop it deliberately. The exception is a rendezvous channel, `sync_channel(0)`, whose successful send means the receiver took the message. ## The panic cascade from `unwrap` Most examples call `tx.send(x).unwrap()`. When the downstream stage dies (it panicked, or returned early), its `Receiver` drops, every subsequent `send` returns `Err`, and every producer's `unwrap()` panics too. The module docs describe this as letting failure propagate among threads, and for a small program that is a fine default. In a long-running service, a producer should treat `Err(SendError(_))` as "the consumer is gone": stop producing, break out of its loop and let its own handles drop, so the shutdown flows upstream instead of turning into a chain of panics. ## Choosing the receive call - **`recv`** for a dedicated consumer thread that has nothing else to do. - **`try_recv` or `try_iter`** for a thread that polls between other work (a game loop draining events once per frame, for example); `try_iter` yields what is queued and stops at the first `Empty` or `Disconnected`. - **`recv_timeout`** when the consumer must wake periodically: to flush a partial batch, check a deadline or emit a heartbeat. Match `Timeout` and `Disconnected` separately; treating both as "try again" turns a closed channel into a busy loop. - `Receiver::recv_deadline` exists but is still unstable, so stable code uses `recv_timeout`.

  • A worker panics halfway through. Does that close the channel for the consumer?
    Only that worker's handle. With the default unwinding strategy, unwinding drops the closure's captured `Sender`, so it stops counting, but the channel stays open while any other clone, or main's original, is alive. The consumer finds out about the panic from that worker's `JoinHandle::join`, not from the channel.
  • Why is `loop { if let Ok(m) = rx.recv_timeout(d) { ... } }` a bug once producers finish?
    After the last `Sender` drops, `recv_timeout` returns `Err(RecvTimeoutError::Disconnected)` immediately on every call, so the loop spins at full speed and never exits. Match the two errors separately: `Timeout` means wait again or flush, `Disconnected` means break.
  • The consumer stage is gone and a producer gets `Err(SendError(frame))`. What should it do?
    Treat it as shutdown: stop producing, optionally log or recycle the returned `frame`, and return so its own handles drop and upstream stages see disconnection in turn. Unwrapping turns one dead stage into a panic in every producer.

  • Thinks the receive loop ends when the worker threads have finished.
  • Believes only the original Sender returned by channel() keeps the channel open.
  • Expects queued messages to be discarded once the last Sender drops.
  • Treats an Ok from send as proof that the message was received.
  • Handles recv_timeout's Disconnected the same way as Timeout.