Skip to content

Fix CLR->COM RCW ownership tracking on the slow path#126731

Open
AaronRobinsonMSFT wants to merge 2 commits intodotnet:mainfrom
AaronRobinsonMSFT:fix/126619-com-rcw-needsrelease
Open

Fix CLR->COM RCW ownership tracking on the slow path#126731
AaronRobinsonMSFT wants to merge 2 commits intodotnet:mainfrom
AaronRobinsonMSFT:fix/126619-com-rcw-needsrelease

Conversation

@AaronRobinsonMSFT
Copy link
Copy Markdown
Member

@AaronRobinsonMSFT AaronRobinsonMSFT commented Apr 9, 2026

Fixes #126619.

This was a regression introduced in #114609

When System.StubHelpers.GetCOMIPFromRCW() misses the fast path on a native-created thread, the slow path can create OLE TLS and still resolve the COM interface pointer from the RCW cache. Those cache hits are borrowed and should not be released by the stub cleanup path.

This change moves the pfNeedsRelease decision to the slow helper so it can distinguish between:

  • RCW cache hits after OLE TLS initialization
  • freshly acquired interface pointers from ComObject::GetComIPFromRCWThrowing

Testing:

Have the slow GetCOMIPFromRCW helper report whether the returned interface pointer actually requires cleanup, so RCW cache hits after OLE TLS initialization are not released as if they were freshly acquired.\n\nFixes dotnet#126619\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke
See info in area-owners.md if you want to be subscribed.

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

Fixes CLR→COM stub cleanup ownership tracking when StubHelpers.GetCOMIPFromRCW() falls back to the slow path on native-created threads, preventing erroneous Release calls for RCW-cache hits after OLE TLS initialization (issue #126619).

Changes:

  • Extend the QCall slow helper (StubHelpers_GetCOMIPFromRCWSlow) to return an explicit pfNeedsRelease flag.
  • Update the slow helper implementation to set pfNeedsRelease=true only for freshly acquired COM IPs (and keep it false for RCW-cache hits).
  • Update managed StubHelpers.GetCOMIPFromRCW to delegate pfNeedsRelease determination to the slow path.
Show a summary per file
File Description
src/coreclr/vm/stubhelpers.h Updates the slow-path QCall signature to include an explicit ownership/cleanup flag.
src/coreclr/vm/stubhelpers.cpp Implements ownership tracking: cache hits return borrowed pointers (no release), fresh acquisitions require release.
src/coreclr/System.Private.CoreLib/src/System/StubHelpers.cs Plumbs pfNeedsRelease through the slow path so cleanup behavior matches actual pointer ownership.

Copilot's findings

  • Files reviewed: 3/3 changed files
  • Comments generated: 1

Extend TrackMyLifetimeTesting with a simple Method() call and add a native-thread callback test that exercises the agile RCW path covered by dotnet#126619.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@AaronRobinsonMSFT
Copy link
Copy Markdown
Member Author

AaronRobinsonMSFT commented Apr 9, 2026

@elinor-fung @jkoritzinsky I've added a test here. It was a bit complicated and I wasted about an hour trying to reason why other tests were failing. Apparently Copilot missed the semantic meaning of IAgileObject which made the STA logic being used for the tests really flakey. I can't get too upset though, IAgileObject is a bit niche.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

3 participants