Fix deadlock between EventListenersLock and ArrayPoolEventSource static ctor - #131501
Merged
mdh1418 merged 2 commits intoJul 29, 2026
Merged
Conversation
…c ctor MetricsEventSource.ParseSpecs uses string interpolation, which calls SharedArrayPool<char>.Rent(). That method accesses ArrayPoolEventSource.Log, triggering ArrayPoolEventSource's static constructor on first use. Since DoCommand runs while holding EventListenersLock, if a concurrent thread is already running ArrayPoolEventSource's static constructor (which itself acquires EventListenersLock), a deadlock results. Fix: eagerly pre-initialize ArrayPoolEventSource in InitializeDefaultEventSources, following the identical pattern used to fix the FrameworkEventSource deadlock in dotnet#126591 (PR dotnet#126737). Fixes dotnet#119014 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @steveisok, @dotnet/area-system-diagnostics-tracing |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR proactively initializes System.Buffers.ArrayPoolEventSource during EventSource.InitializeDefaultEventSources() to avoid a lock-order deadlock that can occur when ArrayPoolEventSource type-initialization is triggered while EventListener.EventListenersLock is held (notably during EventSource command handling paths).
Changes:
- Pre-initialize
System.Buffers.ArrayPoolEventSource.Logalongside other early-initialized defaultEventSources. - Add an explanatory comment documenting the deadlock scenario and why early initialization prevents it.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
noahfalk
approved these changes
Jul 29, 2026
lateralusX
reviewed
Jul 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #119014
Starting dotnet-counters during .NET app initialization can deadlock two threads with a lock-order cycle:
• Thread A holds EventListenersLock (inside EventSource.DoCommand for MetricsEventSource ) and triggers the first-ever use of ArrayPool via string interpolation in ParseSpecs . This causes SharedArrayPool.Rent() to access ArrayPoolEventSource.Log , which needs ArrayPoolEventSource 's type-init lock — held by Thread B.
• Thread B is running ArrayPoolEventSource 's static constructor (holds the type-init lock) and is blocked waiting for EventListenersLock .
The fix pre-initializes ArrayPoolEventSource in InitializeDefaultEventSources() , following the identical pattern already used to fix the analogous FrameworkEventSource deadlock in #126591 (PR #126737 ). After pre-initialization, ArrayPoolEventSource.Log is never uninitialized when Rent() accesses it, so no type-init lock is ever needed — the cycle cannot form.