-
Notifications
You must be signed in to change notification settings - Fork 2
fix(runtime): cancel standalone runs on process signals #80
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
Open
autocarl
wants to merge
10
commits into
yllibed:main
Choose a base branch
from
autocarl:agent/issue-79-signal-handling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d7f52f6
fix(runtime): cancel standalone runs on SIGTERM
autocarl 5d5ae42
fix(runtime): coordinate Ctrl+C signal ownership
autocarl 726dd04
fix(runtime): harden process signal coordination
autocarl 48c5175
docs: define process signal ownership contract
autocarl 00ef1be
fix(signals): harden process ownership contracts
autocarl 3a4b0ce
fix(signals): respect mobile and Unix platform limits
autocarl d07d64c
fix(signals): degrade on registration failure and default the mode to…
carldebilly 59b5684
fix(ci): restore before building in the process-signal stress script
carldebilly c57fa29
docs: diagram the process-signal ownership and epoch mechanics
carldebilly 09a64b9
fix(signals): make the test isolation scope restore a usable coordinator
carldebilly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace Repl; | ||
|
|
||
| internal sealed class ProcessSignalCancellationScope : IAsyncDisposable | ||
| { | ||
| private const int SigIntExitCode = 130; | ||
| private const int SigTermExitCode = 143; | ||
|
|
||
| private readonly CancellationTokenSource _signalCancellation = new(); | ||
| private readonly CancellationTokenSource _linkedCancellation; | ||
| private readonly PosixSignalRegistration? _sigTermRegistration; | ||
| private readonly Lock _gate = new(); | ||
| private Task _cancellationTask = Task.CompletedTask; | ||
| private int _exitCode; | ||
|
autocarl marked this conversation as resolved.
Outdated
|
||
| private bool _disposed; | ||
|
|
||
| public ProcessSignalCancellationScope(CancellationToken cancellationToken) | ||
| { | ||
| _linkedCancellation = CancellationTokenSource.CreateLinkedTokenSource( | ||
| cancellationToken, | ||
| _signalCancellation.Token); | ||
|
|
||
| var cancelKeyRegistered = false; | ||
| try | ||
| { | ||
| Console.CancelKeyPress += HandleCancelKey; | ||
| cancelKeyRegistered = true; | ||
| if (!OperatingSystem.IsWindows()) | ||
| { | ||
| _sigTermRegistration = PosixSignalRegistration.Create( | ||
| PosixSignal.SIGTERM, | ||
| HandleSigTerm); | ||
| } | ||
| } | ||
| catch | ||
| { | ||
| if (cancelKeyRegistered) | ||
| { | ||
| Console.CancelKeyPress -= HandleCancelKey; | ||
| } | ||
|
|
||
| _linkedCancellation.Dispose(); | ||
| _signalCancellation.Dispose(); | ||
| throw; | ||
| } | ||
| } | ||
|
|
||
| public CancellationToken Token => _linkedCancellation.Token; | ||
|
|
||
| public int ExitCode => Volatile.Read(ref _exitCode); | ||
|
|
||
| public async ValueTask DisposeAsync() | ||
| { | ||
| Task cancellationTask; | ||
| lock (_gate) | ||
| { | ||
| if (_disposed) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| _disposed = true; | ||
| cancellationTask = _cancellationTask; | ||
| } | ||
|
|
||
| Console.CancelKeyPress -= HandleCancelKey; | ||
| _sigTermRegistration?.Dispose(); | ||
| try | ||
| { | ||
| #pragma warning disable VSTHRD003 // The OS signal callback starts this task; disposal must observe its completion. | ||
| await cancellationTask.ConfigureAwait(false); | ||
| #pragma warning restore VSTHRD003 | ||
| } | ||
| finally | ||
| { | ||
| _linkedCancellation.Dispose(); | ||
| _signalCancellation.Dispose(); | ||
| } | ||
| } | ||
|
|
||
| internal void HandleCancelKey(object? sender, ConsoleCancelEventArgs context) | ||
| { | ||
| _ = sender; | ||
| if (context.SpecialKey != ConsoleSpecialKey.ControlC | ||
| || CancelKeyHandler.HasActiveConsoleHandler) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| lock (_gate) | ||
| { | ||
| if (_disposed || _exitCode != 0 || CancelKeyHandler.HasActiveConsoleHandler) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| _exitCode = SigIntExitCode; | ||
| context.Cancel = true; | ||
| _cancellationTask = _signalCancellation.CancelAsync(); | ||
| } | ||
| } | ||
|
|
||
| private void HandleSigTerm(PosixSignalContext context) | ||
| { | ||
| lock (_gate) | ||
| { | ||
| if (_disposed || _exitCode != 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| _exitCode = SigTermExitCode; | ||
| context.Cancel = true; | ||
| _cancellationTask = _signalCancellation.CancelAsync(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| namespace Repl; | ||
|
|
||
| /// <summary> | ||
| /// Controls whether standalone runs translate process termination signals into cooperative cancellation. | ||
| /// </summary> | ||
| public enum ProcessSignalHandlingMode | ||
| { | ||
| /// <summary> | ||
| /// Standalone <see cref="ReplApp.Run(string[], ReplRunOptions?)"/> and | ||
| /// <see cref="ReplApp.RunAsync(string[], ReplRunOptions?, CancellationToken)"/> calls | ||
| /// handle Ctrl+C/SIGINT for the duration of the run and also handle SIGTERM on Unix platforms. Interactive sessions retain their existing Ctrl+C behavior. | ||
| /// </summary> | ||
| Automatic = 0, | ||
|
|
||
| /// <summary> | ||
| /// Process signal handling remains the responsibility of the caller. | ||
| /// </summary> | ||
| None = 1, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.