Skip to content

Commit 2e13f6b

Browse files
fs-eireCopilot
andcommitted
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 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent ccbc9cd commit 2e13f6b

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

third_party/emdawnwebgpu/pkg/webgpu/src/library_webgpu.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,9 @@ var LibraryWebGPU = {
909909
#if ASSERTIONS
910910
assert(deviceLostFutureId);
911911
#endif
912-
// Don't keepalive here, because this isn't guaranteed to ever happen.
912+
// Keep the runtime alive until device.lost resolves, to prevent
913+
// maybeExit() from triggering premature ABORT during callUserCallback.
914+
{{{ runtimeKeepalivePush() }}}
913915
WebGPU.Internals.futureInsert(deviceLostFutureId, device.lost.then((info) => {
914916
// If the runtime has exited, avoid calling callUserCallback as it
915917
// will print an error (e.g. if the device got freed during shutdown).
@@ -925,6 +927,7 @@ var LibraryWebGPU = {
925927
{{{ gpu.passAsPointer('messagePtr') }}});
926928
stackRestore(sp);
927929
});
930+
{{{ runtimeKeepalivePop() }}}
928931
}));
929932

930933
// Set up uncaptured error handlers.

0 commit comments

Comments
 (0)