Skip to content

Commit

Permalink
fix: don't mask OperationCanceledException triggered by SIGINT (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
natemcmaster authored Dec 26, 2021
1 parent 6783c5f commit 3774688
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/CommandLineUtils/CommandLineApplication.Execute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public static async Task<int> ExecuteAsync<TApp>(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);
Expand Down
4 changes: 0 additions & 4 deletions src/CommandLineUtils/CommandLineApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/CommandLineUtils/releasenotes.props
Original file line number Diff line number Diff line change
Expand Up @@ -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)
</PackageReleaseNotes>
<PackageReleaseNotes Condition="$(VersionPrefix.StartsWith('3.1.'))">
Improvements:
Expand Down
16 changes: 10 additions & 6 deletions test/CommandLineUtils.Tests/CommandLineApplicationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1154,19 +1155,22 @@ public async Task AsyncTaskWithoutReturnIsAwaitedAsync()
await Assert.ThrowsAsync<InvalidOperationException>(async () => await run);
}

private class DelayTilCanceledProgram
{
public async Task OnExecuteAsync(CancellationToken ct)
{
await Task.Delay(-1, ct);
}
}

[Fact]
public async Task OperationCanceledReturnsExpectedOsCode()
{
var expectedCode = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? 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<string>());
var executeTask = CommandLineApplication.ExecuteAsync<DelayTilCanceledProgram>(testConsole, Array.Empty<string>());
testConsole.RaiseCancelKeyPress();
var exitCode = await executeTask;
Assert.Equal(expectedCode, exitCode);
Expand Down

0 comments on commit 3774688

Please sign in to comment.