expression

gRPC: detect a dropped stream consumer locally

A stream producer connected to a bounded Tokio channel can observe receiver closure. In a real server, wire the response stream lifetime to the receiver and ensure any other spawned workers also stop. This model drops the receiver directly; it does not prove that a remote disconnect reaches the producer. Run in the accompanying Cargo project. Expected stdout (not executed): consumer gone Common mistake: Continuing to produce items indefinitely after the response consumer is gone.

Formal Syntax & Structure

rust syntaxrust
sender.closed().await

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread", start_paused = true)]async fn main() {    use tokio::sync::mpsc;    let (tx, rx) = mpsc::channel::<u64>(2);    let producer = tokio::spawn(async move {        tx.closed().await;        "consumer gone"    });    drop(rx);    println!("{}", producer.await.unwrap());}
Rust Reference