Skip to content

Fix flaky test: retry Directory.Delete for transient testhost.exe locks#54151

Merged
marcpopMSFT merged 2 commits intomainfrom
marcpopMSFT-fixmainCI
May 1, 2026
Merged

Fix flaky test: retry Directory.Delete for transient testhost.exe locks#54151
marcpopMSFT merged 2 commits intomainfrom
marcpopMSFT-fixmainCI

Conversation

@marcpopMSFT
Copy link
Copy Markdown
Member

ItemTemplate_CanBeInstalledAndTestArePassing (nunit/f#) fails in CI with UnauthorizedAccessException on testhost.exe during Directory.Delete cleanup. This is a Windows race condition where dotnet test has exited but the OS hasn't fully released the testhost.exe file handle yet.

Replace all bare Directory.Delete calls in the three test methods with a new DeleteDirectoryWithRetry helper that retries up to 10 times with linear backoff on UnauthorizedAccessException and IOException.

ItemTemplate_CanBeInstalledAndTestArePassing (nunit/f#) fails in CI with
UnauthorizedAccessException on testhost.exe during Directory.Delete cleanup.
This is a Windows race condition where dotnet test has exited but the OS
hasn't fully released the testhost.exe file handle yet.

Replace all bare Directory.Delete calls in the three test methods with a
new DeleteDirectoryWithRetry helper that retries up to 10 times with
linear backoff on UnauthorizedAccessException and IOException.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 29, 2026 18:38
@marcpopMSFT marcpopMSFT requested a review from a team as a code owner April 29, 2026 18:38
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

Addresses CI flakiness in dotnet-new integration tests on Windows by making test cleanup more resilient to transient file locks (e.g., testhost.exe) during directory deletion.

Changes:

  • Replaces direct Directory.Delete(..., recursive: true) cleanup calls in three test methods with a retrying helper.
  • Adds DeleteDirectoryWithRetry helper that retries deletes on UnauthorizedAccessException and IOException with linear backoff.

Comment thread test/dotnet-new.IntegrationTests/DotnetNewTestTemplatesTests.cs Outdated
Copy link
Copy Markdown
Member

@joeloff joeloff left a comment

Choose a reason for hiding this comment

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

Don't we use some sort of exponential backoff logic in the SDK that William wrote long ago? Should we just use that same delay pattern in the test?

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@marcpopMSFT
Copy link
Copy Markdown
Member Author

Don't we use some sort of exponential backoff logic in the SDK that William wrote long ago? Should we just use that same delay pattern in the test?

Looks like we have exponential retry in the utils library. If this doesn't work, we can try that to give us more time.

@marcpopMSFT marcpopMSFT enabled auto-merge April 29, 2026 21:45
}
catch (Exception ex) when ((ex is UnauthorizedAccessException or IOException) && i < maxRetries - 1)
{
Thread.Sleep(100 * (i + 1));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this going to be enough of a wait? Total of ~6 seconds.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think that's Jacques suggestion above to use the exponential backoff. I figured I'd start with this and move to that later as that can greatly increase the time (though if it's needed).

@marcpopMSFT marcpopMSFT merged commit 91e8ea2 into main May 1, 2026
24 checks passed
@marcpopMSFT marcpopMSFT deleted the marcpopMSFT-fixmainCI branch May 1, 2026 00:33
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.

4 participants