Skip to content

[release/8.0-staging] Don't call EnsureInstanceActive under StaticBoxInitLock #108736

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

Merged

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Oct 10, 2024

Fixes a customer-reported issue #108506 for .NET 8.0. It's not an issue for .NET 9.0 and .NET 10.0 since this code was reworked in #99183 (the change is too big to backport to 8.0). Thanks to @davidwrighton for investigation and the suggested fix.

Customer Impact

  • Customer reported
  • Found internally

The issue is a race condition leading to a dead-lock in JIT. The issue is related to Wpf assemblies, and in particular to the use of C++/CLI within WPF. Notably, we're in the process of running the module constructor to initialize the static variables on one assembly, and the field type is defined in a module with a module constructor, and on a different thread we are in the process of running that module constructor, and find ourselves needing to allocate memory for the static boxes. In this case I can see that we have a lock ordering violation, where we take two locks out of order.

The fix is to simply move pFieldMT->EnsureInstanceActive(); outside of the lock we take to assign boxedStaticHandle.

Regression

Testing

We don't have a repro (a race condition difficult to reproduce)

Risk

Low

Copy link
Contributor

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

@EgorBo
Copy link
Member Author

EgorBo commented Oct 10, 2024

/azp run runtime-coreclr outerloop

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@@ -4200,6 +4200,9 @@ void MethodTable::AllocateRegularStaticBox(FieldDesc* pField, Object** boxedStat
MethodTable* pFieldMT = pField->GetFieldTypeHandleThrowing().GetMethodTable();
bool hasFixedAddr = HasFixedAddressVTStatics();

// Activate any dependent modules if necessary
pFieldMT->EnsureInstanceActive();

// Taking a lock since we might come here from multiple threads/places
CrstHolder crst(GetAppDomain()->GetStaticBoxInitLock());
Copy link
Member Author

@EgorBo EgorBo Oct 10, 2024

Choose a reason for hiding this comment

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

NOTE: we can also just remove the lock completely here (to improve perf a bit) and assign the field via InterlockedCompareExchange pattern here, but that might lead to small memory leaks as GC won't collect a redundant allocation on NonGC heap in case of a race.

@carlossanlop
Copy link
Member

Friendly reminder that today 10/14 is Code Complete for the November Release. If this fix is intended to be included in that release, please make sure it's merged before 4pm PT. Otherwise, it will have to wait until next month.

Copy link
Member

@jeffschwMSFT jeffschwMSFT left a comment

Choose a reason for hiding this comment

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

lgtm. we will take for consideration in 8.0.x

@jeffschwMSFT jeffschwMSFT added the Servicing-consider Issue for next servicing release review label Jan 9, 2025
@jeffschwMSFT jeffschwMSFT added this to the 8.0.x milestone Jan 9, 2025
@jeffschwMSFT jeffschwMSFT added Servicing-approved Approved for servicing release and removed Servicing-consider Issue for next servicing release review labels Jan 14, 2025
@jeffschwMSFT jeffschwMSFT modified the milestones: 8.0.x, 8.0.14 Jan 14, 2025
@jeffschwMSFT
Copy link
Member

@EgorBo can you take a look at the CI failures? we can merge when ready

@EgorBo
Copy link
Member Author

EgorBo commented Feb 8, 2025

@jeffschwMSFT sorry for the delay, CI is green now - ready to merge

@jeffschwMSFT jeffschwMSFT merged commit 2dad258 into dotnet:release/8.0-staging Feb 8, 2025
115 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Mar 11, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-VM-coreclr Servicing-approved Approved for servicing release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants