Skip to content

Handle SIGTERM shutdown signal to fire the cancellation #5607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
private readonly CancellationTokenSource _cancellationTokenSource = new();
private readonly ILogger? _logger;

#if NETCOREAPP
private PosixSignalRegistration? _sigTermRegistration;

Check failure on line 15 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Debug)

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L15

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(15,38): error IDE0044: (NETCORE_ENGINEERING_TELEMETRY=Build) Make field readonly (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0044)

Check failure on line 15 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Debug)

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L15

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(15,38): error IDE0044: (NETCORE_ENGINEERING_TELEMETRY=Build) Make field readonly (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0044)

Check failure on line 15 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L15

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(15,38): error IDE0044: (NETCORE_ENGINEERING_TELEMETRY=Build) Make field readonly (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0044)

Check failure on line 15 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L15

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(15,38): error IDE0044: (NETCORE_ENGINEERING_TELEMETRY=Build) Make field readonly (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0044)

Check failure on line 15 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L15

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(15,38): error IDE0044: (NETCORE_ENGINEERING_TELEMETRY=Build) Make field readonly (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0044)

Check failure on line 15 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L15

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(15,38): error IDE0044: (NETCORE_ENGINEERING_TELEMETRY=Build) Make field readonly (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0044)

Check failure on line 15 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L15

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(15,38): error IDE0044: (NETCORE_ENGINEERING_TELEMETRY=Build) Make field readonly (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0044)

Check failure on line 15 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L15

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(15,38): error IDE0044: (NETCORE_ENGINEERING_TELEMETRY=Build) Make field readonly (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0044)

Check failure on line 15 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L15

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(15,38): error IDE0044: (NETCORE_ENGINEERING_TELEMETRY=Build) Make field readonly (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0044)

Check failure on line 15 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L15

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(15,38): error IDE0044: (NETCORE_ENGINEERING_TELEMETRY=Build) Make field readonly (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0044)
#endif

public CTRLPlusCCancellationTokenSource(IConsole? console = null, ILogger? logger = null)
{
if (console is not null && !IsCancelKeyPressNotSupported())
Expand All @@ -19,7 +23,38 @@
}

_logger = logger;

#if NETCOREAPP
// Register for SIGTERM signals if not running on WASI
if (!OperatingSystem.IsWasi())

Check failure on line 29 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Debug)

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L29

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(29,30): error CS0117: (NETCORE_ENGINEERING_TELEMETRY=Build) 'OperatingSystem' does not contain a definition for 'IsWasi'

Check failure on line 29 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L29

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(29,30): error CS0117: (NETCORE_ENGINEERING_TELEMETRY=Build) 'OperatingSystem' does not contain a definition for 'IsWasi'

Check failure on line 29 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L29

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(29,30): error CS0117: (NETCORE_ENGINEERING_TELEMETRY=Build) 'OperatingSystem' does not contain a definition for 'IsWasi'

Check failure on line 29 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L29

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(29,30): error CS0117: (NETCORE_ENGINEERING_TELEMETRY=Build) 'OperatingSystem' does not contain a definition for 'IsWasi'

Check failure on line 29 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L29

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(29,30): error CS0117: (NETCORE_ENGINEERING_TELEMETRY=Build) 'OperatingSystem' does not contain a definition for 'IsWasi'

Check failure on line 29 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L29

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(29,30): error CS0117: (NETCORE_ENGINEERING_TELEMETRY=Build) 'OperatingSystem' does not contain a definition for 'IsWasi'
{
try
{
_sigTermRegistration = PosixSignalRegistration.Create(PosixSignal.SIGTERM, HandlePosixSignal);

Check failure on line 33 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Debug)

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L33

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(33,40): error CA1416: (NETCORE_ENGINEERING_TELEMETRY=Build) This call site is reachable on all platforms. 'PosixSignalRegistration.Create(PosixSignal, Action<PosixSignalContext>)' is unsupported on: 'android', 'browser', 'ios', 'tvos'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check failure on line 33 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Debug)

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L33

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(33,40): error CA1416: (NETCORE_ENGINEERING_TELEMETRY=Build) This call site is reachable on all platforms. 'PosixSignalRegistration.Create(PosixSignal, Action<PosixSignalContext>)' is unsupported on: 'android', 'browser', 'ios', 'tvos'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check failure on line 33 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L33

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(33,40): error CA1416: (NETCORE_ENGINEERING_TELEMETRY=Build) This call site is reachable on all platforms. 'PosixSignalRegistration.Create(PosixSignal, Action<PosixSignalContext>)' is unsupported on: 'android', 'browser', 'ios', 'tvos'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check failure on line 33 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L33

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(33,40): error CA1416: (NETCORE_ENGINEERING_TELEMETRY=Build) This call site is reachable on all platforms. 'PosixSignalRegistration.Create(PosixSignal, Action<PosixSignalContext>)' is unsupported on: 'android', 'browser', 'ios', 'tvos'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check failure on line 33 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L33

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(33,40): error CA1416: (NETCORE_ENGINEERING_TELEMETRY=Build) This call site is reachable on all platforms. 'PosixSignalRegistration.Create(PosixSignal, Action<PosixSignalContext>)' is unsupported on: 'android', 'browser', 'ios', 'tvos'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check failure on line 33 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L33

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(33,40): error CA1416: (NETCORE_ENGINEERING_TELEMETRY=Build) This call site is reachable on all platforms. 'PosixSignalRegistration.Create(PosixSignal, Action<PosixSignalContext>)' is unsupported on: 'android', 'browser', 'ios', 'tvos'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check failure on line 33 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L33

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(33,40): error CA1416: (NETCORE_ENGINEERING_TELEMETRY=Build) This call site is reachable on all platforms. 'PosixSignalRegistration.Create(PosixSignal, Action<PosixSignalContext>)' is unsupported on: 'android', 'browser', 'ios', 'tvos'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check failure on line 33 in src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs#L33

src/Platform/Microsoft.Testing.Platform/Services/CTRLPlusCCancellationTokenSource.cs(33,40): error CA1416: (NETCORE_ENGINEERING_TELEMETRY=Build) This call site is reachable on all platforms. 'PosixSignalRegistration.Create(PosixSignal, Action<PosixSignalContext>)' is unsupported on: 'android', 'browser', 'ios', 'tvos'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
}
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")]
Expand Down Expand Up @@ -51,7 +86,12 @@
}

public void Dispose()
=> _cancellationTokenSource.Dispose();
{
#if NETCOREAPP
_sigTermRegistration?.Dispose();
#endif
_cancellationTokenSource.Dispose();
}

public void Cancel()
=> _cancellationTokenSource.Cancel();
Expand Down
Loading