Skip to content

windows: fix vectored exception handler lifecycle for repeated thread env init/destroy#4842

Open
kogum4 wants to merge 2 commits intobytecodealliance:mainfrom
kogum4:fix/windows-veh-lifecycle
Open

windows: fix vectored exception handler lifecycle for repeated thread env init/destroy#4842
kogum4 wants to merge 2 commits intobytecodealliance:mainfrom
kogum4:fix/windows-veh-lifecycle

Conversation

@kogum4
Copy link

@kogum4 kogum4 commented Feb 24, 2026

Summary

This PR fixes Windows VEH lifecycle handling for repeated runtime/thread-env init-destroy cycles.

On Windows host integrations that repeatedly unload/reload WASM runtime users, stale VEH state could remain and later crash in ntdll exception dispatch.

Reproduction (before)

On Windows, in a host-like lifecycle that repeatedly creates/destroys runtime usage contexts (including thread env init/destroy churn), intermittent crashes were observed in exception dispatch (ntdll, access violation path) after unload/reload cycles.

Changes

File:

  • core/iwasm/common/wasm_runtime_common.c

What changed:

  • add Windows-only VEH ref count
  • guard VEH lifecycle updates with a mutex
  • register VEH only when ref count transitions 0 -> 1
  • remove VEH only when ref count transitions 1 -> 0
  • if RemoveVectoredExceptionHandler fails, keep retry-able ownership state instead of dropping handler state

Behavior change

  • Before: VEH lifecycle could become inconsistent under repeated init/destroy cycles, leading to intermittent post-unload crash behavior.
  • After: VEH registration/removal is reference-counted and synchronized, with robust failure handling on removal.

Validation

  • rebuilt WAMR on Windows
  • rebuilt downstream integration
  • passed downstream C++ tests
  • reran host-style repeated unload/reload + open/close loop; crash no longer reproduced in the previously failing scenario

Notes

No new WAMR-internal test is included in this patch; validation was performed in the integration lifecycle where the issue reproduces.
If maintainers prefer, I can submit a follow-up PR with a WAMR-side regression test for this lifecycle pattern.

Copy link
Contributor

@TianlongLiang TianlongLiang left a comment

Choose a reason for hiding this comment

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

This PR overall LGTM with one minor comments

Since the code is already inside #ifdef BH_PLATFORM_WINDOWS,
the macro is always defined. Use NULL directly for clarity.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

2 participants