Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python VS Code Debug - Capture SIGTERM?

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
like image 516
micah Avatar asked Nov 15 '18 17:11

micah


People also ask

How do I debug Python code in VS Code?

Basic debugging If you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File in Terminal.

How do I debug Python code using PDB?

To start debugging within the program just insert import pdb, pdb. set_trace() commands. Run your script normally, and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace().

Is VS Code good for debugging?

One of the key features of Visual Studio Code is its great debugging support. VS Code's built-in debugger helps accelerate your edit, compile, and debug loop.

Why is my VS Code debugger not working?

Debugger not working Look at the debugger console for any error messages displayed. Look at the Debugger Tools console output for any errors. Remember to re-start VS Code once done (this won't be necessary in a future release). Solution: Clear all expressions from the debugger Watch window and start debugging again.


1 Answers

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.

like image 94
Oswin Noetzelmann Avatar answered Oct 01 '22 09:10

Oswin Noetzelmann