micah
micah

Reputation: 8096

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

Upvotes: 8

Views: 2512

Answers (2)

Collin McCarthy
Collin McCarthy

Reputation: 61

Slightly different from the OP, but I was just trying to debug my signal handlers with VSCode. I was able to do the following.

  • Set a breakpoint in the handler, function, e.g. kill here
  • Set a breakpoint where you call signal.signal(signal.SIGTERM, kill)
  • When the breakpoint at signal.signal() is hit, call os.getpid() in the debugger to get the PID
  • Resume the program (assuming it does some work, if not you can add time.sleep(10))
  • In a separate terminal, call kill <pid> with the PID obtained prior
  • The breakpoint in the signal handler function will be hit so you can step through it

Upvotes: 0

Oswin Noetzelmann
Oswin Noetzelmann

Reputation: 9555

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.

Upvotes: 4

Related Questions