Skip to content

perf(api): opt in to host interrupt registration - #7178

Open
syrusakbary wants to merge 3 commits into
mainfrom
codex/host-interrupt-wrapper
Open

syrusakbary wants to merge 3 commits into
mainfrom
codex/host-interrupt-wrapper

Conversation

@syrusakbary

@syrusakbary syrusakbary commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Description

Add InterruptCallWrapper for synchronous native calls. Like the asyncify wrapper from #7151, it uses the existing CallWrapper API and composes with another wrapper. Acquiring an Interrupter gives the caller a signal handle. The wrapper enables interrupt registration for each invocation, including retries made by an inner wrapper.

let interrupter = store.interrupter();
let mut wrapper = InterruptCallWrapper::new(interrupter.clone());
function.call_wrapped(&mut store, &mut wrapper, &params)?;

Keep the wrapper, interrupter, registry, SIGUSR1 handler, and deferred host-return check in lib/api/src/interrupt.rs. The native host trampolines call InterruptCallWrapper::finish_host_invocation before returning to Wasm. A pending interrupt traps before guest execution resumes. Plain synchronous calls bypass interrupt registration and their host imports skip the registry lookup. The VM exposes an opaque invocation token and generic signal-to-trap recovery. This keeps a signal for an outer store from trapping a nested call into a different store.

Module start runs through Instance::new, outside the function wrapper, and is now plain. The async runtime and tests use the upstream waker and polling behavior. The existing async coroutine enters the native function through a private entry point to retain its interrupt registration while suspended.

Synchronous benchmarks

Seven interleaved release runs on an Apple M5 Max (arm64), macOS 27.0, and Rust 1.99.1, using Singlepass and experimental-host-interrupt in both builds. The baseline is 9529d5b2883, which includes #7165. Times are medians. Export lookup and wrapper construction were outside the timed loop. The baseline registers interrupts on ordinary entry. The updated interruptible case uses call_wrapped with a prebuilt InterruptCallWrapper. The callback loop runs 100,000 host imports per guest entry and reports time per callback.

Case Before After Latency change
Plain typed constant 70.21 ns 29.19 ns −58.4%
Plain typed add 73.60 ns 30.17 ns −59.0%
Plain dynamic add 196.82 ns 144.98 ns −26.3%
Plain typed call with one host import 85.41 ns 41.61 ns −51.3%
Caller-wrapped typed add 88.64 ns 43.42 ns −51.0%
Interruptible typed add 73.03 ns 97.89 ns +34.0%
Plain host callback in a loop 16.36 ns 16.20 ns −0.9%
Interruptible host callback in a loop 16.05 ns 17.54 ns +9.3%

The feature-disabled typed-add control was 28.25 ns before and 29.17 ns after. All five non-interruptible entry cases improved in all seven paired runs, and the interruptible typed add slowed in all seven. The callback loop timing ranges overlapped.

Verification

  • Wasmer API and VM test suites with experimental-host-interrupt and experimental-async. New coverage includes passive handles, wrapper composition and retries, wrong-store rejection, module start, nested stores, and deferred interruption before the guest resumes.
  • Workspace formatting, targeted Clippy for both crates, and Cranelift CLI build.
  • Workspace-wide make lint requires additional dependencies unavailable in the offline Cargo cache.

Committed by Codex (AI agent) on behalf of the user.
Copilot AI balanced review requested due to automatic review settings November 13, 2026 03:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

A critical nested-call interruption bug leaves suspended async imports without a cached interrupt flag or waker, preventing interruption from waking or terminating the call while the host future remains pending.

1 open issue
What changed in this PR

This PR makes native host interruption opt-in per store, reducing overhead for ordinary Wasm calls while integrating interruption with VM entry and async suspension.

Changes:

  • Enable interruption through Store::interrupter() and skip registration for plain calls.
  • Associate interrupt handling with the active Wasm trap context and preserve state across async suspension.
  • Add coverage for opt-in behavior, module starts, nested stores, waker replacement, and cancellation.
File Description
lib/​vm/​src/​trap/​traphandlers.rs Adds interruptible trap contexts and saved async trap state.
lib/​vm/​src/​trap/​mod.rs Adds a trampoline entry with optional store interruption.
lib/​vm/​src/​store.rs Tracks per-store interruption opt-in.
lib/​vm/​src/​interrupt_registry/​unix.rs Refactors registrations and signal-target tracking.
lib/​vm/​src/​interrupt_registry/​mod.rs Gives guards direct access to interrupt flags.
lib/​vm/​src/​instance/​mod.rs Applies store opt-in to module start execution.
lib/​api/​tests/​interrupt.rs Tests plain calls, module starts, and nested-store isolation.
lib/​api/​tests/​interrupt_async.rs Tests async opt-in, waker replacement, and cross-store cancellation.
lib/​api/​src/​entities/​store/​obj.rs Enables interruption when creating a store handle.
lib/​api/​src/​entities/​store/​mod.rs Documents the public opt-in behavior.
lib/​api/​src/​backend/​sys/​entities/​store/​mod.rs Clarifies low-level handles and gates async notifications.
lib/​api/​src/​backend/​sys/​entities/​module.rs Removes module-level interrupt registration.
lib/​api/​src/​backend/​sys/​entities/​function/​typed.rs Routes typed calls through optional interruptible entry.
lib/​api/​src/​backend/​sys/​entities/​function/​mod.rs Updates dynamic entry and host-return interrupt checks.
lib/​api/​src/​backend/​sys/​async_runtime.rs Makes interrupt bookkeeping conditional and preserves suspended trap state.

Review effort: Balanced

Comment on lines +404 to +408
if self.flag.is_none()
&& let Some(registration) = &self.waker
{
// The VM registration stays live on the suspended stack.
self.flag = interrupt_registry::interrupt_flag(registration.store);
Committed by Codex (AI agent) on behalf of the user.
Committed by Codex (AI agent) on behalf of the user.
@marxin

marxin commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

Add InterruptCallWrapper for synchronous native calls.

Interesting - based on the included benchmarks (#7152), with the #7165 change included, I couldn't see any performance difference between experimental-host-interrupt disabled and enabled meaning the performance overhead was negligible.

That said, I would like to see the mentioned benchmarks being included into the criterion infrastructure we've been using.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants