Skip to content
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

Gracefully handling Ctrl+C ignores Program Main exit code. #27595

Closed
NTaylorMullen opened this issue Oct 10, 2018 · 4 comments
Closed

Gracefully handling Ctrl+C ignores Program Main exit code. #27595

NTaylorMullen opened this issue Oct 10, 2018 · 4 comments
Milestone

Comments

@NTaylorMullen
Copy link

Repro:

  • dotnet new console
  • Paste the repro code below into Program.cs
  • dotnet run
  • Hit Ctrl + C
  • Look at the last exit code.

image

We expect in the below repo for the exit code to be 1337

Built a repro that's inspired by ASP.NET's hosting bits here: https://github.com/aspnet/Hosting/blob/88d33cd665736e9c73298d24901c9dc99607d414/src/Microsoft.AspNetCore.Hosting/WebHostExtensions.cs#L134-L162

Repro:

using System;
using System.Threading;
using System.Threading.Tasks;

namespace CtrlCExit
{
    class Program
    {
        static int Main(string[] args)
        {
            var done = new ManualResetEventSlim(false);
            var token = new CancellationTokenSource().Token;
            using (var cts = CancellationTokenSource.CreateLinkedTokenSource(token))
            {
                AttachCtrlcSigtermShutdown(cts, done, shutdownMessage: "Cancel key press received, attempting to exit gracefully.");

                try
                {
                    Console.WriteLine("Waiting for Ctrl+C to be pressed...");
                    cts.Token.WaitHandle.WaitOne();
                }
                finally
                {
                    done.Set();
                }
            }

            Console.WriteLine("Exited gracefully.");
            return 1337;
        }

        private static void AttachCtrlcSigtermShutdown(CancellationTokenSource cts, ManualResetEventSlim resetEvent, string shutdownMessage)
        {
            void Shutdown()
            {
                if (!cts.IsCancellationRequested)
                {
                    if (!string.IsNullOrEmpty(shutdownMessage))
                    {
                        Console.WriteLine(shutdownMessage);
                    }
                    try
                    {
                        cts.Cancel();
                    }
                    catch (ObjectDisposedException) { }
                }

                // Wait on the given reset event
                resetEvent.Wait();
            };

            AppDomain.CurrentDomain.ProcessExit += (sender, eventArgs) => Shutdown();
            Console.CancelKeyPress += (sender, eventArgs) =>
            {
                Shutdown();
                // Don't terminate the process immediately, wait for the Main thread to exit gracefully.
                eventArgs.Cancel = true;
            };
        }
    }
}

/cc @halter73 @Tratcher

@danmoseley
Copy link
Member

I repro this, and indeed on .NET Framework it produces 1337

@danmoseley
Copy link
Member

danmoseley commented Oct 10, 2018

-1073741510 (0xC000013A) is STATUS_CTRL_C_EXIT

// {Application Exit by CTRL+C}
// The application terminated as a result of a CTRL+C.
//
#define STATUS_CONTROL_C_EXIT ((NTSTATUS)0xC000013AL)

@danmoseley
Copy link
Member

@jeffschwMSFT I didn't debug this, but I'm thinking it's either runtime/hosting model or CLI?

@jkotas
Copy link
Member

jkotas commented Oct 11, 2018

The problem is that dotnet run process itself is being terminated by Ctrl+C and exiting. It does not repro if you run the repro directly without the intermediate process, like dotnet bin\Debug\netcoreapp3.0\repro.dll.

This is CLI problem. Looks like a dup of https://github.com/dotnet/cli/issues/812 - see comment at https://github.com/dotnet/corefx/issues/5339#issuecomment-171049771

@jkotas jkotas closed this as completed Oct 11, 2018
@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 3.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants