Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Make Admin JSInvoke more robust with retry

Leendert de Borstd06db18c42ee
diff --git a/apps/server/AliasVault.Admin/Main/Pages/MainBase.cs b/apps/server/AliasVault.Admin/Main/Pages/MainBase.cs
index 671aec047..4af1097d8 100644
--- a/apps/server/AliasVault.Admin/Main/Pages/MainBase.cs
+++ b/apps/server/AliasVault.Admin/Main/Pages/MainBase.cs
@@ -108,8 +108,19 @@ public abstract class MainBase : OwningComponentBase
if (firstRender)
{
- // Update default ApexCharts chart color based on the dark mode setting.
- await SetDefaultApexChartOptionsAsync();
+ // Update default ApexCharts chart color based on the dark mode setting. Cosmetic, so a failure must not end the circuit.
+ try
+ {
+ await SetDefaultApexChartOptionsAsync();
+ }
+ catch (JSDisconnectedException)
+ {
+ // The browser disconnected before the first render finished, nothing left to style.
+ }
+ catch (InvalidOperationException)
+ {
+ // The page script did not load in time, charts keep their default colors.
+ }
}
}
diff --git a/apps/server/AliasVault.Admin/Services/JSInvokeService.cs b/apps/server/AliasVault.Admin/Services/JSInvokeService.cs
index d8a99083c..24f879478 100644
--- a/apps/server/AliasVault.Admin/Services/JSInvokeService.cs
+++ b/apps/server/AliasVault.Admin/Services/JSInvokeService.cs
@@ -14,6 +14,11 @@ using Microsoft.JSInterop;
/// </summary>
public class JsInvokeService(IJSRuntime js)
{
+ /// <summary>
+ /// The smallest delay between attempts, so a zero initial delay still backs off.
+ /// </summary>
+ private static readonly TimeSpan MinRetryDelay = TimeSpan.FromMilliseconds(50);
+
/// <summary>
/// Invoke a JavaScript function with retry and exponential backoff.
/// </summary>
@@ -24,7 +29,7 @@ public class JsInvokeService(IJSRuntime js)
/// <returns>Async Task.</returns>
public async Task RetryInvokeAsync(string functionName, TimeSpan initialDelay, int maxAttempts, params object[] args)
{
- TimeSpan delay = initialDelay;
+ TimeSpan delay = initialDelay > MinRetryDelay ? initialDelay : MinRetryDelay;
for (int attempt = 1; attempt <= maxAttempts; attempt++)
{
try
@@ -36,6 +41,11 @@ public class JsInvokeService(IJSRuntime js)
return; // Successfully called the JS function, exit the method
}
}
+ catch (JSDisconnectedException)
+ {
+ // The browser is gone, retrying cannot succeed.
+ return;
+ }
catch
{
// Optionally log the exception
@@ -61,9 +71,10 @@ public class JsInvokeService(IJSRuntime js)
/// <param name="args">Arguments to pass on to the javascript function.</param>
/// <returns>The value returned from the JavaScript function.</returns>
/// <exception cref="InvalidOperationException">Thrown when the JS function could not be called after all attempts.</exception>
+ /// <exception cref="JSDisconnectedException">Thrown when the browser disconnected from the circuit.</exception>
public async Task<TValue> RetryInvokeWithResultAsync<TValue>(string functionName, TimeSpan initialDelay, int maxAttempts, params object[] args)
{
- TimeSpan delay = initialDelay;
+ TimeSpan delay = initialDelay > MinRetryDelay ? initialDelay : MinRetryDelay;
for (int attempt = 1; attempt <= maxAttempts; attempt++)
{
@@ -75,6 +86,11 @@ public class JsInvokeService(IJSRuntime js)
return await js.InvokeAsync<TValue>(functionName, args);
}
}
+ catch (JSDisconnectedException)
+ {
+ // The browser is gone, retrying cannot succeed.
+ throw;
+ }
catch
{
// Optionally log the exception