zacharywhitley opened PR #12512 from tegmentum:fix/global-code-registry-idempotent to bytecodealliance:main:
Summary
This PR makes
register_codeandunregister_codeoperations idempotent to prevent SIGABRT crashes caused by virtual address reuse in long-running processes.Fixes #12511
Problem
When creating and destroying many
EngineandModuleinstances, the OS can reuse virtual addresses before allArc<CodeMemory>references are fully released. This causes:
Duplicate registration: New engine gets same address as old engine whose Arc hasn't been deallocated yet
assert!(prev.is_none())fails → SIGABRTDouble unregistration: Old Arc finally deallocates after new engine re-registered at same address
assert!(code.is_some())fails → SIGABRTThis consistently crashes after ~350-400 engine/module creations in a single process.
Solution
Add a
BTreeSetto track registered addresses and check before performing operations:
register_code: Skip if address already registeredunregister_code: Skip if address not registeredThis makes both operations safe to call multiple times with the same address.
Changes
- Added
registered_addresses()function returning a staticRwLock<BTreeSet<usize>>- Modified
register_codeto check tracking set before inserting- Modified
unregister_codeto check tracking set before removing- Added debug logging (only in debug builds) for skipped operations
Testing
- Ran stress test creating 500+ engines/modules in a loop - passes without crash
- Full wasmtime test suite passes
- No performance regression (BTreeSet lookup is O(log n), same as existing BTreeMap)
Impact
This fix enables:
- JVM integrations (where
signals_based_traps(false)is required)- Long-running servers dynamically loading/unloading WASM modules
- Large test suites with many engine/module tests
- Any application creating 350+ engines/modules
Backward Compatibility
- No API changes
- No behavior changes for correctly behaving code
- Only affects edge case of address reuse with deferred Arc deallocation
zacharywhitley requested wasmtime-compiler-reviewers for a review on PR #12512.
zacharywhitley requested fitzgen for a review on PR #12512.
zacharywhitley requested wasmtime-wasi-reviewers for a review on PR #12512.
zacharywhitley requested wasmtime-core-reviewers for a review on PR #12512.
zacharywhitley requested wasmtime-default-reviewers for a review on PR #12512.
zacharywhitley updated PR #12512.
alexcrichton closed without merge PR #12512.
alexcrichton commented on PR #12512:
I don't believe that this fix is correct, so I'm going to close this in favor of continuing discussion on https://github.com/bytecodealliance/wasmtime/issues/12511.
Last updated: Feb 24 2026 at 04:36 UTC