Skip to content

Fix concurrent modification in MetricsCollection#15581

Merged
nohwnd merged 2 commits intomicrosoft:mainfrom
nohwnd:fix-concurrency
Mar 26, 2026
Merged

Fix concurrent modification in MetricsCollection#15581
nohwnd merged 2 commits intomicrosoft:mainfrom
nohwnd:fix-concurrency

Conversation

@nohwnd
Copy link
Copy Markdown
Member

@nohwnd nohwnd commented Mar 24, 2026

Use \ConcurrentDictionary\ instead of \Dictionary\ in \MetricsCollection\ to prevent \InvalidOperationException\ when metrics are added concurrently during parallel test execution with data collectors.

Closes #15579

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

Copilot AI review requested due to automatic review settings March 24, 2026 20:14
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 addresses concurrency issues in telemetry metrics collection by switching to a thread-safe dictionary implementation, and also disables the EnableShutdownAfterTestRun API (marking it obsolete with a compile-time error) and removes its remaining usage/tests.

Changes:

  • Replace Dictionary with ConcurrentDictionary in MetricsCollection to prevent concurrent modification exceptions.
  • Mark EnableShutdownAfterTestRun as [Obsolete(..., error: true)] across the interface and implementations, and stop reading it during runs.
  • Remove unit tests and code paths that depended on EnableShutdownAfterTestRun.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/Adapter/FrameworkHandleTests.cs Removes tests covering EnableShutdownAfterTestRun (now made obsolete with error).
src/Microsoft.TestPlatform.ObjectModel/Adapter/Interfaces/IFrameworkHandle.cs Marks EnableShutdownAfterTestRun obsolete (error) at the public interface level.
src/Microsoft.TestPlatform.CrossPlatEngine/Execution/BaseRunTests.cs Removes runtime read of EnableShutdownAfterTestRun during test execution.
src/Microsoft.TestPlatform.CrossPlatEngine/Adapter/FrameworkHandle.cs Marks the EnableShutdownAfterTestRun implementation obsolete (error).
src/Microsoft.TestPlatform.Common/Telemetry/MetricsCollection.cs Switches internal storage to ConcurrentDictionary for thread-safe metric writes.
src/Microsoft.TestPlatform.Common/ExtensionDecorators/SerialTestRunDecoratorFrameworkHandle.cs Marks decorator property EnableShutdownAfterTestRun obsolete (error).
src/Microsoft.TestPlatform.Common/ExtensionDecorators/SerialTestRunDecorator.cs Marks decorator property EnableShutdownAfterTestRun obsolete (error).

Copilot AI review requested due to automatic review settings March 25, 2026 15:16
…ent 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>
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

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

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>
@nohwnd
Copy link
Copy Markdown
Member Author

nohwnd commented Mar 26, 2026

FAils on hangdump flakiness and the issue with default diag

@nohwnd nohwnd merged commit bca4a11 into microsoft:main Mar 26, 2026
2 of 4 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.

concurrency error

2 participants