9

I'm dealing with a python-written server that locks up, and stops working, including logging. I wonder if there's a python equivalent to java's "kill -3" signal that at least prints the current stacktrace.

spike
  • 91
  • 1
  • 2
  • 2
    `kill -2` sends SIGINT, which at least on Linux seems to translate into a `KeyboardInterrupt` exception. That might cause a stack trace to be dumped somewhere. It depends on the server of course. – Jason Orendorff Jan 29 '10 at 22:36

3 Answers3

7

Use the faulthandler module. https://pypi.python.org/pypi/faulthandler/

import faulthandler
faulthandler.register(signal.SIGUSR1)

This works outside of Python's interpreter loop's signal handling at the C level so it will work even when the Python interpreter itself is hung waiting on something else.

See also: http://docs.python.org/dev/library/faulthandler

gps
  • 1,360
  • 12
  • 12
2
import signal, traceback
def quit_handler(signum,frame):
    traceback.print_stack()
signal.signal(signal.SIGQUIT,quit_handler)
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • If the server "locks up", it doesn't necessarily get a SIGQUIT signal, I guess. – AndiDog Jan 29 '10 at 21:35
  • @AndiDog: I think you're supposed to use `kill(1)` to send it one when you manually detect that it has locked up. – SamB Nov 21 '10 at 18:24
0

You can find a (Unix-only) solution in this question.

Community
  • 1
  • 1
AndiDog
  • 68,631
  • 21
  • 159
  • 205