diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d6f564f..0b077aa0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,9 +15,11 @@ * [@natemcmaster]: fix new CI to correctly publish symbols to nuget.org * [@scott-xu]: show option types in help text when OptionAttribute.Template is set ([#429]) * [@skirchner989]: change to not throw when a validator is not of type AttributeValidator ([#431]) +* [@natemcmaster]: don't mask OperationCanceledException triggered by SIGINT ([#483]) [#429]: https://github.com/natemcmaster/CommandLineUtils/pull/429 [#431]: https://github.com/natemcmaster/CommandLineUtils/pull/431 +[#484]: https://github.com/natemcmaster/CommandLineUtils/pull/484 ### Other diff --git a/src/CommandLineUtils/CommandLineApplication.Execute.cs b/src/CommandLineUtils/CommandLineApplication.Execute.cs index 78fff95d..7c2ea98b 100644 --- a/src/CommandLineUtils/CommandLineApplication.Execute.cs +++ b/src/CommandLineUtils/CommandLineApplication.Execute.cs @@ -78,6 +78,10 @@ public static async Task ExecuteAsync(CommandLineContext context, Can app.Conventions.UseDefaultConventions(); return await app.ExecuteAsync(context.Arguments, cancellationToken); } + catch (OperationCanceledException) + { + return s_exitCodeOperationCanceled; + } catch (CommandParsingException ex) { context.Console.Error.WriteLine(ex.Message); diff --git a/src/CommandLineUtils/CommandLineApplication.cs b/src/CommandLineUtils/CommandLineApplication.cs index 7987e8a0..629c0165 100644 --- a/src/CommandLineUtils/CommandLineApplication.cs +++ b/src/CommandLineUtils/CommandLineApplication.cs @@ -881,10 +881,6 @@ void cancelHandler(object o, ConsoleCancelEventArgs e) return await command._handler(handlerCancellationTokenSource.Token); } - catch (OperationCanceledException) - { - return s_exitCodeOperationCanceled; - } finally { _context.Console.CancelKeyPress -= cancelHandler; diff --git a/src/CommandLineUtils/releasenotes.props b/src/CommandLineUtils/releasenotes.props index 442c1718..7ecb2ff5 100644 --- a/src/CommandLineUtils/releasenotes.props +++ b/src/CommandLineUtils/releasenotes.props @@ -14,6 +14,7 @@ Fixes: * @natemcmaster: fix new CI to correctly publish symbols to nuget.org * @scott-xu: show option types in help text when OptionAttribute.Template is set (#429) * @skirchner989: change to not throw when a validator is not of type AttributeValidator (#431) +* @natemcmaster: don't mask OperationCanceledException triggered by SIGINT (#483) Improvements: diff --git a/test/CommandLineUtils.Tests/CommandLineApplicationTests.cs b/test/CommandLineUtils.Tests/CommandLineApplicationTests.cs index ee34b9ee..9b93e45d 100644 --- a/test/CommandLineUtils.Tests/CommandLineApplicationTests.cs +++ b/test/CommandLineUtils.Tests/CommandLineApplicationTests.cs @@ -7,6 +7,7 @@ using System.IO; using System.Linq; using System.Runtime.InteropServices; +using System.Threading; using System.Threading.Tasks; using Moq; using Xunit; @@ -1154,6 +1155,14 @@ public async Task AsyncTaskWithoutReturnIsAwaitedAsync() await Assert.ThrowsAsync(async () => await run); } + private class DelayTilCanceledProgram + { + public async Task OnExecuteAsync(CancellationToken ct) + { + await Task.Delay(-1, ct); + } + } + [Fact] public async Task OperationCanceledReturnsExpectedOsCode() { @@ -1161,12 +1170,7 @@ public async Task OperationCanceledReturnsExpectedOsCode() ? unchecked((int)0xC000013A) : 130; var testConsole = new TestConsole(_output); - var app = new CommandLineApplication(testConsole); - app.OnExecuteAsync(async ct => - { - await Task.Delay(-1, ct); - }); - var executeTask = app.ExecuteAsync(Array.Empty()); + var executeTask = CommandLineApplication.ExecuteAsync(testConsole, Array.Empty()); testConsole.RaiseCancelKeyPress(); var exitCode = await executeTask; Assert.Equal(expectedCode, exitCode);