Skip to content

Commit 9c249c9

Browse files
committed
Fix catch (OperationCancelledException) blocks to add when guards - only ignore cancellations set by caller
1 parent 3fbb3e8 commit 9c249c9

File tree

28 files changed

+45
-41
lines changed

28 files changed

+45
-41
lines changed

src/Particular.LicensingComponent/AuditThroughput/AuditThroughputCollectorHostedService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken)
4040
}
4141
} while (await timer.WaitForNextTickAsync(cancellationToken));
4242
}
43-
catch (OperationCanceledException)
43+
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
4444
{
4545
logger.LogInformation($"Stopping {nameof(AuditThroughputCollectorHostedService)}");
4646
}

src/Particular.LicensingComponent/BrokerThroughput/BrokerThroughputCollectorHostedService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static ReadOnlyDictionary<string, string> LoadBrokerSettingValues(IEnumerable<Ke
5353
}
5454
} while (await timer.WaitForNextTickAsync(stoppingToken));
5555
}
56-
catch (OperationCanceledException)
56+
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
5757
{
5858
logger.LogInformation($"Stopping {nameof(BrokerThroughputCollectorHostedService)}");
5959
}

src/ServiceControl.AcceptanceTesting/ScenarioWithEndpointBehaviorExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ public override async Task Stop(CancellationToken cancellationToken = default)
139139
{
140140
await checkTask;
141141
}
142-
catch (OperationCanceledException)
142+
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
143143
{
144-
//Swallow
144+
// Even though we are stopping, ONLY swallow when OCE from callee to not hide any ungraceful stop errors
145145
}
146146
finally
147147
{

src/ServiceControl.Audit/Auditing/ImportFailedAudits.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ await failedAuditStore.ProcessFailedMessages(
4848
Logger.Debug($"Successfully re-imported failed audit message {transportMessage.Id}.");
4949
}
5050
}
51-
catch (OperationCanceledException)
51+
catch (OperationCanceledException) when (token.IsCancellationRequested)
5252
{
5353
// no-op
5454
}

src/ServiceControl.Audit/Infrastructure/Hosting/Commands/ImportFailedAuditsCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public override async Task Execute(HostArguments args, Settings settings)
3737
{
3838
await importer.Run(tokenSource.Token);
3939
}
40-
catch (OperationCanceledException)
40+
catch (OperationCanceledException) when (tokenSource.IsCancellationRequested)
4141
{
4242
// no op
4343
}

src/ServiceControl.Infrastructure.Metrics/MetricsReporter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void Start()
3232
await Task.Delay(interval, tokenSource.Token).ConfigureAwait(false);
3333
}
3434
}
35-
catch (OperationCanceledException)
35+
catch (OperationCanceledException) when (tokenSource.IsCancellationRequested)
3636
{
3737
//no-op
3838
}

src/ServiceControl.Infrastructure/AsyncTimer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public TimerJob(Func<CancellationToken, Task<TimerJobExecutionResult>> callback,
4040

4141
//Otherwise execute immediately
4242
}
43-
catch (OperationCanceledException)
43+
catch (OperationCanceledException) when (token.IsCancellationRequested)
4444
{
4545
// no-op
4646
}
@@ -50,7 +50,7 @@ public TimerJob(Func<CancellationToken, Task<TimerJobExecutionResult>> callback,
5050
}
5151
}
5252
}
53-
catch (OperationCanceledException)
53+
catch (OperationCanceledException) when (token.IsCancellationRequested)
5454
{
5555
// no-op
5656
}
@@ -64,7 +64,7 @@ public async Task Stop()
6464
return;
6565
}
6666

67-
tokenSource.Cancel();
67+
await tokenSource.CancelAsync().ConfigureAwait(false);
6868
tokenSource.Dispose();
6969

7070
if (task != null)
@@ -73,7 +73,7 @@ public async Task Stop()
7373
{
7474
await task.ConfigureAwait(false);
7575
}
76-
catch (OperationCanceledException)
76+
catch (OperationCanceledException) when (tokenSource.IsCancellationRequested)
7777
{
7878
//NOOP
7979
}

src/ServiceControl.Infrastructure/ReadOnlyStream.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public override Task CopyToAsync(Stream destination, int bufferSize, Cancellatio
5151

5252
return Task.CompletedTask;
5353
}
54-
catch (OperationCanceledException e)
54+
catch (OperationCanceledException e) when (cancellationToken.IsCancellationRequested)
5555
{
5656
return Task.FromCanceled(e.CancellationToken);
5757
}
@@ -113,7 +113,7 @@ public override Task<int> ReadAsync(byte[] buffer, int offset, int count, Cancel
113113

114114
return Task.FromResult(result);
115115
}
116-
catch (OperationCanceledException e)
116+
catch (OperationCanceledException e) when (cancellationToken.IsCancellationRequested)
117117
{
118118
return Task.FromCanceled<int>(e.CancellationToken);
119119
}
@@ -136,7 +136,7 @@ public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken
136136

137137
return new ValueTask<int>(result);
138138
}
139-
catch (OperationCanceledException e)
139+
catch (OperationCanceledException e) when (cancellationToken.IsCancellationRequested)
140140
{
141141
return new ValueTask<int>(Task.FromCanceled<int>(e.CancellationToken));
142142
}

src/ServiceControl.Infrastructure/Watchdog.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ public Task Start(Action onFailedOnStartup)
5454

5555
failedOnStartup ??= false;
5656
}
57-
catch (OperationCanceledException)
57+
catch (OperationCanceledException) when (!shutdownTokenSource.IsCancellationRequested)
5858
{
59-
//Do not Delay
59+
// Continue, as OCE is not from caller
6060
continue;
6161
}
6262
catch (Exception e)
@@ -81,7 +81,7 @@ public Task Start(Action onFailedOnStartup)
8181
{
8282
await Task.Delay(timeToWaitBetweenStartupAttempts, shutdownTokenSource.Token).ConfigureAwait(false);
8383
}
84-
catch (OperationCanceledException)
84+
catch (OperationCanceledException) when (shutdownTokenSource.IsCancellationRequested)
8585
{
8686
//Ignore
8787
}

src/ServiceControl.Monitoring/Infrastructure/RemoveExpiredEndpointInstances.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken)
4848
}
4949
} while (await timer.WaitForNextTickAsync(cancellationToken));
5050
}
51-
catch (OperationCanceledException)
51+
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
5252
{
5353
logger.LogInformation($"Stopping {nameof(RemoveExpiredEndpointInstances)} timer");
5454
}

0 commit comments

Comments
 (0)