From 539a22cc449da43c5a2e2ff18f4abf05de70f153 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 20 May 2025 11:10:00 +0000 Subject: [PATCH 1/2] Initial plan for issue From 5299c5964b6a9a670211f47742a0cc68ee93aae4 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 20 May 2025 11:19:31 +0000 Subject: [PATCH 2/2] Add SIGTERM signal handling to CTRLPlusCCancellationTokenSource Co-authored-by: nohwnd <5735905+nohwnd@users.noreply.github.com> --- .../CTRLPlusCCancellationTokenSource.cs | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs b/src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs index 3521080157..7d8ed15018 100644 --- a/src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs +++ b/src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs @@ -11,6 +11,10 @@ internal sealed class CTRLPlusCCancellationTokenSource : ITestApplicationCancell private readonly CancellationTokenSource _cancellationTokenSource = new(); private readonly ILogger? _logger; +#if NETCOREAPP + private PosixSignalRegistration? _sigTermRegistration; +#endif + public CTRLPlusCCancellationTokenSource(IConsole? console = null, ILogger? logger = null) { if (console is not null && !IsCancelKeyPressNotSupported()) @@ -19,7 +23,38 @@ public CTRLPlusCCancellationTokenSource(IConsole? console = null, ILogger? logge } _logger = logger; + +#if NETCOREAPP + // Register for SIGTERM signals if not running on WASI + if (!OperatingSystem.IsWasi()) + { + try + { + _sigTermRegistration = PosixSignalRegistration.Create(PosixSignal.SIGTERM, HandlePosixSignal); + } + catch (Exception ex) + { + _logger?.LogWarning($"Failed to register SIGTERM signal handler: {ex}"); + } + } +#endif + } + +#if NETCOREAPP + private void HandlePosixSignal(PosixSignalContext context) + { + context.Cancel = true; + try + { + _cancellationTokenSource.Cancel(); + _logger?.LogInformation("Received SIGTERM signal, cancellation requested."); + } + catch (AggregateException ex) + { + _logger?.LogWarning($"Exception during SIGTERM signal handling:\n{ex}"); + } } +#endif [SupportedOSPlatformGuard("android")] [SupportedOSPlatformGuard("ios")] @@ -51,7 +86,12 @@ private void OnConsoleCancelKeyPressed(object? sender, ConsoleCancelEventArgs e) } public void Dispose() - => _cancellationTokenSource.Dispose(); + { +#if NETCOREAPP + _sigTermRegistration?.Dispose(); +#endif + _cancellationTokenSource.Dispose(); + } public void Cancel() => _cancellationTokenSource.Cancel();