Skip to content

Async-lifted exports trap with "borrow handles still remain at the end of the call" #255

Closed

Description

@FrancoisLDaigneault

Versions: componentize-py 0.25.1, and main at 704719e (same code). Host: Wasmtime 43 or newer. The check arrived in bytecodealliance/wasmtime#12550, first released in 43.0.0. main already builds against Wasmtime 48.

What happens. An async func export that takes a borrow<T> of an imported resource traps as soon as the Python coroutine returns:

error while executing at wasm backtrace:
    ... componentize_py_runtime::async_::__pyfunction_call_task_return
    ... <unknown>!task.return invoke-event
    ...: borrow handles still remain at the end of the call

Cause. In runtime/src/lib.rs, async_::call_task_return (v0.25.1 lines 486-504) calls task.return before it releases the borrows:

let mut call = MyCall::new(results);
func.call_task_return(&mut call);          // task.return
if borrows != 0 {
    release_borrows(py, ...);              // drops the borrow handles, too late
}

The synchronous path (export_call_, line 1302) releases the borrows before it returns, which is the right order.

Why the guest's order is the one that's wrong. The Canonical ABI reference (design/mvp/canonical-abi/definitions.py, Task.return_) runs trap_if(self.num_borrows > 0) before it resolves the task. Concurrency.md ("Borrows") says: "If a callee task attempts to return when its num_borrows is greater than zero, the callee traps." Wasmtime enforces this in concurrent.rs task_complete → validate_scope_exit → vm/component/resources.rs. Wasmtime 42 and earlier didn't check at task.return. That's probably why #182 passed its tests and the order went unnoticed.

Fix. Release the borrows first:

-        let mut call = MyCall::new(results);
-        func.call_task_return(&mut call);
+        // `task.return` traps while any borrow lent to this task is still
+        // outstanding, so drop them first.
         if borrows != 0 {
             release_borrows(py, *unsafe { Box::from_raw(borrows as *mut Vec<Borrow>) });
         }
+
+        let mut call = MyCall::new(results);
+        func.call_task_return(&mut call);

A result type can't hold a borrow<T>, so lowering the results after the release is safe. The synchronous path and the other release_borrows call site stay as they are.

Reproduction.

package example:borrow@0.1.0;
interface res { resource thing { constructor(); } }
world w {
    import res;
    export check: async func(t: borrow<res.thing>) -> u32;
}

Instantiate the component with Wasmtime 43 or newer, create a thing, and call check with a borrow of it. Before the fix the call traps with the message above; after it, the call returns. src/test/wit/tests.wit has no test of this shape: borrows appear only in synchronous exports, and the async exports take no resource parameters. A test next to test-resource-borrow-import would cover it.

A pull request with the fix and a regression test will follow.

Activity

  1. added a commit that references this issue on Oct 30, 2026
    4bbdeb9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions