From 8e16c57eb39ea5d31139950154482e63b496a460 Mon Sep 17 00:00:00 2001 From: Yulong Wang <7679871+fs-eire@users.noreply.github.com> Date: Thu, 12 Mar 2026 23:00:54 -0700 Subject: [PATCH] emdawnwebgpu: Add runtimeKeepalive for device.lost handler The device.lost handler in emwgpuAdapterRequestDevice is wrapped in callUserCallback(), which calls maybeExit() after the callback executes. When runtimeKeepaliveCounter is 0 (common for --no-entry WASM modules), maybeExit() triggers _exit(0), setting ABORT=true. This causes all subsequent callUserCallback() invocations to silently drop their callbacks, breaking any WebGPU operations that follow (e.g., requestAdapter for a new session). Fix by adding runtimeKeepalivePush() before the device.lost promise setup, and runtimeKeepalivePop() after callUserCallback() returns inside the .then() handler. This ensures the runtime stays alive during the callback and maybeExit() sees counter >= 1. The pop is placed after callUserCallback (not before) because callUserCallback internally calls maybeExit() - if the pop happened first, the counter would be 0 during maybeExit, still triggering the premature exit. Bug: onnxruntime#27427 --- third_party/emdawnwebgpu/pkg/webgpu/src/library_webgpu.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/third_party/emdawnwebgpu/pkg/webgpu/src/library_webgpu.js b/third_party/emdawnwebgpu/pkg/webgpu/src/library_webgpu.js index 590d4cb2d2a..61b07f616b6 100644 --- a/third_party/emdawnwebgpu/pkg/webgpu/src/library_webgpu.js +++ b/third_party/emdawnwebgpu/pkg/webgpu/src/library_webgpu.js @@ -909,7 +909,9 @@ var LibraryWebGPU = { #if ASSERTIONS assert(deviceLostFutureId); #endif - // Don't keepalive here, because this isn't guaranteed to ever happen. + // Keep the runtime alive until device.lost resolves, to prevent + // maybeExit() from triggering premature ABORT during callUserCallback. + {{{ runtimeKeepalivePush() }}} WebGPU.Internals.futureInsert(deviceLostFutureId, device.lost.then((info) => { // If the runtime has exited, avoid calling callUserCallback as it // will print an error (e.g. if the device got freed during shutdown). @@ -925,6 +927,7 @@ var LibraryWebGPU = { {{{ gpu.passAsPointer('messagePtr') }}}); stackRestore(sp); }); + {{{ runtimeKeepalivePop() }}} })); // Set up uncaptured error handlers.