Skip to content

Commit 39052d8

Browse files
philasmarGarrettBeatty
authored andcommitted
fix retrying valid IO exceptions
1 parent 9647310 commit 39052d8

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

sdk/src/Services/S3/Custom/Transfer/Internal/AtomicFileHandler.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ internal class AtomicFileHandler : IDisposable
3333
{
3434
private string _tempFilePath;
3535
private bool _disposed = false;
36+
private static readonly object _fileLock = new object();
3637

3738
/// <summary>
3839
/// Creates a temporary file with unique identifier for atomic operations.
@@ -68,10 +69,24 @@ public string CreateTemporaryFile(string destinationPath)
6869
_tempFilePath = tempPath;
6970
return tempPath;
7071
}
71-
catch (IOException) when (attempt < 99)
72+
catch (IOException)
7273
{
73-
// File exists, try again with new ID
74-
continue;
74+
lock (_fileLock)
75+
{
76+
// If the file now exists when we check immediately after the exception,
77+
// it means another process or thread beat us to the creation (race condition).
78+
if (File.Exists(tempPath))
79+
{
80+
// File exists, try again with new ID
81+
continue;
82+
}
83+
else
84+
{
85+
// The file does *not* exist, which means the IOException was caused by
86+
// something else entirely (e.g., permissions, disk full, network error).
87+
throw; // Re-throw the original exception as it was an unexpected error.
88+
}
89+
}
7590
}
7691
}
7792

0 commit comments

Comments
 (0)