# E0098

Warning name: `cancellable_defer`

A `defer` or `errdefer` expression contains cancellable asynchronous code.
If cancellation triggers the cleanup, its asynchronous operations can be
cancelled immediately as well, preventing the cleanup from completing.

## Erroneous example

The examples use the experimental standard-library async runtime:

```{literalinclude} /sources/error_codes/0098_error/moon.pkg
:language: moonbit
```

```{literalinclude} /sources/error_codes/0098_error/top.mbt
:language: moonbit
```

## Suggestion

Protect required asynchronous cleanup from cancellation. With
`moonbitlang/core/async`, wrap it in `@async.protect_from_cancel`:

```{literalinclude} /sources/error_codes/0098_fixed/top.mbt
:language: moonbit
```

Synchronous cleanup does not need this wrapper. Keep protected cleanup bounded
so that cancellation does not wait indefinitely for it to finish.
