Cancellation and drop
Quick answer
Dropping an owned, unfinished future prevents further polling and drops its owned state. It does not undo external effects already performed by earlier polls.
Know what is being cancelled
Cancellation safety means an unfinished operation can be abandoned without violating the relevant application contract. A dropped future and a dropped Tokio JoinHandle are different: dropping the handle detaches the spawned task. Tokio abort requests cancellation of an async task; await its handle to observe completion or cancellation. Blocking tasks may not be abortable once running.
use std::{future::{Future, pending}, task::{Context, Poll, Waker}};struct Guard;impl Drop for Guard { fn drop(&mut self) { println!("cleanup"); } }fn main() { let mut f = Box::pin(async { let _guard = Guard; pending::<()>().await; }); let mut cx = Context::from_waker(Waker::noop()); assert_eq!(f.as_mut().poll(&mut cx), Poll::Pending); drop(f);}Expected stdout (not executed):
cleanupDropping the future drops the guard created during its first poll.
#[tokio::main(flavor = "current_thread")]async fn main() { let h = tokio::spawn(std::future::pending::<()>()); h.abort(); println!("cancelled: {}", h.await.unwrap_err().is_cancelled());}Manual local Rust/Cargo candidate; not executed. Expected behavior:
cancelled: trueThis task cannot finish normally, so awaiting the abort reports cancellation.
Common mistake
Do not assume Drop performs asynchronous flushing or reverses a database write.
Try it yourself
Explain why dropping h without abort would not stop the pending spawned task.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Cancellation and drop — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
An owned future is cancelled after writing part of a message. Which claim is valid?
Cancellation and drop — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::{future::{Future, pending}, task::{Context, Poll, Waker}}; struct Guard; impl Drop for Guard { fn drop(&mut self) { println!("cleanup"); } } fn main() { let mut f = Box::pin(async { let _guard = Guard; pending::<()>().await; }); let mut cx = Context::from_waker(Waker::noop()); assert_eq!(f.as_mut().poll(&mut cx), Poll::Pending); drop(f); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Cancellation and drop — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust #[tokio::main(flavor = "current_thread")] async fn main() { let h = tokio::spawn(std::future::pending::<()>()); h.abort(); println!("cancelled: {}", h.await.unwrap_err().is_cancelled()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.