Fix BaseException escape from host callback trampoline (#336)#337
Merged
alexcrichton merged 1 commit intobytecodealliance:mainfrom May 7, 2026
Merged
Conversation
…nce#336) The host-callback trampoline in `wasmtime/_func.py` caught `Exception`, which let `BaseException` subclasses (`KeyboardInterrupt`, `SystemExit`, custom `BaseException` subclasses) escape into the Rust array_call trampoline with an undefined `c_void_p` return value, causing a libmalloc SIGABRT inside `HostFunc::array_call_trampoline`. Broadens the catch to `BaseException`. The existing `LAST_EXCEPTION` / `Trap("python exception")` machinery handles propagation back to the Python caller unchanged. Adds regression tests for `KeyboardInterrupt`, `SystemExit`, and a custom `BaseException` subclass raised from host callbacks. Fixes bytecodealliance#336
Member
|
Looks good to me, thanks! |
This was referenced May 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #336.
The host-callback trampoline in
wasmtime/_func.pycaughtExceptionrather thanBaseException, soBaseExceptionsubclasses (KeyboardInterrupt,SystemExit, customBaseExceptionsubclasses) escaped the handler and caused the Python process to SIGABRT inside Rust'sHostFunc::array_call_trampoline(libmalloc "pointer being freed was not allocated") rather than propagating to the Python caller.The fix broadens the catch to
BaseException. The existingLAST_EXCEPTION/Trap("python exception")machinery already handles re-raising on the Python side, so no other code changes are needed.This is Option A from the issue — the minimum diff that closes the abort. Today, the original Python exception is re-raised at the call boundary via
maybe_raise_last_exn, so after this fixKeyboardInterruptfrom a host callback comes back asKeyboardInterruptand^Cfeels like^C. Option B (catchBaseExceptionbut explicitly preserveKeyboardInterrupt/SystemExitsemantics in some other way, or re-shape propagation) would be a behavioural decision; happy to send a follow-up PR if you'd prefer something different here. Flagging the choice for maintainer preference.Test plan
uv run pytest)test_host_base_exceptioncoversKeyboardInterrupt,SystemExit, and a customBaseExceptionsubclass from host callbacksFatal Python error: Aborted) before the fix, proving the test exercises the bugNotes
LAST_EXCEPTION: Optional[Exception]is now technically narrower than what we may store. Project mypy settings don't flag it; left untouched to keep the diff minimal, but happy to widen it if preferred.