Skip to content

Third party code may remove cancel keypress handler #30464

Open

Description

@paralaxsd

Hi,
I have an issue with the way the Console.CancelKeyPress event is implemented:

In my scenario I'm running a vanilla ASP.Net Core 2.2 web application. The web application internally adds a ctrl-c handler and gracefully shuts down the application when ctrl-c is pressed.

Now I had to add some logic to the application that's run in a Java VM via JNI. It seems that initializing JNI registers a SIGINT handler that throws away the console ctrl-c handler registered by ASP.Net.
Pressing ctrl-c when JNI is initialized immediately exits the process even leaving zombie processes behind on Linux.

I looked into the implementation of Console.CancelKeyPress and found that it uses an internal helper type ConsolePal.ControlCHandlerRegistrar that's only registering a ctrl-c handler on the first usage of event addition. On subsequent event additions only the cancel callbacks are chained together.

In my scenario this means that after JNI init I can no longer use Console.CancelKeyPress because it will not re-register the handler and throw the JNI handler out.

I have a workaround for this issue but it depends on private reflection. It sets Console.s_registrar to null and registers a dummy ctrl-c handler in order to throw out the JNI handler. Naturally this approach can break any day when the implementation changes and so I'd appreciate any help in this matter.

var consoleType = typeof(Console);
var registrar = consoleType.GetField("s_registrar", BindingFlags.Static | BindingFlags.NonPublic);

if (registrar != null)
{
	registrar.SetValue(null, null);

	void Dummy(object o, ConsoleCancelEventArgs e) { }
	Console.CancelKeyPress += Dummy;
}

Thanks!

Edit: Unfortunately the workaround above does not work on Linux. I've validated it on Windows with dotnet core 2.2.101.

Edit 2: I found that on Linux a workaround for this issue is to retrieve the current SIGINT handler before a problematic native call and restore if afterwards. E.g.:

#include <signal.h>
// ...
struct sigaction oldAct = {0};
sigaction(SIGINT, nullptr, &oldAct);

// a third party call that changes the SIGINT handler
const long flag = JNI_CreateJavaVM(&vm, reinterpret_cast<void**>(&env), &vmArgs);

sigaction(SIGINT, &oldAct, nullptr);

Activity

  1. transferred this issue fromdotnet/corefxon Feb 1, 2020
  2. added this to the 5.0 milestone on Feb 1, 2020
  3. added
    untriaged
    on Feb 23, 2020
  4. adamsitnik commented on Jun 24, 2020

    @adamsitnik
    Member

    Hi @paralaxsd

    Thank you for detailed report!

    I am not familiar with JNI and I've one question: is it running within the same process or a different process on the same VM?

    Thanks,
    Adam

  5. modified the milestones: 5.0.0, Future on Jun 24, 2020
  6. paralaxsd commented on Jun 24, 2020

    @paralaxsd
    Author

    I am not familiar with JNI and I've one question: is it running within the same process or a different process on the same VM?

    Hi Adam,

    In the scenario I've described a JNI VM was created in-process via PInvoke. Initializing JNI is really just an example of any piece of (third party) native code replacing the ctrl-c handler which leaves Console.CancelKeyPress unable to continue working properly afterwards.

  7. removed
    untriaged
    on Jul 6, 2020
  8. modified the milestones: Future, 6.0.0 on Aug 14, 2020
  9. modified the milestones: 6.0.0, 7.0.0 on Jul 26, 2021
  10. modified the milestones: 7.0.0, Future on Jul 9, 2022
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions