WebGPU: make WebGpuContextFactory::Cleanup exception-safe and idempotent#29207
Draft
samuel100 wants to merge 1 commit into
Draft
WebGPU: make WebGpuContextFactory::Cleanup exception-safe and idempotent#29207samuel100 wants to merge 1 commit into
samuel100 wants to merge 1 commit into
Conversation
Detach the static contexts map and default instance before releasing them so the factory is left in a clean, reusable state even if Dawn's Metal teardown throws (an Objective-C NSException on macOS). Guard each release independently so a throw in one cannot leak the other or leave the statics dangling. Fixes the 'ReleaseEpFactory failed ... Unknown exception' teardown error and the per-register/unregister WGPUInstance leak. See microsoft#29206.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Makes
WebGpuContextFactory::Cleanup()exception-safe and idempotent.It currently does:
If
delete contexts_throws,contexts_is left non-null,default_instance_is never released, anddefault_instance_is left dangling. On macOS, tearing down Dawn's Metal device/instance can raise an Objective-CNSException, which propagates through C++ frames on arm64 and is caught by thecatch (...)in the WebGPU plugin EP'sReleaseEpFactory— surfacing as:This patch detaches the static state first (so the factory is left clean and reusable even if a destructor throws) and guards each release independently, so:
ERRORgoes away (the plugin'sReleaseEpFactoryreturns OK), andWGPUInstance/ DawnInstanceBaseis no longer leaked on every register→unregister cycle.Motivation and Context
Fixes #29206.
The current behavior is benign for single-shot processes (the library still unloads and the OS reclaims resources) but is a real cumulative leak for long-lived hosts that register/unregister EPs repeatedly, and the
ERRORlog is misleading. The plugin EP README already lists WebGPU cleanup under "Missing parts".A deeper follow-up could wrap the Metal teardown in
@try/@catchso Dawn doesn't throw at all, but the exception-safety fix here is correct and minimal on its own and matches the best-effort teardown already used inReleaseEpFactory.Note for reviewers