expression
gRPC: stop and join request-owned background work
A request-owned Tokio task can outlive a dropped JoinHandle. If the application owns a cancellable async worker, explicitly stop and join it on cancellation. This controlled fixture demonstrates task cancellation, not Tonic disconnect detection. Prefer cooperative cleanup where necessary. Run in the accompanying Cargo project. Expected stdout (not executed): worker cancelled: true Common mistake: Dropping a JoinHandle and assuming that the spawned work stops immediately.
Formal Syntax & Structure
rust syntaxrust
worker.abort(); worker.awaitWorking Example
Example Snippetrust
#[tokio::main(flavor = "current_thread", start_paused = true)]async fn main() { let (started_tx, started_rx) = tokio::sync::oneshot::channel(); let worker = tokio::spawn(async move { started_tx.send(()).unwrap(); std::future::pending::<()>().await; }); started_rx.await.unwrap(); worker.abort(); println!("worker cancelled: {}", worker.await.unwrap_err().is_cancelled());}