Skip to content

Commit

Permalink
Catch and ignore expected exceptions (#1386)
Browse files Browse the repository at this point in the history
  • Loading branch information
myieye authored Jan 17, 2025
1 parent a62c709 commit 8f541f8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
15 changes: 11 additions & 4 deletions backend/FwLite/FwLiteShared/Auth/OAuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,18 @@ private readonly Channel<OAuthLoginRequest>

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
}
}

Expand Down
15 changes: 11 additions & 4 deletions backend/FwLite/FwLiteShared/Sync/BackgroundSyncService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit 8f541f8

Please sign in to comment.