Fix GCHandle double-free race in WinHttpRequestState#125293
Fix GCHandle double-free race in WinHttpRequestState#125293EgorBo merged 3 commits intodotnet:mainfrom
Conversation
…dBlob - WinHttpRequestState.Dispose(): Replace volatile bool check-then-act with Interlocked.Exchange to eliminate TOCTOU race. Two code paths can reach Dispose (OnRequestHandleClosing callback and fallback disposal in StartRequest), and the non-atomic volatile bool allowed both threads to pass the guard simultaneously. - PinnedBlob.Dispose(): Add IsAllocated guard before Free() and remove misleading comment that claimed Free() mutation prevents double-free (it only prevents it on the same struct instance, not on copies). Co-authored-by: Copilot <[email protected]>
|
Tagging subscribers to this area: @karelz, @dotnet/ncl |
There was a problem hiding this comment.
Pull request overview
Addresses two potential double-free scenarios involving GCHandle.Free by making disposal idempotent under concurrency in WinHttpHandler, and by hardening a test utility’s Dispose implementation.
Changes:
- Replace non-atomic
volatile booldispose guard with anInterlocked.Exchange-based guard inWinHttpRequestState.Dispose. - Update
PinnedBlob.Dispose()(test utility) to only free theGCHandlewhen it’s allocated, and remove a misleading comment about copy-safety.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/System.Reflection.Metadata/tests/TestUtilities/PinnedBlob.cs | Makes PinnedBlob.Dispose() robust against default/uninitialized instances by guarding GCHandle.Free() with IsAllocated. |
| src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/WinHttpRequestState.cs | Prevents concurrent double-dispose/double-free by using an atomic dispose gate via Interlocked.Exchange. |
|
Was this hit anywhere? Or is it just based on code observation? Note that I have no qualms about making this change. |
@ManickaP I can just remove the volatile then. The problem that the current change looks very suspicius. Example: But it does look like |
ManickaP
left a comment
There was a problem hiding this comment.
Feel free to merge this, I'm not against this change. I was just making sure we don't have any serious, unexpected problems we don't know about.
|
/ba-g deadletters on iOS.Device |
volatile bool _disposeddoesn't make the following code atomic: