natemcmaster / CommandLineUtils

Command line parsing and utilities for .NET

Home Page:https://natemcmaster.github.io/CommandLineUtils/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ctrl+C does not stop synchronous Execute when called from the HostBuilder.RunCommandLineApplicationAsync

lucastheisen opened this issue · comments

Describe the bug
Our application has many subcommands, some of which are synch, some async. We use the HostBuilder to initialize all the DI objects and as such, RunCommandLineApplicationAsync appears to be running the application async regardless of whether or not the eventual commmand is sync or async. From what i have gathered on other related issues if run async, then application shutdown is waited on before exit after Ctrl+C. I am not explicitly supplying a cancellation token, but the CommandLineLifetime appears to prevent immediate shutdown, regardless

To Reproduce
Use HostBuilder

        public static async Task<int> Main(string[] args)
            => return await new HostBuilder()
                // more initialization here
                .RunCommandLineApplicationAsync<Program>(args)
                .ConfigureAwait(false);

With a synch subcommand

        protected int OnExecute(CommandLineApplication app)
            => longRunningSynchronousWork();

Start, then press Ctrl+C

Expected behavior
Ctrl+C immediately stops application

Additional context
This can be worked around with:

        protected int OnExecute(CommandLineApplication app)
        {
            Console.CancelKeyPress += (_, e) => e.Cancel = false;

            return longRunningSynchronousTask();
        }

Marking as help-wanted. If you have good way to fix this, feel free to send a pull request.

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please comment if you believe this should remain open, otherwise it will be closed in 14 days. Thank you for your contributions to this project.

Closing due to inactivity.
If you are looking at this issue in the future and think it should be reopened, please make a commented here and mention natemcmaster so he sees the notification.