8

I'd like to force sys.exit() when the python debugger is stopped. When I stop the debugger I see Terminated: 15 so I assume this is SIGTERM. However, when stopping the debugger, my kill function isn't called.

def kill(sig, frame):
  sys.exit(0)

signal.signal(signal.SIGINT, kill)
signal.signal(signal.SIGTERM, kill)

When stopping the vscode debugger, what signal is sent?

Edit:

Just tried all of them. No love

for s in signal.Signals:
  try:
    signal.signal(s, self._kill)
  except:
    pass
micah
  • 7,596
  • 10
  • 49
  • 90
  • `SIGHUP` or `SIGSEGV` or `SIGSTOP`? – Torxed Nov 15 '18 at 18:05
  • @Torxed no luck – micah Nov 15 '18 at 18:06
  • How about [this](https://stackoverflow.com/questions/2148888/python-trap-all-signals) to see if anything triggers? Edited my first comment, I think it's `SIGSTOP` if anything. – Torxed Nov 15 '18 at 18:07
  • Have you added a print statement or something to your kill function, or tried executing it with `python -m trace --trace script.py` if that's possible through the debugger you're using? Just to see if any trace of signal handling is occuring? I honestly have no idea what Visual Studio Code is doing and I imagine there's not a lot of documentation on the matter either (seeing as it's pretty new after all). – Torxed Nov 15 '18 at 18:25

1 Answers1

4

For now we seem to be OOL (out of luck) - I ran into the same issue and found that VS Code python extension does issue a SIGKILL on debug stop, which cannot be cought.

Unlike the node.js extenstion, the Python extension also does not support setting the type to SIGTERM or SIGINT.

The only workaround I found is to have an open terminal (type: Pythen Debug Terminal) in VS Code. It should show the python command behavior and output during debug. Bring the terminal into focus by clicking on it and press ctrl-C manually. This should stop the debugged program gracefully and your catching the SIGTERM or SIGINT will work.

Oswin Noetzelmann
  • 9,166
  • 1
  • 33
  • 46
  • 1
    I tried the solution you suggest without luck. The Ctrl-C is not captured and my Pytrhon program continues its execution. – TommasoF Nov 29 '20 at 10:09
  • This is a big bug. This doesn't allow temporary dirs/files to be cleaned up either. – four43 Feb 07 '22 at 18:56