Skip to content

Commit 6ed5af2

Browse files
committed
fix: add retry logic for file stream creation in asset download
- Implemented a retry mechanism with exponential backoff to handle file locking issues during file stream creation. - Improved error logging for failed attempts to enhance debugging and traceability.
1 parent 5b2720e commit 6ed5af2

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

EasyExtractUnitypackageRework/EasyExtractCrossPlatform/Services/UpdateService.Assets.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,31 @@ private static async Task DownloadAssetAsync(ReleaseAssetInfo asset, string dest
2828

2929
await using var responseStream =
3030
await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
31-
await using var fileStream =
32-
new FileStream(destinationPath, FileMode.Create, FileAccess.Write, FileShare.None);
31+
32+
FileStream? fileStream = null;
33+
const int maxRetries = 3;
34+
for (var i = 0; i < maxRetries; i++)
35+
try
36+
{
37+
fileStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write, FileShare.None);
38+
break;
39+
}
40+
catch (IOException ex)
41+
{
42+
if (i == maxRetries - 1)
43+
{
44+
LoggingService.LogError(
45+
$"DownloadAsset: failed to create file stream after {maxRetries} attempts | path='{destinationPath}'",
46+
ex);
47+
throw;
48+
}
49+
50+
LoggingService.LogWarning(
51+
$"DownloadAsset: file locked, retrying... ({i + 1}/{maxRetries}) | path='{destinationPath}'");
52+
await Task.Delay(500 * (i + 1), cancellationToken).ConfigureAwait(false);
53+
}
54+
55+
await using var fs = fileStream!;
3356

3457
var buffer = new byte[81920];
3558
long totalRead = 0;
@@ -40,7 +63,7 @@ private static async Task DownloadAssetAsync(ReleaseAssetInfo asset, string dest
4063
if (read == 0)
4164
break;
4265

43-
await fileStream.WriteAsync(buffer.AsMemory(0, read), cancellationToken).ConfigureAwait(false);
66+
await fs.WriteAsync(buffer.AsMemory(0, read), cancellationToken).ConfigureAwait(false);
4467
totalRead += read;
4568

4669
double? percentage = null;

0 commit comments

Comments
 (0)