Skip to content

OperationCanceledException should not be treated as an error #430

@jnm2

Description

@jnm2

If you use a command handler that is cancellable:

CommandHandler.Create<IConsole, CancellationToken>(async (console, cancellationToken) =>
{
    while (true)
    {
        await Task.Delay(1000, cancellationToken).ConfigureAwait(false);
        console.Out.WriteLine("Still alive");
    }
}

There's a good chance in general that it will cancel by throwing TaskCanceledException or OperationCanceledException, since the TPL is designed around throwing these. System.Threading.Tasks.Task goes so far as to recognize these exceptions as cancellation rather than failure.

However, System.CommandLine treats it as though it was like any other exception. That causes this unpleasantness when the user presses Ctrl+C.

image

This exception should never be unhandled so long as the cancellation token has actually been set, since System.CommandLine should be expecting it.

(OperationCanceledException.CancellationToken should be ignored because there are plenty of APIs that lose the cancellation token by having to use TaskCompletionSource.SetCanceled or CancellationTokenSource.CreateLinkedTokenSource in their implementation.)

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinghelp wantedExtra attention is needed

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions