What are the drop order guarantees for local and captured variables in async functions/blocks?

I'm trying to understand the exact drop order guarantees for variables in async functions and async blocks. Specifically, I'm confused about how two different categories of variables behave:

  1. Captured variables (including async fn parameters captured by the implicit async move block).
  2. Local variables declared inside the async body (especially those held across .await points).

Context & References

1. Captured Variables:
According to the Rust Reference on Async Blocks:

Executing an async block is similar to executing a closure expression: its immediate effect is to produce and return an anonymous type.

And for closures, the Rust Reference on Closure Types states:

The variables that a closure captures by move are dropped in an unspecified order.

Since an async fn parameters are captured by the desugared async move block, this suggests their drop order might be unspecified.

2. Local Variables inside the async body:
In standard synchronous functions, local variables are dropped in reverse declaration order. However, inside an async block, local variables that live across .await points are compiled into fields of the underlying generator/Future state machine struct.

Questions

  1. Captured Variables / Function Parameters: Is the drop order of captured variables (and async fn parameters) strictly unspecified, just like closures?
  2. Local Variables declared inside: Are local variables inside an async block still strictly guaranteed to drop in reverse declaration order?
  3. Interactions: What is the relative drop order between local variables and captured variables when a Future is dropped? (e.g., are locals dropped before captures?)

Code Example

Consider this scenario:

struct Guard(&'static str);
impl Drop for Guard {
    fn drop(&mut self) {
        println!("Dropped: {}", self.0);
    }
}

async fn test_drop_order(param1: Guard, param2: Guard) {
    let local1 = Guard("local1");
    
    let local2 = Guard("local2");
}

If the Future returned by test_drop_order is dropped:

  • Is local2 guaranteed to drop before local1?
  • Is the drop order between param1 and param2 unspecified?

In your example code, local2 is dropped first, then local1 (reverse order)

The captured variables by async move are dropped in order inside their async scope. If one of them is passed to yet another async scope (spawned async), it becomes only in order in the new async scope

For illustration

let a
let b

async move {
    a
    b
}

a and b are dropped in order (default order is reverse order) inside this async block scope. If they are passed by normal reference or by value to new async block and then you .await the async block inside the old async block, it is still dropped in the default order. Like

async fn run(b: &b) {

}

let a
let b

async move {
    a
    run(&b).await
}

Because the async move need to wait run to finish, so a will never be dropped first because a is dropped at the end of the async move, but the assync move needs to wait async run to meet its end. So the lifetime of b is the duration of async fn run, where the lifetime of a is the duration of async fn run + the duration of the async move

The out of order starts when you use concurrent async by spawning it. Like

async fn run(a: a) {
    a
}

let a
let b

async move {
    
    tokio::spawn(async move {
        run(a).await
    })

    b
}

Now a is out of the drop order of the old async scope, it can be dropped earlier or longer than b depending on the duration of the spawned async and the old async, which one finish faster. But a is dropped in order in its new async scope (the spawned async)

To put it simple : mixing sync and async await does not make the drop become out of order (because you will have to await anyway), so as long as you call .await the drop is in default order (the order in the place where await is called). Only spawning async that make the drop become out of order but only out of the order from the old block, it is still in default order inside its own new async block (the spawned async)

You seems not to explain how order the caputred parameters in my example are dropped. Anyway, I didn't find the relevant documentation says these points. Could you cite the relevant wording here?

I answered above, the order is like normal sync code, no different because you don't spawn parallel async. The order in sync code is LIFO (Last In First Out), so your async code is also like that

I didn't have article because I didn't know it by reading an article. But by experiment, open Rust playground, import Tokio, write struct and impl Drop, put println in the impl Drop, write async await code, put tokio::time::sleep to simulate different execution durations, observe how is the order of the println messages

However, the Rust reference says the async block captures the environments, which behaves as what closure does, the drop order for captured environments are unspecified. IMO, the parameters should be caputured by the async block that is desugared for the async function.

Do you mean this?

That is just saying async captures behave like clossure capture. So if you want to understand async capture, just remember or take a look at clossure capture. It does not talk about drop order. But the drop order as long as you don't spawn it to be concurrent, it is exactly like the drop order in clossure, it is also exactly like the drop order in sync Rust code and sync function

They're looking for guaranteed behaviour or an explicit declaration that it's not specified. Closure drop order is explicitly unspecified to date (as they cited).


If you can't find an RFC, FCP, or at least documentation, assume there is no guarantee. (I'm unaware of any offhand but async isn't my forte.) Those are Rust's means of providing guarantees.

AFAIK there's no difference between no guarantee as per the last paragraph, and "explicitly unspecified", aside from a courtesy heads-up. I.e., it may (or may not) be specified eventually.

I cited the relevant wording in my question

The variables that a closure captures by move are dropped in an unspecified order.

If async block shares the same capture mode with the closure, the parameters captured by async block will be drop in an unspecified order, right?

My bad, I didn't read the 2nd link. Just read the first link and then write the code :v in my code show they are in normal, but maybe it is because the variable types are same. I did find a PR, but it was a fix for async fn parameter to make the drop order match the sync version. Now that I read that the clossure is not specified yet, I will try different types first and run many times to see the order. In clossure it is because struct fields reorder, but async uses enum, so I will check first

That is more like because under the hood, clossure is dessugared to struct + impl block. And compiler can reorder the struct fields for optimization? But async uses enum

So, do you mean the parameters of the async function that will be captured by the desugarred async block in a move capture mode will be dropped in an unspecified order?

I mean I am unaware of any guarantee or explicit documentation, be it "something definite", "unspecified but same as closures", "unspecified and not necessarily the same as closures" (the sane default position), or anything else. (But I haven't deep-dived.)

I.e.: Sorry but I'm no help for the OP, and I only replied to try to clarify what I think you're after.

Brain wave: you could put all your captures in a struct with a Drop implementation and then AFAIU they'd have to obey struct field drop order.