Skip to content

Fix managed cache publication races#1472

Open
leculver wants to merge 1 commit into
microsoft:mainfrom
leculver:split/c-managed-cache-publication
Open

Fix managed cache publication races#1472
leculver wants to merge 1 commit into
microsoft:mainfrom
leculver:split/c-managed-cache-publication

Conversation

@leculver
Copy link
Copy Markdown
Contributor

@leculver leculver commented Jun 3, 2026

Summary

Fixes ClrMD-owned managed cache publication races that do not require DAC serialization.

This PR keeps the change focused on managed object/cache safety:

  • avoids torn struct publication for ClrDacType.GCDesc
  • deduplicates racing ClrTypeFactory type creation
  • publishes singleton/error-module/basic-type state atomically
  • uses reference equality for singleton type comparisons so equality checks do not accidentally dispatch into DAC-backed type/name work
  • fixes related racy lazy cache patterns in managed ClrMD state

Part of #420.

Validation

  • Built as part of the split branch verification: dotnet build src/Microsoft.Diagnostics.Runtime/Microsoft.Diagnostics.Runtime.csproj --framework net10.0 -- passed with 0 warnings and 0 errors.
  • This split branch was recombined with the other reduced branches and compared against lockfree-mmf-threadsafe; all non-stress paths matched the original combined branch.

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

This PR hardens ClrMD’s managed caches against publication races by making several lazy-initialization and singleton publication paths thread-safe without requiring additional DAC serialization.

Changes:

  • Deduplicates concurrent ClrType creation by using an atomic publish-or-discard pattern in ClrTypeFactory.GetOrCreateType.
  • Makes several lazy caches race-safe (GCDesc publication, basic type array publication/slot initialization, module type maps).
  • Avoids equality paths that can trigger expensive/racy DAC-backed lookups by preferring ReferenceEquals for heap singletons.
Show a summary per file
File Description
src/Microsoft.Diagnostics.Runtime/Implementation/ClrTypeFactory.cs Deduplicates racing type creation and hardens basic-type/module fallback publication.
src/Microsoft.Diagnostics.Runtime/Implementation/ClrDacType.cs Publishes GCDesc safely and switches singleton comparisons to reference equality.
src/Microsoft.Diagnostics.Runtime/ClrType.cs Adjusts ClrType.Equals to avoid DAC-backed work for MT==0 types (but currently introduces a regression; see comment).
src/Microsoft.Diagnostics.Runtime/ClrObject.cs Uses ReferenceEquals for ErrorType checks to avoid dispatching into ClrType.Equals.
src/Microsoft.Diagnostics.Runtime/ClrModule.cs Adds lock + volatile publication for typedef/typeref maps to avoid racy ??= initialization.

Copilot's findings

  • Files reviewed: 5/5 changed files
  • Comments generated: 2

Comment on lines +522 to +523
if (MethodTable == 0 || other.MethodTable == 0)
return false;
Comment on lines 148 to +157
lock (_types)
{
if (_types.TryGetValue(mt, out ClrType? winner))
{
ApplyDeferredComponentType(winner, obj);
return winner;
}

_types[mt] = result;
}
Make ClrMD-owned managed caches safe under concurrent inspection. Avoid torn struct publication for ClrDacType.GCDesc, deduplicate ClrTypeFactory races, publish singleton/error-module state atomically, and use reference equality for singleton type comparisons so equality checks do not accidentally dispatch into DAC-backed type/name work. This change is intentionally separate from DAC serialization; it fixes managed cache publication and identity semantics.
@leculver leculver force-pushed the split/c-managed-cache-publication branch from 15b8c1a to 1fc3a1d Compare June 3, 2026 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants