5

I want to create a program that does something in which someone terminates the script by clicking the stop button in PyCharm. I tried

from sys import exit

def handler(signal_received, frame):
    # Handle any cleanup here
    print('SIGINT or CTRL-C detected. Exiting gracefully')
    exit(0)


if __name__ == '__main__':
    signal(SIGINT, handler)

    print('Running. Press CTRL-C to exit.')
    while True:
        # Do nothing and hog CPU forever until SIGINT received.
        pass

from https://www.devdungeon.com/content/python-catch-sigint-ctrl-c.

I tried on both Mac and Windows. On the Mac, PyCharm behaved as expected, when I click the stop button it catches the SIGINT. But on Windows, I did exactly the same thing, but it just straightly returns to me a Process finished with exit code -1. Is there something I can do to change to make the Windows behave like what on Mac?

Any help is appreciated!

Luke Yang
  • 53
  • 3
  • 1
    Your question is strange. PyCharm is assumed to be used by developers, not to run programs for generic users, so I would be surprised that stop button is not working as expected (for developers). I think you should reframe, and not using PyCharm as shell. -- In any cases, signals are OS specific, SIGINT convention come from UNIX, so probably windows doesn't has it, and some SIGKILL is used instead (SIGKILL is not cancellable on POSIX systems). Note: https://youtrack.jetbrains.com/issue/PY-13316 : "Stop action does not send SIGINT" – Giacomo Catenazzi Sep 03 '20 at 10:03
  • 1
    I disagree, its a perfectly sound question as the full lifecycle of a program should be intentional including shutdown actions. Its not possible to know the behavior of your program if you cannot exercise the case - but your follow up information is very helpful! Thank you for that. – LeanMan Feb 28 '21 at 00:40

2 Answers2

4

I don't think it's a strange question at all. On unix systems, pycham sends a SIGTERM, waits one second, then send a SIGKILL. On windows, it does something else to end the process, something that seems untrappable. Even during development you need a way to cleanly shut down a process that uses native resources. In my case, there is a CAN controller that, if not shut down properly, can't ever be opened again. My work around was to build a simple UI with a stop button that shuts the process down cleanly. The problem is, out of habit, from using pycharm, goland, and intellij, is to just hit the red, square button. Every time I do that I have to reboot the development system. So I think it is clearly also a development time question.

wz2b
  • 1,017
  • 7
  • 24
  • Yea I agree. I'm quite surprised there isn't a more development friendly solution to this problem. Its quite paramount. – LeanMan Feb 28 '21 at 00:37
3

This actually isnt a simple thing, because PyCharm sends SIGKILL with the stop button. Check the discussion here https://youtrack.jetbrains.com/issue/PY-13316

There is a comment that you can enable "kill windows process softly", however it didnt work for me. The one that does work is emulate terminal in the debug config, then use control c when you select the console window

Elixir
  • 31
  • 2
  • have you tried to follow this: https://stackoverflow.com/a/45220708/10421103 ? – LeanMan Feb 28 '21 at 00:45
  • Update: I have tried the instructions and it does work for me. – LeanMan Feb 28 '21 at 00:57
  • I tried those instructions, seems to tbe the same as another way of enabling "kill windows process softly" .. it doesnt work for me (on windows). Although it might be because my application is threaded... it shouldnt though since they are daemon threads? Hmm... – Elixir Mar 01 '21 at 03:15
  • I for some reason feel threading won't change the behavior here as a python process is single threaded. – LeanMan Mar 03 '21 at 01:00