From 8897476b81ca8c00c47bb95d64fdfd03f941b192 Mon Sep 17 00:00:00 2001 From: Leendert de Borst Date: Mon, 8 Jun 2026 08:57:50 +0200 Subject: [PATCH] Fix HostedService to restart loop upon non-cancellation exception (#2144) --- .../ServiceExtensions/StatusHostedService.cs | 6 ++++-- .../Utilities/AliasVault.WorkerStatus/StatusWorker.cs | 8 ++++---- 2 files changed, 8 insertions(+), 6 deletions(-) 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(ILogger> 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 logger, Func