Cancelling Async Rust

https://news.ycombinator.com/rss Hits: 20
Summary

This is an edited, written version of my RustConf 2025 talk about cancellations in async Rust. Like the written version of my RustConf 2023 talk, I’ve tried to retain the feel of a talk while making it readable as a standalone blog entry. Some links:VIDEOIntroduction#Let’s start with a simple example – you decide to read from a channel in a loop and gather a bunch of messages:loop { match rx.recv().await { Ok(msg) => process(msg), Err(_) => return, } } All good, nothing wrong with this, but you realize sometimes the channel is empty for long periods of time, so you add a timeout and print a message:loop { match timeout(Duration::from_secs(5), rx.recv()).await { Ok(Ok(msg)) => process(msg), Ok(Err(_)) => return, Err(_) => println!("no messages for 5 seconds"), } } There’s nothing wrong with this code—it behaves as expected.Now you realize you need to write a bunch of messages out to a channel in a loop:loop { let msg = next_message(); match tx.send(msg).await { Ok(_) => println!("sent successfully"), Err(_) => return, } } But sometimes the channel gets too full and blocks, so you add a timeout and print a message:loop { let msg = next_message(); match timeout(Duration::from_secs(5), tx.send(msg)).await { Ok(Ok(_)) => println!("sent successfully"), Ok(Err(_)) => return, Err(_) => println!("no space for 5 seconds"), } } It turns out that this code is often incorrect, because not all messages make their way to the channel.Hi, I’m Rain, and this post is about cancelling async Rust. This post is split into three parts:What is cancellation? It’s an extremely powerful part of async Rust but also one that is very hard to reason thoroughly about.Analyzing cancellations: Going deep into their mechanics and providing some helpful ways to think about them.What can be done? Solutions, including practical guidance, and real bugs we’ve found and fixed in production codebases.Before we begin, I want to lay my cards on the table – I really love async Rust!Me speaking at RustConf 2023...

First seen: 2025-10-03 17:53

Last seen: 2025-10-04 12:57