Skip to content

Commit 13fa363

Browse files
committed
Cancel OnMessage immediately IHostedService.StopAsync is invoked
1 parent d5d6c3a commit 13fa363

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/ServiceControl/Operations/ErrorIngestion.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public override async Task StopAsync(CancellationToken cancellationToken)
132132
{
133133
try
134134
{
135+
await stoppingToken.CancelAsync();
135136
await watchdog.Stop(cancellationToken);
136137
channel.Writer.Complete();
137138
await base.StopAsync(cancellationToken);
@@ -261,8 +262,11 @@ async Task StopAndTeardownInfrastructure(CancellationToken cancellationToken)
261262
}
262263
}
263264

264-
async Task OnMessage(MessageContext messageContext, CancellationToken cancellationToken)
265+
async Task OnMessage(MessageContext messageContext, CancellationToken cancellationTokenParent)
265266
{
267+
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationTokenParent, stoppingToken.Token);
268+
var cancellationToken = cts.Token;
269+
266270
if (settings.MessageFilter != null && settings.MessageFilter(messageContext))
267271
{
268272
return;
@@ -307,6 +311,7 @@ async Task EnsureStopped(CancellationToken cancellationToken = default)
307311
TransportInfrastructure transportInfrastructure;
308312
IMessageReceiver messageReceiver;
309313

314+
readonly CancellationTokenSource stoppingToken = new();
310315
readonly Settings settings;
311316
readonly ITransportCustomization transportCustomization;
312317
readonly TransportSettings transportSettings;

0 commit comments

Comments
 (0)