Skip to content

Fix WebGPU static destruction crash by heap-allocating contexts map#27470

Merged
fs-eire merged 2 commits intomainfrom
fix/webgpu-static-destruction-crash
Feb 27, 2026
Merged

Fix WebGPU static destruction crash by heap-allocating contexts map#27470
fs-eire merged 2 commits intomainfrom
fix/webgpu-static-destruction-crash

Conversation

@fs-eire
Copy link
Copy Markdown
Contributor

@fs-eire fs-eire commented Feb 27, 2026

Heap-allocate WebGpuContextFactory::contexts_ to avoid crashes during static destruction when dependent DLLs (e.g. dxcompiler.dll) have already been unloaded. Cleanup() explicitly deletes the map; on abnormal termination it safely leaks.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses a shutdown-time crash in the WebGPU EP by avoiding static destruction of the global contexts registry (which can run after dependent DLLs like dxcompiler.dll are already unloaded).

Changes:

  • Replaces WebGpuContextFactory::contexts_ with a heap-allocated std::unordered_map pointer to prevent destructor execution during process static teardown.
  • Adds lazy allocation of the contexts map on first use, and explicitly deletes it in WebGpuContextFactory::Cleanup().
  • Updates context lookup/release code paths to handle contexts_ == nullptr with explicit ORT_ENFORCE messages.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
onnxruntime/core/providers/webgpu/webgpu_context.h Changes the static contexts registry to a pointer and documents the shutdown rationale.
onnxruntime/core/providers/webgpu/webgpu_context.cc Implements lazy allocation, null-guarded access, and explicit deletion/reset in Cleanup().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread onnxruntime/core/providers/webgpu/webgpu_context.h Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread onnxruntime/core/providers/webgpu/webgpu_context.h
@fs-eire fs-eire merged commit 7f905e0 into main Feb 27, 2026
92 of 95 checks passed
@fs-eire fs-eire deleted the fix/webgpu-static-destruction-crash branch February 27, 2026 19:12
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.

4 participants