Skip to content

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

Merged
AaronRobinsonMSFT merged 2 commits intodotnet:mainfrom
AaronRobinsonMSFT:fix/126619-com-rcw-needsrelease
Apr 10, 2026
Merged

Fix CLR->COM RCW ownership tracking on the slow path#126731
AaronRobinsonMSFT merged 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.

@AaronRobinsonMSFT AaronRobinsonMSFT merged commit 12d6cfa into dotnet:main Apr 10, 2026
115 of 117 checks passed
@github-project-automation github-project-automation bot moved this to Done in AppModel Apr 10, 2026
@AaronRobinsonMSFT AaronRobinsonMSFT deleted the fix/126619-com-rcw-needsrelease branch April 10, 2026 17:54
@AaronRobinsonMSFT
Copy link
Copy Markdown
Member Author

/backport to release/10.0

@github-actions
Copy link
Copy Markdown
Contributor

Started backporting to release/10.0 (link to workflow run)

agocke pushed a commit that referenced this pull request Apr 11, 2026
…126757)

Backport of #126731 to release/10.0

/cc @AaronRobinsonMSFT

## Customer Impact

- [x] Customer reported
- [ ] Found internally

This causes issues with COM object lifetime management. It is a real
issue with COM interop and will impact anyone using COM interop from an
unmanaged thread. User reported issue can be found at
#126619.

## Regression

- [x] Yes
- [ ] No

This was introduced in #114609.
That work was apart of the removal of `HELPER_METHOD_FRAME` completed in
.NET 10 - #95695.

## Testing

A test has been added to ensure this particular issue won't crop up
again and adds more coverage to built-in COM interop support.

## Risk

Low. This is addressing a real regression and putting back the logic to
what is was prior to work done around `HELPER_METHOD_FRAME` removal.

---------

Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

4 participants