Skip to content

Commit 427e89d

Browse files
committed
Watchdog stop passed CancellationToken.None which could block stop till infinity
1 parent fa0cec2 commit 427e89d

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

src/ServiceControl.Audit/Auditing/AuditIngestion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public override async Task StopAsync(CancellationToken cancellationToken)
260260
{
261261
try
262262
{
263-
await watchdog.Stop();
263+
await watchdog.Stop(cancellationToken);
264264
channel.Writer.Complete();
265265
await base.StopAsync(cancellationToken);
266266
}

src/ServiceControl.Infrastructure.Tests/WatchdogTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public async Task It_shuts_down_gracefully()
3131

3232
await started.Task;
3333

34-
await dog.Stop();
34+
await dog.Stop(TestContext.CurrentContext.CancellationToken);
3535

3636
await stopped.Task;
3737
}
@@ -52,7 +52,7 @@ public async Task When_stop_fails_it_reports_the_failure()
5252

5353
await started.Task;
5454

55-
await dog.Stop();
55+
await dog.Stop(TestContext.CurrentContext.CancellationToken);
5656

5757
Assert.That(lastFailure, Is.EqualTo("Simulated"));
5858
}
@@ -91,7 +91,7 @@ public async Task On_failure_triggers_stopping()
9191

9292
await restarted.Task;
9393

94-
await dog.Stop();
94+
await dog.Stop(TestContext.CurrentContext.CancellationToken);
9595
}
9696

9797
[Test]
@@ -129,7 +129,7 @@ public async Task When_first_start_attempt_works_it_recovers_from_further_errors
129129

130130
await recoveredFromError.Task;
131131

132-
await dog.Stop();
132+
await dog.Stop(TestContext.CurrentContext.CancellationToken);
133133

134134
//Make sure failure is cleared
135135
Assert.That(lastFailure, Is.Null);

src/ServiceControl.Infrastructure/Watchdog.cs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ public class Watchdog
1717
ILog log;
1818
string taskName;
1919

20-
public Watchdog(string taskName, Func<CancellationToken, Task> ensureStarted,
21-
Func<CancellationToken, Task> ensureStopped, Action<string> reportFailure, Action clearFailure,
22-
TimeSpan timeToWaitBetweenStartupAttempts, ILog log)
20+
public Watchdog(
21+
string taskName,
22+
Func<CancellationToken, Task> ensureStarted,
23+
Func<CancellationToken, Task> ensureStopped, Action<string> reportFailure,
24+
Action clearFailure,
25+
TimeSpan timeToWaitBetweenStartupAttempts,
26+
ILog log
27+
)
2328
{
2429
this.taskName = taskName;
2530
this.ensureStopped = ensureStopped;
@@ -87,25 +92,27 @@ public Task Start(Action onFailedOnStartup)
8792
//Ignore, no need to log cancellation of delay
8893
}
8994
}
90-
try
91-
{
92-
log.Debug($"Stopping watching process {taskName}");
93-
//We don't pass the shutdown token here because it has already been cancelled and we want to ensure we stop the ingestion.
94-
await ensureStopped(CancellationToken.None).ConfigureAwait(false);
95-
}
96-
catch (Exception e)
97-
{
98-
log.Error($"Error while trying to stop {taskName}.", e);
99-
reportFailure(e.Message);
100-
}
10195
});
10296
return Task.CompletedTask;
10397
}
10498

105-
public Task Stop()
99+
public async Task Stop(CancellationToken cancellationToken)
106100
{
107-
shutdownTokenSource.Cancel();
108-
return watchdog;
101+
try
102+
{
103+
log.Debug($"Stopping watching process {taskName}");
104+
await shutdownTokenSource.CancelAsync().ConfigureAwait(false);
105+
await watchdog.ConfigureAwait(false);
106+
}
107+
catch (Exception e)
108+
{
109+
log.Error($"Error while trying to stop {taskName}.", e);
110+
throw;
111+
}
112+
finally
113+
{
114+
await ensureStopped(cancellationToken).ConfigureAwait(false);
115+
}
109116
}
110117
}
111118
}

src/ServiceControl/Operations/ErrorIngestion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public override async Task StopAsync(CancellationToken cancellationToken)
132132
{
133133
try
134134
{
135-
await watchdog.Stop();
135+
await watchdog.Stop(cancellationToken);
136136
channel.Writer.Complete();
137137
await base.StopAsync(cancellationToken);
138138
}

0 commit comments

Comments
 (0)