Skip to content

free the buffer maybe_pop_list allocates for bytes - #262

Merged
dicej merged 1 commit into
bytecodealliance:mainfrom
Kinfe123:fix/free-lowered-bytes
Nov 30, 2026
Merged

dicej merged 1 commit into
bytecodealliance:mainfrom
Kinfe123:fix/free-lowered-bytes

Conversation

@Kinfe123

Copy link
Copy Markdown
Contributor

Fixes #261.

maybe_pop_list copies a bytes value into a buffer from alloc::alloc, and wit-dylib lowers the list straight from that buffer without freeing it. So every bytes value a component passes to an import as a list<u8> stays allocated for the life of the instance.

This registers the buffer with defer_deallocate, like the runtime's other temporary buffers, so it's freed when the MyCall drops. A pending async import keeps its MyCall in Promise::ImportCall until the subtask finishes, and an export keeps its MyCall until post-return, so the buffer isn't freed while it can still be read.

Testing

The reproduction from #261 on Linux (ubuntu-latest, Wasmtime CLI 48.0.2). Both wheels were built with maturin build --release --features pyo3 at v0.25.1, one with this change applied (3c50de8, the same diff on v0.25.1):

Memory cap v0.25.1, bindings With this change, bindings os-write, both
64 MiB panics (exit 134) exits 0 exits 0
128 MiB panics (exit 134) exits 0 exits 0
256 MiB exits 0 exits 0 exits 0

On this branch, bash .github/workflows/lint.sh passes, and so does COMPONENTIZE_PY_TEST_COUNT=20 PROPTEST_MAX_SHRINK_ITERS=0 cargo test --release (127, 22 and 11 tests). Both ran in this workflow run.

I haven't added a regression test, since the existing tests don't watch an instance's memory. I'm happy to add one if you have a place in mind.

This doesn't add the null check for a failed alloc::alloc that #261 also describes. That's a separate change.

`maybe_pop_list` copies a `bytes` value into a buffer from `alloc::alloc`
and returns it, and wit-dylib lowers the list straight from that buffer.
wit-dylib only frees lists it lowers element by element, so this buffer
belongs to the interpreter, which never freed it: every `bytes` value a
component passed to an import as a `list<u8>` stayed allocated for the
life of the instance.

Register the buffer with `defer_deallocate`, like the runtime's other
temporary buffers, so it's freed when the `MyCall` drops. A pending async
import keeps its `MyCall` until the subtask finishes, and an export keeps
its `MyCall` until post-return.

Fixes bytecodealliance#261.

@dicej dicej left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@dicej
dicej merged commit 1854f32 into bytecodealliance:main Nov 30, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bytes passed to an import as list<u8> are never freed

2 participants