Skip to content

23 · Concurrency

Wait for resource cleanup

using async session = await open_session() installs a resource implementing core::cleanup::AsyncCleanup. Scope exit waits for close, including early returns. Nested resources close in reverse acquisition order, exactly once.

On this page

using async session = await open_session() installs a resource implementing core::cleanup::AsyncCleanup. Scope exit waits for close, including early returns. Nested resources close in reverse acquisition order, exactly once.

Complete example

Run this example locally with the installed CLI and host scheduler.

Wait for resource cleanup.

04-async-cleanup.zolo
// Returning from the inner scope still waits for both sessions to close.

use core::cleanup::AsyncCleanup

let mut close_order = 0
struct Session { id: int }

impl AsyncCleanup for Session {
    async fn close(self) {
        sleep 1ms
        close_order = close_order * 10 + self.id
    }
}

async fn open_session(id: int) -> Session = Session { id: id }

async fn send_batch() -> int {
    using async outer = await open_session(1) {
        using async inner = await open_session(2) {
            return 7
        }
    }
}

let sent = await send_batch()
assert(sent == 7)
assert(close_order == 21)
print("sent: {sent}, close order: {close_order}")

Requires the Zolo CLI/host — open in the playground or run locally.

Reference

Reference · Source program

zolo run examples/features/75-syntax-four-async/04-async-cleanup.zolo --no-cache

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

enespt-br