Fix flaky test: retry Directory.Delete for transient testhost.exe locks#54151
Fix flaky test: retry Directory.Delete for transient testhost.exe locks#54151marcpopMSFT merged 2 commits intomainfrom
Conversation
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>
There was a problem hiding this comment.
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
DeleteDirectoryWithRetryhelper that retries deletes onUnauthorizedAccessExceptionandIOExceptionwith linear backoff.
joeloff
left a comment
There was a problem hiding this comment.
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>
Looks like we have exponential retry in the utils library. If this doesn't work, we can try that to give us more time. |
| } | ||
| catch (Exception ex) when ((ex is UnauthorizedAccessException or IOException) && i < maxRetries - 1) | ||
| { | ||
| Thread.Sleep(100 * (i + 1)); |
There was a problem hiding this comment.
Is this going to be enough of a wait? Total of ~6 seconds.
There was a problem hiding this comment.
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).
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.