Repository navigation
Initialize out SafeHandles before publishing them - #1878
Merged
Merged
Conversation
Co-authored-by: Copilot App <[email protected]>
Jevan Saks (jevansaks)
marked this pull request as ready for review
November 9, 2026 22:44
Jevan Saks (jevansaks)
enabled auto-merge (squash)
November 9, 2026 22:45
Sergio Pedri (Sergio0694)
approved these changes
Nov 9, 2026
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.
Summary
Relates to #1877.
Generated friendly overloads currently assign a newly allocated safe handle to the caller's
outparameter before invoking native code, then read that parameter again forMarshal.InitHandle. When the caller passes a field, concurrent or reentrant code can clear or replace that field during the native call, causing initialization to target null or a different object.Keep the allocated safe handle in a private local, initialize that local after the native call, and only then assign it to the
outparameter. This also avoids exposing the uninitialized safe handle to premature disposal. Allocation still happens before acquiring the native resource, preserving the existing allocation-failure protection. The path for targets withoutMarshal.InitHandleis unchanged.Generated code before and after
For the
SubscribeServiceChangeNotificationsfriendly overload used in #1877, the relevant generated statements are shown below. The unchanged input-handle lifetime management, method signature, and surroundingtry/finallyare omitted; namespace qualifications and line wrapping are simplified for readability.Before
When the caller passes
out _dbSubscription,pSubscriptionaliases that field. Clearing or replacing_dbSubscriptionduring the native call changes the object passed toMarshal.InitHandle.After
Initialization now targets the private local regardless of changes to the caller's field. The
outparameter receives the safe handle only after initialization.Remaining limitation
This does not guarantee that a subscription handle is available when a native notification callback fires before registration completes. Callers must still coordinate callback processing, registration, and disposal; publishing an uninitialized safe handle early did not provide a usable handle during that window.
Validation
UseInitHandleApiWhenPossibletests: 6 passed.VerifySignaturecase forWlanOpenHandle: passed, including generated-code compilation.SubscribeServiceChangeNotificationswith concurrent clearing of the output field completed withoutNullReferenceException(the original generator reproduced the exception). An invalid manager handle ensured no native subscriptions were created.