Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 14, 2025

This PR addresses intermittent test failures in System.Diagnostics.Tests.PerformanceCounterTests.PerformanceCounter_IncrementBy_IncrementByReadOnly and other performance counter tests that occur due to file access conflicts during category creation and deletion operations.

Root Cause

The tests were failing with:

System.ComponentModel.Win32Exception : The process cannot access the file because it is being used by another process.
   at System.Diagnostics.PerformanceCounterLib.RegisterFiles(String arg0, Boolean unregister)
   at System.Diagnostics.PerformanceCounterCategory.Delete(String categoryName)
   at System.Diagnostics.Tests.Helpers.DeleteCategory(String categoryName)

This error indicates file access conflicts, likely due to antivirus scanners or other processes temporarily accessing the performance counter files during test execution.

Solution

Enhanced the test helper methods with robust retry logic to handle transient file access issues:

1. Enhanced CreateCategory helper

  • Wraps PerformanceCounterCategory.Create calls with RetryHelper.Execute
  • Retries up to 10 times for retriable exceptions

2. Enhanced DeleteCategory helper

  • Wraps PerformanceCounterCategory.Delete calls with RetryHelper.Execute
  • Retries up to 10 times for retriable exceptions

3. Added intelligent exception filtering

  • IsRetriableException method identifies specific exceptions that should trigger retries:
    • Win32Exception with error codes 32 (sharing violation), 5 (access denied), 33 (lock violation)
    • IOException for general file access issues
    • UnauthorizedAccessException for permission issues
  • Non-retriable exceptions (like argument validation errors) are immediately propagated

4. Updated test cleanup

  • Changed direct PerformanceCounterCategory.Delete calls in test files to use the retry-enabled Helpers.DeleteCategory method

Example of the fix in action

Before:

// This could fail with file access conflicts
PerformanceCounterCategory.Delete(categoryName);

After:

// This retries automatically on file access conflicts
RetryHelper.Execute(() =>
{
    PerformanceCounterCategory.Delete(categoryName);
}, maxAttempts: 10, retryWhen: IsRetriableException);

The retry logic follows the same pattern already established in the codebase with RetryOnAllPlatforms methods, ensuring consistency with existing test infrastructure.

Fixes #105925.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

…ess conflicts

Co-authored-by: ericstj <8918108+ericstj@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Test failure: System.Diagnostics.Tests.PerformanceCounterTests.PerformanceCounter_IncrementBy_IncrementByReadOnly Fix intermittent PerformanceCounter test failures due to file access conflicts Jul 14, 2025
@Copilot Copilot AI requested a review from ericstj July 14, 2025 19:22
Copilot finished work on behalf of ericstj July 14, 2025 19:22
Copy link
Member

@ericstj ericstj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot please address this feedback.

…calls with RetryHelper and use Helpers.DeleteCategory

Co-authored-by: ericstj <8918108+ericstj@users.noreply.github.com>
@Copilot Copilot AI requested a review from ericstj July 14, 2025 20:51
Copilot finished work on behalf of ericstj July 14, 2025 20:51
@ericstj ericstj requested a review from tarekgh July 14, 2025 20:55
@ericstj ericstj marked this pull request as ready for review July 14, 2025 20:55
Copy link
Member

@tarekgh tarekgh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@ericstj
Copy link
Member

ericstj commented Jul 15, 2025

/ba-g Android tests timeout - not related, IO.Pipelines test dead-lettered - not touched in this PR.

@ericstj ericstj merged commit c20e5e1 into main Jul 15, 2025
82 of 87 checks passed
@jkotas jkotas deleted the copilot/fix-105925 branch July 20, 2025 00:03
@github-actions github-actions bot locked and limited conversation to collaborators Aug 19, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test failure: System.Diagnostics.Tests.PerformanceCounterTests.PerformanceCounter_IncrementBy_IncrementByReadOnly

3 participants