Skip to content

[rel/18.0] Fix concurrent modification in MetricsCollection - #16742

Merged
Azat Mukhametshin (azat-msft) merged 3 commits into
microsoft:rel/18.0from
azat-msft:azat-msft/rel-18.0-metricscollection-concurrent
Nov 20, 2026
Merged

Azat Mukhametshin (azat-msft) merged 3 commits into
microsoft:rel/18.0from
azat-msft:azat-msft/rel-18.0-metricscollection-concurrent

Conversation

@azat-msft

Copy link
Copy Markdown
Member

Description

Backport of #15581 to rel/18.0. MetricsCollection used a plain Dictionary, and parallel attachment processing adds metrics from several threads. That fails TestRunWithCodeCoverageParallel intermittently with Operations that change non-concurrent collections must have exclusive access. It failed this way in 3 of the last 6 official rel/18.0 builds. rel/18.7 and later already have the fix.

The backported regression test now asserts the exact count, because this branch's MSTest has no Assert.IsGreaterThan.

Related issue

Fixes #15579 on rel/18.0.

  • I have ensured that there is a previously discussed and approved issue.

* fix: use ConcurrentDictionary in MetricsCollection to prevent concurrent modification

MetricsCollection.Add is called from multiple threads during parallel
test execution with data collectors. Using Dictionary causes
InvalidOperationException when concurrent updates corrupt the state.

Added regression test that exercises concurrent Add from 10 threads.

Closes microsoft#15579

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Fix Moq verification: use IDictionary instead of Dictionary

ConcurrentDictionary is not a subclass of Dictionary, so Moq's
It.Is<Dictionary<...>> type check fails. Use IDictionary<...> which
matches both Dictionary and ConcurrentDictionary.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
(cherry picked from commit bca4a11)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@azat-msft
Azat Mukhametshin (azat-msft) merged commit efa9756 into microsoft:rel/18.0 Nov 20, 2026
6 checks passed
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.

3 participants