From 8f541f8ea1a3d3b8e57fb750e5cbc8ff5bea24af Mon Sep 17 00:00:00 2001 From: Tim Haasdyk Date: Fri, 17 Jan 2025 15:27:46 +0100 Subject: [PATCH] Catch and ignore expected exceptions (#1386) --- backend/FwLite/FwLiteShared/Auth/OAuthService.cs | 15 +++++++++++---- .../FwLiteShared/Sync/BackgroundSyncService.cs | 15 +++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/backend/FwLite/FwLiteShared/Auth/OAuthService.cs b/backend/FwLite/FwLiteShared/Auth/OAuthService.cs index b67f77032..f018c7108 100644 --- a/backend/FwLite/FwLiteShared/Auth/OAuthService.cs +++ b/backend/FwLite/FwLiteShared/Auth/OAuthService.cs @@ -74,11 +74,18 @@ private readonly Channel protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - await foreach (var loginRequest in _requestChannel.Reader.ReadAllAsync(stoppingToken)) + try + { + await foreach (var loginRequest in _requestChannel.Reader.ReadAllAsync(stoppingToken)) + { + //don't await, otherwise we'll block the channel reader and only 1 login will be processed at a time + //cancel the login after 5 minutes, otherwise it'll probably hang forever and abandoned requests will never be cleaned up + _ = Task.Run(() => StartLogin(loginRequest, stoppingToken.Merge(new CancellationTokenSource(TimeSpan.FromMinutes(5)).Token)), stoppingToken); + } + } + catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) { - //don't await, otherwise we'll block the channel reader and only 1 login will be processed at a time - //cancel the login after 5 minutes, otherwise it'll probably hang forever and abandoned requests will never be cleaned up - _ = Task.Run(() => StartLogin(loginRequest, stoppingToken.Merge(new CancellationTokenSource(TimeSpan.FromMinutes(5)).Token)), stoppingToken); + // Expected during shutdown } } diff --git a/backend/FwLite/FwLiteShared/Sync/BackgroundSyncService.cs b/backend/FwLite/FwLiteShared/Sync/BackgroundSyncService.cs index e12033f60..b92868099 100644 --- a/backend/FwLite/FwLiteShared/Sync/BackgroundSyncService.cs +++ b/backend/FwLite/FwLiteShared/Sync/BackgroundSyncService.cs @@ -74,11 +74,18 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) await SyncProject(crdtProject, true, stoppingToken); } - await foreach (var project in _syncResultsChannel.Reader.ReadAllAsync(stoppingToken)) + try + { + await foreach (var project in _syncResultsChannel.Reader.ReadAllAsync(stoppingToken)) + { + //todo, this might not be required, but I can't remember why I added it + await Task.Delay(100, stoppingToken); + await SyncProject(project, false, stoppingToken); + } + } + catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) { - //todo, this might not be required, but I can't remember why I added it - await Task.Delay(100, stoppingToken); - await SyncProject(project, false, stoppingToken); + // Expected during shutdown } }