Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move ComWrappers AddRef to C/C++ #110762

Merged
merged 8 commits into from
Dec 18, 2024
Merged

Move ComWrappers AddRef to C/C++ #110762

merged 8 commits into from
Dec 18, 2024

Conversation

jkotas
Copy link
Member

@jkotas jkotas commented Dec 17, 2024

Xaml invokes AddRef while holding a lock that it also holds while a GC is in progress. Managed AddRef had to synchronize with the GC that caused intermittent deadlocks with the other thread holding Xaml's lock.

This change reverts the managed AddRef implementation to match .NET Native and CoreCLR.

Fixes #110747

Xaml invokes AddRef while holding a lock that it *also* holds while a GC is in progress. Managed AddRef had to synchronize with the GC that caused intermittent deadlocks with the other thread holding Xaml's lock.

This change reverts the managed AddRef implementation to match .NET Native and CoreCLR.

Fixes dotnet#110747
Co-authored-by: Aaron Robinson <arobins@microsoft.com>
Copy link
Contributor

@Sergio0694 Sergio0694 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!! 😄

Tested with the Microsoft Store and could not repro any hangs:

I then tried to use the Store for a while, and also did the same thing that triggered the hang last time (opening and closing it in quick succession a couple dozen times). I could not repro the issue and everything seemed to be working great 🎉

@jkotas jkotas marked this pull request as ready for review December 17, 2024 14:11
@jkotas jkotas requested a review from jkoritzinsky December 17, 2024 14:32
Comment on lines 1305 to 1312
#if false // Implemented in C/C++ to avoid GC transitions
[UnmanagedCallersOnly]
internal static unsafe uint IUnknown_AddRef(IntPtr pThis)
{
ManagedObjectWrapper* wrapper = ComInterfaceDispatch.ToManagedObjectWrapper((ComInterfaceDispatch*)pThis);
return wrapper->AddRef();
}
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like the #if false pattern. I'd rather we just include a comment on line 1160 explaining that we have it implemented in C/C++ to avoid GC transitions.

Copy link
Member

@AaronRobinsonMSFT AaronRobinsonMSFT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @jkoritzinsky's comment about the #if false style. Other than that, LGTM.

@jkotas jkotas merged commit d7e85d9 into dotnet:main Dec 18, 2024
88 checks passed
@jkotas jkotas deleted the issue-110747 branch December 18, 2024 15:22
@jkotas
Copy link
Member Author

jkotas commented Dec 18, 2024

/backport to release/9.0-staging

Copy link
Contributor

Started backporting to release/9.0-staging: https://github.com/dotnet/runtime/actions/runs/12396026201

Sergio0694 pushed a commit to Sergio0694/runtime that referenced this pull request Jan 7, 2025
* Move ComWrappers AddRef to C/C++

Xaml invokes AddRef while holding a lock that it *also* holds while a GC is in progress. Managed AddRef had to synchronize with the GC that caused intermittent deadlocks with the other thread holding Xaml's lock.

This change reverts the managed AddRef implementation to match .NET Native and CoreCLR.

Fixes dotnet#110747

Co-authored-by: Aaron Robinson <arobins@microsoft.com>
@github-actions github-actions bot locked and limited conversation to collaborators Jan 18, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hang during AddRef/Release calls from GC callbacks for IReferenceTracker support
4 participants