Skip to content

Make dump reader caches safe for concurrent reads#1471

Open
leculver wants to merge 1 commit into
microsoft:mainfrom
leculver:split/b-dump-reader-cache-safety
Open

Make dump reader caches safe for concurrent reads#1471
leculver wants to merge 1 commit into
microsoft:mainfrom
leculver:split/b-dump-reader-cache-safety

Conversation

@leculver
Copy link
Copy Markdown
Contributor

@leculver leculver commented Jun 3, 2026

Summary

Makes the file-backed dump reader cache layer safe for concurrent readers without changing DAC behavior.

This PR fixes managed cache publication in the dump-reader/cache-entry layer:

  • publishes CoreDumpReader and ELF-derived lazy state with atomic/locked first-publisher patterns
  • makes Windows cached reader pages immutable once published
  • returns only ArrayPool buffers that lost publication and were never visible to readers
  • avoids returning published evicted buffers to ArrayPool while in-flight readers might still hold references

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 updates ClrMD’s file-backed dump reader caching and ELF/core-dump lazy state initialization to be safe under concurrent readers, primarily by switching from per-page locks to atomic “first publisher wins” patterns and ensuring published pages remain immutable/usable by in-flight readers.

Changes:

  • Reworked Windows segment cache entry page population to use Volatile.Read + Interlocked.CompareExchange publication, with a hook to discard unpublished pages (for returning unused pooled buffers).
  • Updated the ArrayPool-backed cache entry eviction behavior to avoid returning published/evicted buffers to ArrayPool while concurrent readers may still hold references.
  • Converted several Linux ELF/core-dump lazily initialized caches to lock-free or locked-first-publisher patterns (Volatile/Interlocked or explicit lock + publication flag), and marked the Linux coredump reader as thread-safe.
Show a summary per file
File Description
src/Microsoft.Diagnostics.Runtime/Windows/CacheEntryBase.cs Removes per-page RW locks and publishes cache pages via CAS; adds DiscardUnusedPage for safe cleanup of unpublished pages.
src/Microsoft.Diagnostics.Runtime/Windows/ArrayPoolBasedCacheEntry.cs Adapts page-in to possible concurrent execution; returns only unpublished loser buffers to ArrayPool, and makes eviction CAS-based without returning published buffers.
src/Microsoft.Diagnostics.Runtime/Linux/ElfFile.cs Makes DynamicSection and virtual-address reader initialization safe for concurrent callers using Volatile/CAS.
src/Microsoft.Diagnostics.Runtime/Linux/ElfCoreFile.cs Makes loaded-images publication lock-free, virtual address space init lock-free, and auxv table loading single-writer with a publication flag.
src/Microsoft.Diagnostics.Runtime/DataReaders/Core/CoreDumpReader.cs Makes coredump reader caches publish safely under concurrency; changes IsThreadSafe to true and replaces nullable bool cache with atomic tri-state int.

Copilot's findings

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

Comment on lines +64 to +66
bool result = SpecialDiagInfo.TryReadSpecialDiagInfo(this, out _);
Interlocked.CompareExchange(ref _isCreatedByDotNetRuntimeState, result ? 2 : 1, 0);
return result;
Fix thread-safety in the file-backed dump reader cache layer without changing DAC behavior. Publish CoreDumpReader and ELF-derived lazy state with atomic/locked first-publisher patterns, and make the Windows cached reader publish immutable pages safely for concurrent readers. Preserve ArrayPool safety by returning only unpublished CAS-loser buffers and leaving published evicted buffers to normal GC so in-flight readers cannot observe reused arrays.
@leculver leculver force-pushed the split/b-dump-reader-cache-safety branch from 544d124 to 35794a8 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