Skip to content

Commit

Permalink
release 1.5.2.0
Browse files Browse the repository at this point in the history
Fix the bug where exception is not throwed after retrying.
  • Loading branch information
0x7c13 committed Dec 21, 2023
1 parent a129039 commit 10a3346
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Notepads/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="mp uap uap5 uap10 desktop4 iot2 rescap">

<Identity Name="19282JackieLiu.Notepads-Beta" Publisher="CN=40E66D07-5A3A-4954-9CA3-A1EB15ED0804" Version="1.5.1.0" />
<Identity Name="19282JackieLiu.Notepads-Beta" Publisher="CN=40E66D07-5A3A-4954-9CA3-A1EB15ED0804" Version="1.5.2.0" />
<mp:PhoneIdentity PhoneProductId="df234254-de03-48f0-80a0-11b5082ec740" PhonePublisherId="00000000-0000-0000-0000-000000000000" />

<Properties>
Expand Down
32 changes: 21 additions & 11 deletions src/Notepads/Utilities/FileSystemUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,8 @@ public static async Task WriteTextToFileAsync(StorageFile file, string text, Enc
var content = encoding.GetBytes(text);
var result = encoding.GetPreamble().Concat(content).ToArray();
await PathIO.WriteBytesAsync(file.Path, result);
});
},
maxRetryAttempts: 3); // Retry 3 times for this case.
}
else // Use StorageFile API to save
{
Expand All @@ -589,7 +590,8 @@ public static async Task WriteTextToFileAsync(StorageFile file, string text, Enc
await writer.FlushAsync();
stream.SetLength(stream.Position); // Truncate
}
});
},
maxRetryAttempts: 5); // Retry 5 times for this case
}
}

Expand All @@ -600,13 +602,14 @@ public static async Task WriteTextToFileAsync(StorageFile storageFile, string te
{
await ExecuteFileIOOperationWithRetries(storageFile,
"FileSystemUtility_WriteTextToFileAsync_UsingFileIO",
async () => await FileIO.WriteTextAsync(storageFile, text));
async () => await FileIO.WriteTextAsync(storageFile, text),
maxRetryAttempts: 10); // Retry 10 times for this case since it is used for saving the session data.
}

private static async Task ExecuteFileIOOperationWithRetries(StorageFile file,
string operationName,
Func<Task> action,
int maxRetryAttempts = 7)
int maxRetryAttempts)
{
bool deferUpdatesUsed = true;
int retryAttempts = 0;
Expand Down Expand Up @@ -635,13 +638,20 @@ public static async Task WriteTextToFileAsync(StorageFile storageFile, string te
await action.Invoke(); // Execute FileIO action
break;
}
catch (Exception ex) when ((ex.HResult == ERROR_ACCESS_DENIED) ||
(ex.HResult == ERROR_SHARING_VIOLATION) ||
(ex.HResult == ERROR_UNABLE_TO_REMOVE_REPLACED) ||
(ex.HResult == ERROR_FAIL))
catch (Exception ex)
{
errorCodes.Add("0x" + Convert.ToString(ex.HResult, 16));
await Task.Delay(13); // Delay 13ms before retrying for all retriable errors
if (retryAttempts == maxRetryAttempts)
{
throw; // Rethrow the last attempt exception
}
else if ((ex.HResult == ERROR_ACCESS_DENIED) ||
(ex.HResult == ERROR_SHARING_VIOLATION) ||
(ex.HResult == ERROR_UNABLE_TO_REMOVE_REPLACED) ||
(ex.HResult == ERROR_FAIL))
{
errorCodes.Add("0x" + Convert.ToString(ex.HResult, 16));
await Task.Delay(10); // Delay 10ms before retrying for all retriable errors
}
}
}
}
Expand All @@ -663,7 +673,7 @@ public static async Task WriteTextToFileAsync(StorageFile storageFile, string te
// File name, path and content are not included to respect/protect user privacy
Analytics.TrackEvent(operationName, new Dictionary<string, string>()
{
{ "RetryAttempts", (retryAttempts - 1).ToString() },
{ "RetryAttempts", retryAttempts.ToString() },
{ "DeferUpdatesUsed" , deferUpdatesUsed.ToString() },
{ "ErrorCodes", string.Join(", ", errorCodes) },
{ "FileUpdateStatus", fileUpdateStatus }
Expand Down

0 comments on commit 10a3346

Please sign in to comment.