Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions onnxruntime/core/providers/webgpu/webgpu_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1075,14 +1075,31 @@
void WebGpuContextFactory::Cleanup() {
std::lock_guard<std::mutex> lock(mutex_);

if (contexts_ != nullptr) {
delete contexts_;
contexts_ = nullptr;
// Detach the static state first so the factory is left in a clean, reusable
// state even if a destructor throws during teardown. On macOS, releasing
// Dawn's Metal objects can raise an Objective-C NSException (caught here by
// `catch (...)`); without detaching first, a throw in `delete contexts_`
// would leak `default_instance_` and leave the statics dangling, breaking any
// subsequent re-initialization and leaking a WGPUInstance per
// register/unregister cycle. Guard each release independently so a throw in
// one cannot leak the other.
auto* contexts = contexts_;
contexts_ = nullptr;
WGPUInstance instance = default_instance_;
default_instance_ = nullptr;

try {

Check failure on line 1091 in onnxruntime/core/providers/webgpu/webgpu_context.cc

View workflow job for this annotation

GitHub Actions / webgpu_minimal_build_edge_build_x64_RelWithDebInfo

the following warning is treated as an error
delete contexts;
} catch (...) {
LOGS_DEFAULT(WARNING) << "Exception while destroying WebGPU contexts during teardown; ignoring.";
}

if (default_instance_ != nullptr) {
wgpuInstanceRelease(default_instance_);
default_instance_ = nullptr;
if (instance != nullptr) {
try {
wgpuInstanceRelease(instance);
} catch (...) {
LOGS_DEFAULT(WARNING) << "Exception while releasing WebGPU instance during teardown; ignoring.";
}
}
}

Expand Down
Loading