From 9b3af2a42c96da773e889432a68fe3556ba6ddac Mon Sep 17 00:00:00 2001 From: kogum4 Date: Wed, 25 Feb 2026 01:07:10 +0900 Subject: [PATCH 1/2] Fix Windows VEH registration/removal lifecycle --- core/iwasm/common/wasm_runtime_common.c | 58 +++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 3dbe496950..08443f6230 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -415,19 +415,49 @@ runtime_exception_handler(EXCEPTION_POINTERS *exce_info) } #endif /* end of BH_PLATFORM_WINDOWS */ +#ifdef BH_PLATFORM_WINDOWS +static PVOID runtime_exception_handler_handle = NULL; +static int32 runtime_exception_handler_ref_count = 0; +#if defined(OS_THREAD_MUTEX_INITIALIZER) +static korp_mutex runtime_exception_handler_lock = OS_THREAD_MUTEX_INITIALIZER; +#endif +#endif + static bool runtime_signal_init() { #ifndef BH_PLATFORM_WINDOWS return os_thread_signal_init(runtime_signal_handler) == 0 ? true : false; #else - if (os_thread_signal_init() != 0) +#if defined(OS_THREAD_MUTEX_INITIALIZER) + os_mutex_lock(&runtime_exception_handler_lock); +#endif + + if (os_thread_signal_init() != 0) { +#if defined(OS_THREAD_MUTEX_INITIALIZER) + os_mutex_unlock(&runtime_exception_handler_lock); +#endif return false; + } + + if (runtime_exception_handler_ref_count == 0) { + runtime_exception_handler_handle = + AddVectoredExceptionHandler(1, runtime_exception_handler); + } - if (!AddVectoredExceptionHandler(1, runtime_exception_handler)) { + if (!runtime_exception_handler_handle) { os_thread_signal_destroy(); +#if defined(OS_THREAD_MUTEX_INITIALIZER) + os_mutex_unlock(&runtime_exception_handler_lock); +#endif return false; } + + runtime_exception_handler_ref_count++; + +#if defined(OS_THREAD_MUTEX_INITIALIZER) + os_mutex_unlock(&runtime_exception_handler_lock); +#endif #endif return true; } @@ -436,7 +466,29 @@ static void runtime_signal_destroy() { #ifdef BH_PLATFORM_WINDOWS - RemoveVectoredExceptionHandler(runtime_exception_handler); +#if defined(OS_THREAD_MUTEX_INITIALIZER) + os_mutex_lock(&runtime_exception_handler_lock); +#endif + + if (runtime_exception_handler_ref_count > 0) { + runtime_exception_handler_ref_count--; + } + + if (runtime_exception_handler_ref_count == 0 + && runtime_exception_handler_handle) { + if (RemoveVectoredExceptionHandler(runtime_exception_handler_handle)) { + runtime_exception_handler_handle = NULL; + } + else { + /* Keep the handle so future init/destroy cycles can retry remove. + * Clearing it here may leave a live callback registered forever. */ + runtime_exception_handler_ref_count = 1; + } + } + +#if defined(OS_THREAD_MUTEX_INITIALIZER) + os_mutex_unlock(&runtime_exception_handler_lock); +#endif #endif os_thread_signal_destroy(); } From 72810b88992503aa18fe364ac99136b57d13e6e6 Mon Sep 17 00:00:00 2001 From: kogum4 Date: Thu, 26 Feb 2026 01:10:22 +0900 Subject: [PATCH 2/2] Remove redundant OS_THREAD_MUTEX_INITIALIZER guards 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 --- core/iwasm/common/wasm_runtime_common.c | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 08443f6230..d0bd0b35ad 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -418,9 +418,7 @@ runtime_exception_handler(EXCEPTION_POINTERS *exce_info) #ifdef BH_PLATFORM_WINDOWS static PVOID runtime_exception_handler_handle = NULL; static int32 runtime_exception_handler_ref_count = 0; -#if defined(OS_THREAD_MUTEX_INITIALIZER) -static korp_mutex runtime_exception_handler_lock = OS_THREAD_MUTEX_INITIALIZER; -#endif +static korp_mutex runtime_exception_handler_lock = NULL; #endif static bool @@ -429,14 +427,10 @@ runtime_signal_init() #ifndef BH_PLATFORM_WINDOWS return os_thread_signal_init(runtime_signal_handler) == 0 ? true : false; #else -#if defined(OS_THREAD_MUTEX_INITIALIZER) os_mutex_lock(&runtime_exception_handler_lock); -#endif if (os_thread_signal_init() != 0) { -#if defined(OS_THREAD_MUTEX_INITIALIZER) os_mutex_unlock(&runtime_exception_handler_lock); -#endif return false; } @@ -447,17 +441,13 @@ runtime_signal_init() if (!runtime_exception_handler_handle) { os_thread_signal_destroy(); -#if defined(OS_THREAD_MUTEX_INITIALIZER) os_mutex_unlock(&runtime_exception_handler_lock); -#endif return false; } runtime_exception_handler_ref_count++; -#if defined(OS_THREAD_MUTEX_INITIALIZER) os_mutex_unlock(&runtime_exception_handler_lock); -#endif #endif return true; } @@ -466,9 +456,7 @@ static void runtime_signal_destroy() { #ifdef BH_PLATFORM_WINDOWS -#if defined(OS_THREAD_MUTEX_INITIALIZER) os_mutex_lock(&runtime_exception_handler_lock); -#endif if (runtime_exception_handler_ref_count > 0) { runtime_exception_handler_ref_count--; @@ -486,9 +474,7 @@ runtime_signal_destroy() } } -#if defined(OS_THREAD_MUTEX_INITIALIZER) os_mutex_unlock(&runtime_exception_handler_lock); -#endif #endif os_thread_signal_destroy(); }