Hasty Briefsbeta

Cancelling Async Rust

4 hours ago
  • #tokio
  • #async-rust
  • #cancellation
  • Cancellation in async Rust is powerful but can lead to bugs if not handled carefully.
  • Futures in Rust are passive and only execute when awaited or polled, unlike in other languages.
  • Cancellation occurs when a future is dropped, not awaited, or not polled further.
  • Cancel safety refers to a future's ability to be cancelled without side effects.
  • Cancel correctness is a global property ensuring system correctness despite cancellations.
  • Common cancellation patterns include forgetting to await, using `try_join`, and `select` macros.
  • Solutions to mitigate cancellation issues include making futures cancel-safe, avoiding cancellations, and using tasks.
  • Tokio mutexes and certain APIs like `write_all` are prone to cancel correctness issues.
  • Systematic solutions like async drop and linear types are proposed but face implementation challenges.
  • Recommendations include avoiding Tokio mutexes, rewriting APIs for cancel safety, and ensuring cancel-unsafe futures complete.