Skip to content

Support explicit cancellation of Wasm execution#28

Draft
jtakakura wants to merge 8 commits intoclojurewasm:mainfrom
jtakakura:feat/execution-cancellation
Draft

Support explicit cancellation of Wasm execution#28
jtakakura wants to merge 8 commits intoclojurewasm:mainfrom
jtakakura:feat/execution-cancellation

Conversation

@jtakakura
Copy link
Copy Markdown
Contributor

Summary

This PR implements a mechanism to explicitly cancel a running WebAssembly instance from an external thread, as requested in #27.

Previously, execution could only be stopped via pre-set fuel limits or deadline timeouts. This change introduces a thread-safe cancelled flag in the Vm struct, allowing host applications to interrupt long-running or infinite loops from a different thread.

Proposed Changes

Core VM Implementation

  • Added cancelled: std.atomic.Value(bool) to the Vm struct for thread-safe concurrent access.
  • Implemented Vm.cancel() to set the flag and ensured Vm.reset() clears it at the start of each invocation.
  • Integrated the check into consumeInstructionBudget(). This allows the VM to stop execution at the next checkpoint (~every 1024 instructions).
  • Updated the JIT execution path (jitFuelCheckHelper) to honor the cancellation flag.

Public API

  • Zig API: Added WasmModule.cancel() as a high-level method to request cancellation.
  • C API: Added zwasm_module_cancel(zwasm_module_t *module) to include/zwasm.h and src/c_api.zig.
  • Errors: Added error.Canceled and updated the CLI to provide a descriptive error message when cancellation occurs.

Testing

  • Added unit tests in src/vm.zig that simulate a background thread canceling both interpreter and JIT-compiled loops.
  • Enhanced C API tests in test/c_api/test_ffi.c to verify that the zwasm_module_cancel symbol is correctly exported and functional in a multi-threaded C environment.

Related Issue

Closes #27

@jtakakura jtakakura force-pushed the feat/execution-cancellation branch 7 times, most recently from c643a26 to d5b5615 Compare April 11, 2026 00:06
@jtakakura jtakakura force-pushed the feat/execution-cancellation branch 2 times, most recently from ac45a2c to 1d2dee7 Compare April 12, 2026 01:17
@jtakakura jtakakura force-pushed the feat/execution-cancellation branch 2 times, most recently from a0323eb to 3438e30 Compare April 12, 2026 03:11
@jtakakura jtakakura force-pushed the feat/execution-cancellation branch from 3438e30 to 04e81c8 Compare April 12, 2026 03:19
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.

Support explicit cancellation of Wasm execution

1 participant