Skip to content

CancelKeyPress not firing on coreclr on ubuntu #16088

Closed

Description

@pakrym

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

Activity

  1. changed the title [-]CancelKeyPress not workin[/-] [+]CancelKeyPress not firing on coreclr on ubuntu[/+] on Jan 12, 2016
  2. stephentoub commented on Jan 12, 2016

    @stephentoub
    Member

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

  3. pakrym commented on Jan 12, 2016

    @pakrym
    ContributorAuthor

    Updated issue with more information.

  4. stephentoub commented on Jan 12, 2016

    @stephentoub
    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?

  5. pakrym commented on Jan 12, 2016

    @pakrym
    ContributorAuthor

    Added WriteLine before read line, observe the same behavior.

  6. pakrym commented on Jan 12, 2016

    @pakrym
    ContributorAuthor

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

  7. stephentoub commented on Jan 12, 2016

    @stephentoub
    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

  8. davidfowl commented on Jan 12, 2016

    @davidfowl
    Member

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

  9. MaximRouiller commented on Jan 13, 2016

    @MaximRouiller

    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.

  10. removed their assignment
    on Jan 13, 2016
  11. TheRealPiotrP commented on Jan 14, 2016

    @TheRealPiotrP

    @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?

  12. stephentoub commented on Jan 14, 2016

    @stephentoub
    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.

  13. transferred this issue fromdotnet/corefxon Jan 31, 2020
  14. added this to the 1.0.0-rc2 milestone on Jan 31, 2020
  15. ghost locked as resolved and limited conversation to collaborators on Jan 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions