Rust Has No Async Drop: Why You Can't Await in a Destructor
Here's a struct that looks entirely reasonable, and doesn't compile:
struct Session {
stream: tokio::net::TcpStream,
}
impl Drop for Session {
fn drop(&mut self) {
// send a goodbye frame before the socket closes
self.stream.write_all(b"BYE\r\n").await; // does not compile
}
}
The compiler stops you straight away: await is only allowed inside async functions and blocks. Drop::drop is defined as fn drop(&mut self), a perfectly ordinary synchronous method. There is no async fn drop variant to opt into, and there never has been.
If you go looking for one, you'll find a long-running design discussion in the async working group, still unresolved, about whether a sound version could exist at all. This is a real gap in the language, and nobody has found a satisfying way to close it.
Drop runs in places no executor can reach
The obvious fix looks like it should work: make drop return a future, and have the runtime poll it to completion before deallocating the value.
The trouble is that "have the runtime poll it" assumes a runtime is there and able to do so at the exact point the drop glue runs. Rust drops values in far too many places to guarantee that.
Here's where drop glue actually runs:
- At the end of a scope, which might be inside a synchronous function that never touches an executor at all.
- During panic unwinding, where the stack is being torn down to get out of trouble as fast as possible, and scheduling more work is the last thing anyone wants.
- When a
Futureis dropped mid-poll, for example becausetokio::select!chose a different branch, or aJoinHandlewas aborted. The value being dropped is often a local variable inside another future'spollimplementation. There is no waker context in which to register interest in a nested future's progress; you are already inside somebody else's poll.
That last case is the one that kills the idea. Calling an async fn doesn't run it to completion. It produces a Future that has to be polled, and polling can return Poll::Pending arbitrarily many times before it finishes.
If drop returned such a future, something would need to keep polling it, possibly across several wake-ups, after the value that owns the resource is already logically gone.
Actually, this bit is interesting: nothing in the ownership model has a slot for "this value is dead but still needs CPU time later". You'd need a second, hidden lifecycle bolted onto every value that implements it. That's a different and much harder feature than the one anyone was asking for.
Spawning or blocking from Drop both bite
Resources still need async cleanup, so the ecosystem has settled on a few well-worn patterns, all with sharp edges.
Spawn a detached task
The first is spawning a detached task from inside drop:
impl Drop for Session {
fn drop(&mut self) {
let stream = std::mem::replace(&mut self.stream, /* dummy */);
tokio::spawn(async move {
let mut stream = stream;
let _ = stream.write_all(b"BYE\r\n").await;
});
}
}
This compiles and often "works", but it quietly gives up two guarantees you probably wanted:
tokio::spawnrequires an active runtime context. Call this from a thread with no tokio runtime and it panics on the spot, which is a spectacularly unwelcome thing for a destructor to do.- A spawned task is detached: nothing waits for it. If the process exits, or the runtime shuts down, shortly after the drop runs, the goodbye frame may never be sent.
You've turned "always runs synchronously" into "runs eventually, maybe, if nothing more urgent happens first".
Block the current thread
The second workaround blocks the current thread to force progress:
impl Drop for Session {
fn drop(&mut self) {
if let Ok(handle) = tokio::runtime::Handle::try_current() {
let _ = tokio::task::block_in_place(|| {
handle.block_on(self.stream.write_all(b"BYE\r\n"))
});
}
}
}
This fixes the "never actually runs" problem and swaps in a worse one. Calling block_on from inside a runtime worker thread that's already driving other futures can deadlock or panic outright ("Cannot start a runtime from within a runtime").
That's why block_in_place exists: to work around it on the multi-threaded scheduler. It doesn't help at all on the current-thread scheduler, which has no other thread to hand the remaining work to.
An explicit async close() with Drop as a safety net
What the ecosystem has converged on is to stop pretending Drop can do the job. Make the async cleanup an explicit, consuming method that the caller is expected to invoke before the value goes out of scope, and demote Drop to a safety net that only complains if you forgot:
pub struct Session {
stream: Option<tokio::net::TcpStream>,
}
impl Session {
pub async fn close(mut self) -> std::io::Result<()> {
if let Some(mut stream) = self.stream.take() {
stream.write_all(b"BYE\r\n").await?;
stream.shutdown().await?;
}
Ok(())
}
}
impl Drop for Session {
fn drop(&mut self) {
if self.stream.is_some() {
tracing::warn!("Session dropped without calling close(); cleanup skipped");
}
}
}
Tokio itself uses the same shape. AsyncWriteExt::shutdown exists precisely because dropping an async writer, or a BufWriter wrapping one, does not flush it.
TLS streams need shutdown to send a proper close_notify rather than just severing the connection. If you never call it, the data you thought you wrote may not have gone anywhere. Drop can't rescue you at that point; all it can do is log that you forgot.
RAII's guarantee quietly stops at the network
Read honestly, this pattern means Rust never got async destructors. It got a convention where the "real" destructor is a method you have to remember to call, and the compiler-invoked Drop::drop is reduced to a diagnostic for catching the times you didn't.
Given the constraints, that's a reasonable trade-off. But it means the type system no longer enforces the one thing RAII exists to guarantee: that cleanup happens whether or not the caller remembers to ask for it.
For synchronous resources that guarantee still holds. For anything that needs a network round trip to close cleanly, it's back to being your problem.