From 8b942ffe8dc00c7ea064a2e9d34169084de012a7 Mon Sep 17 00:00:00 2001 From: Ramon Smits Date: Fri, 7 Feb 2025 15:20:38 +0100 Subject: [PATCH] Ensure TrySetException is always set on exception --- .../Auditing/AuditIngestion.cs | 17 +++++++++-------- src/ServiceControl/Operations/ErrorIngestion.cs | 17 +++++++++-------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/ServiceControl.Audit/Auditing/AuditIngestion.cs b/src/ServiceControl.Audit/Auditing/AuditIngestion.cs index 7ecd7aaf20..564967a872 100644 --- a/src/ServiceControl.Audit/Auditing/AuditIngestion.cs +++ b/src/ServiceControl.Audit/Auditing/AuditIngestion.cs @@ -223,19 +223,20 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) await auditIngestor.Ingest(contexts); } } - catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) + catch (Exception e) { - throw; // Catch again in outer catch - } - catch (Exception e) // show must go on - { - logger.Info("Ingesting messages failed", e); - // signal all message handling tasks to terminate foreach (var context in contexts) { - context.GetTaskCompletionSource().TrySetException(e); + _ = context.GetTaskCompletionSource().TrySetException(e); } + + if (e is OperationCanceledException && stoppingToken.IsCancellationRequested) + { + break; // ExecuteAsync cancelled + } + + logger.Info("Ingesting messages failed", e); } finally { diff --git a/src/ServiceControl/Operations/ErrorIngestion.cs b/src/ServiceControl/Operations/ErrorIngestion.cs index 331eb38440..d7e8ad302b 100644 --- a/src/ServiceControl/Operations/ErrorIngestion.cs +++ b/src/ServiceControl/Operations/ErrorIngestion.cs @@ -99,19 +99,20 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) await ingestor.Ingest(contexts); } } - catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) + catch (Exception e) { - throw; // Catch again in outer catch - } - catch (Exception e) // show must go on - { - Logger.Info("Ingesting messages failed", e); - // signal all message handling tasks to terminate foreach (var context in contexts) { - context.GetTaskCompletionSource().TrySetException(e); + _ = context.GetTaskCompletionSource().TrySetException(e); } + + if (e is OperationCanceledException && stoppingToken.IsCancellationRequested) + { + break; // ExecuteAsync cancelled + } + + Logger.Info("Ingesting messages failed", e); } finally {