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

CancelKeyPress not firing on coreclr on ubuntu #16088

Closed
pakrym opened this issue Jan 12, 2016 · 11 comments
Closed

CancelKeyPress not firing on coreclr on ubuntu #16088

pakrym opened this issue Jan 12, 2016 · 11 comments

Comments

@pakrym
Copy link
Contributor

pakrym commented Jan 12, 2016

Code:

using System;
using System.IO;

namespace HelloMvc
{
    public class Program
    {
        public static void Main(string[] args)
        {
               System.Console.CancelKeyPress += (s,e) => System.Console.WriteLine("BBB");
               Console.ReadLine();
        }
    }
}

project.json:

{
    "version": "1.0.1-*",
    "compilationOptions": {
        "emitEntryPoint": true
    },
    "dependencies" : {
        "NETStandard.Library": "1.0.0-*"
    },
    "frameworks": {
        "dnxcore50": { }
    }
}

Steps:

  1. Compile
  2. Run
  3. Press Ctrl+C

Coreclr output:

<Empty>

Mono output:

BBB

Environment:

> uname -a
Linux pkrymets 3.19.0-31-generic dotnet/runtime#13856-Ubuntu SMP Wed Oct 7 15:04:02 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

> cat /usr/local/share/dotnet/cli/.version
ecda2a810615dda9f280fd8a87d23be2e2fcae45
1.0.0-dev-1452359345

Packages:

Package references:
  Microsoft.NETCore.Platforms 1.0.1-rc2-23712
  Microsoft.NETCore.Runtime 1.0.1-rc2-23712
  Microsoft.NETCore.Runtime.CoreCLR 1.0.1-rc2-23712
  Microsoft.NETCore.Runtime.Native 1.0.1-rc2-23712
  Microsoft.NETCore.Targets 1.0.1-rc2-23712
  Microsoft.NETCore.Targets.DNXCore 5.0.0-rc2-23712
  Microsoft.NETCore.Windows.ApiSets 1.0.1-rc2-23712
  Microsoft.Win32.Primitives 4.0.1-rc2-23712
* NETStandard.Library 1.0.0-rc2-23712
  NETStandard.Platform 1.0.0-rc2-23712
  System.AppContext 4.0.1-rc2-23712
  System.Collections 4.0.11-rc2-23712
  System.Collections.Concurrent 4.0.11-rc2-23712
  System.Console 4.0.0-rc2-23712
  System.Diagnostics.Debug 4.0.11-rc2-23712
  System.Diagnostics.Process 4.1.0-rc2-23712
  System.Diagnostics.Tools 4.0.1-rc2-23712
  System.Diagnostics.Tracing 4.0.21-rc2-23712
  System.Globalization 4.0.11-rc2-23712
  System.Globalization.Calendars 4.0.1-rc2-23712
  System.IO 4.1.0-rc2-23712
  System.IO.FileSystem 4.0.1-rc2-23712
  System.IO.FileSystem.Primitives 4.0.1-rc2-23712
  System.Linq 4.0.1-rc2-23712
  System.Net.Primitives 4.0.11-rc2-23712
  System.Net.Sockets 4.1.0-rc2-23712
  System.Private.Uri 4.0.1-rc2-23712
  System.Reflection 4.1.0-rc2-23712
  System.Reflection.Extensions 4.0.1-rc2-23712
  System.Reflection.Primitives 4.0.1-rc2-23712
  System.Reflection.TypeExtensions 4.1.0-rc2-23712
  System.Resources.ResourceManager 4.0.0
  System.Runtime 4.0.21-rc2-23712
  System.Runtime.Extensions 4.0.11-rc2-23712
  System.Runtime.Handles 4.0.1-rc2-23712
  System.Runtime.InteropServices 4.0.21-rc2-23712
  System.Runtime.InteropServices.RuntimeInformation 4.0.0-rc2-23712
  System.Runtime.Numerics 4.0.1-rc2-23712
  System.Text.Encoding 4.0.11-rc2-23712
  System.Text.Encoding.Extensions 4.0.11-rc2-23712
  System.Text.RegularExpressions 4.0.11-rc2-23712
  System.Threading 4.0.11-rc2-23712
  System.Threading.Tasks 4.0.11-rc2-23712
  System.Threading.Timer 4.0.1-rc2-23712

@muratg

@pakrym pakrym changed the title CancelKeyPress not workin CancelKeyPress not firing on coreclr on ubuntu Jan 12, 2016
@stephentoub
Copy link
Member

What build are you using? Can you share a minimal repro?

@pakrym
Copy link
Contributor Author

pakrym commented Jan 12, 2016

Updated issue with more information.

@stephentoub
Copy link
Member

I'm on Ubuntu 14.04. I just tried your repro building coreclr/corefx from the latest sources and using corerun. I also tried downloading dotnet-cli from the tarball at https://github.com/dotnet/cli and using dotnet build. In both cases, it produced the expected output, printing out BBB and then exiting.

@pallavit, are you able to repro this?

@pakrym, just to make sure you're not accidentally ctrl-c'ing before you've actually gotten to the registration, can you add a Console.WriteLine after the CancelKeyPress registration so you know that it's actually completed?

@pakrym
Copy link
Contributor Author

pakrym commented Jan 12, 2016

Added WriteLine before read line, observe the same behavior.

@pakrym
Copy link
Contributor Author

pakrym commented Jan 12, 2016

Updated to latest 1.0.0-dev-1452620519 dotnet, didn't help

@stephentoub
Copy link
Member

Spoke offline with @pakrym... turns out when building and then executing with dotnet, he does see the handler execute correctly. It's just when using dotnet run that the handler doesn't get invoked.

In trying it out locally, it looks like this is an issue specific to how dotnet run is designed/implemented, not with corefx or coreclr. The SIGINT signal from the ctrl-c press is getting sent to the dotnet process and killing it.

cc: @piotrpMSFT

@stephentoub
Copy link
Member

@davidfowl
Copy link
Member

Forget dotnet run for a minute, is there a way to handle SIGTERM in .NET Core? Or does that require pinvoking?

@MaximRouiller
Copy link

SIGTERM would be handled by libuv right? If I recall, libuv does not flow back that event back. Unless you have a custom version of libuv, that might be it.

@pallavit pallavit removed their assignment Jan 13, 2016
@TheRealPiotrP
Copy link
Contributor

@davidfowl it looks like we can receive SIGTERM via System.Console.CancelKeyPress. When we receive it we should pass it on from dotnet --> dotnet-run --> the app.

@stephentoub is there an API to send Ctrl+C to a spawned process?

@stephentoub
Copy link
Member

it looks like we can receive SIGTERM via System.Console.CancelKeyPress

Not unless some other code is registering for SIGTERM and sending another signal in its place. CancelKeyPress is for ConsoleSpecialKey.ControlC and ConsoleSpecialKey.ControlBreak, which are SIGINT and SIGQUIT, respectively. SIGTERM does not currently trigger a CancelKeyPress event.

is there an API to send Ctrl+C to a spawned process?

Not exposed in the framework. On Unix it can be achieved via a P/Invoke to kill, and on Windows via a P/Invoke to GenerateConsoleCtrlEvent I believe. There's an open issue requesting such a feature be added (https://github.com/dotnet/corefx/issues/1838), but there's also no reason it needs to be in corefx itself and could easily be done as a 3rd-party library.

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 1.0.0-rc2 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Jan 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants