Skip to content

Commit e05abbc

Browse files
authored
Merge pull request #1408 from dotnet/betterRetry
Retry file operations on access denied errors
2 parents e166693 + 9e543ab commit e05abbc

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

src/Shared/Utilities.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ namespace Nerdbank.GitVersioning;
77

88
internal static class Utilities
99
{
10-
private const int ProcessCannotAccessFileHR = unchecked((int)0x80070020);
10+
private const int SharingViolation = unchecked((int)0x80070020); // ERROR_SHARING_VIOLATION
11+
private const int AccessDenied = unchecked((int)0x80070005); // ERROR_ACCESS_DENIED
1112

1213
internal static void FileOperationWithRetry(Action operation)
1314
{
@@ -20,11 +21,15 @@ internal static void FileOperationWithRetry(Action operation)
2021
operation();
2122
break;
2223
}
23-
catch (IOException ex) when (ex.HResult == ProcessCannotAccessFileHR && retriesLeft > 0)
24+
catch (Exception ex) when (IsTransientFileAccessError(ex) && retriesLeft > 1)
2425
{
25-
Task.Delay(100).Wait();
26+
Thread.Sleep(100);
2627
continue;
2728
}
2829
}
2930
}
31+
32+
private static bool IsTransientFileAccessError(Exception ex) => ex is
33+
IOException { HResult: SharingViolation } or
34+
UnauthorizedAccessException { HResult: AccessDenied };
3035
}

0 commit comments

Comments
 (0)