Skip to content

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

Fix HostedService to restart loop upon non-cancellation exception (#2144)

Leendert de Borst8897476b81ca
diff --git a/apps/server/Utilities/AliasVault.WorkerStatus/ServiceExtensions/StatusHostedService.cs b/apps/server/Utilities/AliasVault.WorkerStatus/ServiceExtensions/StatusHostedService.cs
index 6c0fe02ae..d234e15ba 100644
--- a/apps/server/Utilities/AliasVault.WorkerStatus/ServiceExtensions/StatusHostedService.cs
+++ b/apps/server/Utilities/AliasVault.WorkerStatus/ServiceExtensions/StatusHostedService.cs
@@ -58,14 +58,16 @@ public class StatusHostedService<T>(ILogger<StatusHostedService<T>> logger, Glob
// Start the inner while loop with the second cancellationToken.
await ExecuteInnerAsync(stoppingToken);
}
- catch (OperationCanceledException ex)
+ catch (OperationCanceledException ex) when (stoppingToken.IsCancellationRequested)
{
- // Expected so we only log information.
+ // Genuine host shutdown, exit the loop gracefully.
logger.LogInformation(ex, "StatusHostedService<{ServiceType}> is stopping due to a cancellation request.", typeof(T).Name);
break;
}
catch (Exception ex)
{
+ // Any other exception should not break the loop but instead log the error
+ // and let the restart logic below retry the worker.
logger.LogError(ex, "An error occurred in StatusHostedService<{ServiceType}>", typeof(T).Name);
}
finally
diff --git a/apps/server/Utilities/AliasVault.WorkerStatus/StatusWorker.cs b/apps/server/Utilities/AliasVault.WorkerStatus/StatusWorker.cs
index a3dfb12a8..996dc9fc1 100644
--- a/apps/server/Utilities/AliasVault.WorkerStatus/StatusWorker.cs
+++ b/apps/server/Utilities/AliasVault.WorkerStatus/StatusWorker.cs
@@ -65,20 +65,20 @@ public class StatusWorker(ILogger<StatusWorker> logger, Func<IWorkerStatusDbCont
await Task.Delay(5000, stoppingToken);
}
- catch (TaskCanceledException)
+ catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
{
- // Expected when the service is stopped - exit the loop gracefully.
+ // Genuine host shutdown, exit the loop gracefully.
break;
}
catch (Exception e)
{
+ // Any other exception including database cancellations/timeouts should not break the loop but instead log the error
+ // and let the restart logic below retry the worker.
logger.LogError(e, "StatusWorker exception");
await Task.Delay(5000, stoppingToken);
}
}
- // Service is hard stopping: not in software but on OS level.
- // Mark the service as stopped.
try
{
using var dbContext = createDbContext();