Skip to content

Commit fbf965c

Browse files
authored
Merge pull request #1340 from dotnet/stabilize-tests
Fix intermittent test host crash on Windows during cleanup
2 parents 70eeceb + 03ce469 commit fbf965c

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

test/Nerdbank.GitVersioning.Tests/RepoTestBase.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ protected virtual void Dispose(bool disposing)
8383
{
8484
this.LibGit2Repository?.Dispose();
8585
this.Context?.Dispose();
86+
87+
// LibGit2Sharp may still hold native file handles even after Dispose().
88+
// Force a GC cycle to release any lingering native handles before
89+
// attempting to delete the temporary directories.
90+
GC.Collect();
91+
GC.WaitForPendingFinalizers();
92+
8693
foreach (string dir in this.repoDirectories)
8794
{
8895
try

test/Nerdbank.GitVersioning.Tests/TestUtilities.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,19 @@ internal static void DeleteDirectory(string path)
2929
{
3030
Directory.Delete(path, true);
3131
}
32-
catch (UnauthorizedAccessException)
32+
catch (Exception ex) when (ex is UnauthorizedAccessException or IOException)
3333
{
3434
// Unknown why this fails so often.
3535
// Somehow making commits with libgit2sharp locks files
3636
// such that we can't delete them (but Windows Explorer can).
37+
// Redirect stderr so that rd errors don't bleed into the test host's
38+
// stderr, which can cause the test platform to misinterpret file-lock
39+
// messages as a crash (non-zero exit + stderr output).
3740
var psi = new ProcessStartInfo("cmd.exe", $"/c rd /s /q \"{path}\"");
3841
psi.WorkingDirectory = Path.GetTempPath();
3942
psi.WindowStyle = ProcessWindowStyle.Hidden;
43+
psi.RedirectStandardError = true;
44+
psi.UseShellExecute = false;
4045
var process = Process.Start(psi);
4146
process.WaitForExit();
4247
}

0 commit comments

Comments
 (0)