Skip to content

CTRL+C stops working after importing the signal module #2102

@rmadsen-ks

Description

@rmadsen-ks
import signal

seems to break C# CTRL+C signal handling.

Environment

  • Pythonnet version:
  • Python version: 3.11
  • Operating System: Windows 11
  • .NET Runtime: 478

Details

I want to shut down the process by calling CTRL+C. That signal is intercepted from C# to call some additional logic.

However, it seems like after importing the signal module, the signal handler no longer works.

I was able to reproduce the issue modifying only the Pythonnet Console project the following way:

The following works:

            // Main() ...

            string[] cmd = Environment.GetCommandLineArgs();

            PythonEngine.Initialize(false, false);
            
            Py.Import("os"); // note, here the 'os' module is imported.
            Console.WriteLine("Waiting 5 sec. Verify if CTRL+C exits the application");
            System.Threading.Thread.Sleep(5000);
            int i = 0;
            // int i = Runtime.Py_Main(cmd.Length, cmd);
            PythonEngine.Shutdown();

            return i;

The following does not work:

            // Main() ...

            string[] cmd = Environment.GetCommandLineArgs();

            PythonEngine.Initialize();
            
            Py.Import("signal"); //here the signal  module gets imported.
            Console.WriteLine("Waiting 5 sec. Verify if CTRL+C exits the application");
            System.Threading.Thread.Sleep(5000);
            int i = 0;
            // int i = Runtime.Py_Main(cmd.Length, cmd);
            PythonEngine.Shutdown();

            return i;

This might be the same as: #1746

Except I have not been able to reproduce the issue with numpy.

This seems like something that if it cannot be fixed, at least it should be documented how to work around it.

This also sounds a bit like #449, but that is a different issue.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions