Skip to content

Commit 155b8cf

Browse files
committed
Be more selective about process shutdown signals
1 parent ed74e20 commit 155b8cf

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

src/SeqCli/Cli/Commands/Forwarder/RunCommand.cs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Diagnostics.CodeAnalysis;
1818
using System.IO;
1919
using System.Net;
20+
using System.Threading;
2021
using System.Threading.Tasks;
2122
using Autofac;
2223
using Autofac.Extensions.DependencyInjection;
@@ -206,23 +207,45 @@ static async Task<int> RunStandardIOAsync(ServerService service, TextWriter cout
206207
{
207208
service.Start();
208209

209-
try
210+
var waitForShutDownRequest = new TaskCompletionSource();
211+
var done = new ManualResetEventSlim(false);
212+
213+
void ShutDown()
210214
{
211-
Console.TreatControlCAsInput = true;
212-
var k = Console.ReadKey(true);
213-
while (k.Key != ConsoleKey.C || !k.Modifiers.HasFlag(ConsoleModifiers.Control))
214-
k = Console.ReadKey(true);
215+
waitForShutDownRequest.TrySetResult();
216+
done.Wait();
217+
}
215218

216-
cout.WriteLine("Ctrl+C pressed; stopping...");
219+
try
220+
{
217221
Console.TreatControlCAsInput = false;
222+
Console.CancelKeyPress += (_, _) =>
223+
{
224+
cout.WriteLine("Ctrl+C pressed; stopping...");
225+
Log.Information("Interrupt signal received");
226+
ShutDown();
227+
};
218228
}
219-
catch (Exception ex)
229+
catch (IOException)
220230
{
221-
Log.Debug(ex, "Console not attached, waiting for any input");
222-
Console.Read();
231+
Log.Information("Disabling Ctrl+C handling; process is non-interactive");
223232
}
224233

225-
await service.StopAsync();
234+
AppDomain.CurrentDomain.ProcessExit += (_, _) =>
235+
{
236+
Log.Information("Termination signal received");
237+
ShutDown();
238+
};
239+
240+
try
241+
{
242+
await waitForShutDownRequest.Task;
243+
await service.StopAsync();
244+
}
245+
finally
246+
{
247+
done.Set();
248+
}
226249

227250
return 0;
228251
}

test/SeqCli.EndToEnd/Support/CaptiveProcess.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public sealed class CaptiveProcess : ITestProcess, IDisposable
1515
readonly ManualResetEvent _outputComplete = new(false);
1616
readonly ManualResetEvent _errorComplete = new(false);
1717

18-
readonly object _sync = new();
18+
readonly Lock _sync = new();
1919
readonly StringWriter _output = new();
2020

2121
public CaptiveProcess(
@@ -27,7 +27,7 @@ public CaptiveProcess(
2727
string stopCommandFullExePath = null,
2828
string stopCommandArgs = null)
2929
{
30-
if (fullExePath == null) throw new ArgumentNullException(nameof(fullExePath));
30+
ArgumentNullException.ThrowIfNull(fullExePath);
3131
_captureOutput = captureOutput;
3232
_stopCommandFullExePath = stopCommandFullExePath;
3333
_stopCommandArgs = stopCommandArgs;

0 commit comments

Comments
 (0)