How to troubleshoot hanging script?

K

kj

Hi! I have a Pythonoob question.

I have a script that hangs indefinitely at random times; the only
thing to do at this point is to kill it.

I'm looking for suggestions on how to troubleshoot and debug the
problem.

I'm not even sure of where exactly the script is hanging, though
I suspect it is right around when it tries to read from a pipe
(generated by popen). (I arrived at this guess by putting print
statements all over the place, but I'm not sure this is a very
reliable way to pinpoint the error.)

So the first thing I'd like to do is find out exactly where the
script is hanging. It would be nice if I could just hit Ctrl-C
(which sends SIGINT from the terminal) when the script is hanging,
to kill it and get a backtrace, but when this script hangs it
becomes unresponsive to Ctrl-C! The only way to kill it is with
something like

% pkill -KILL my_script.py

or even

% pkill -TERM my_script.py

....or -ABRT or -QUIT. I tried to exploit this by adding this to the
script:

import signal

def term_handler(signum, frame):
raise KeyboardInterrupt

signal.signal(signal.SIGTERM, term_handler)

....but this did not help at all; in fact, after this addition, the script
no longer responded to pkill -TERM.

TIA!

Kynn
 
A

Alexandru Palade

Hi,

Just a thought, I'm not sure it's the best way.
You can start an alarm just before the line you *think* it's wrong and
cancel it after that. If it's activated then you probably have
pinpointed the location. Have a look at the signals module
http://docs.python.org/lib/module-signal.html, the alarm function.

Hope it helps.
 
B

Bruno Desthuilliers

kj a écrit :
Hi! I have a Pythonoob question.

I have a script that hangs indefinitely at random times; the only
thing to do at this point is to kill it.

I'm looking for suggestions on how to troubleshoot and debug the
problem.

I'm not even sure of where exactly the script is hanging, though
I suspect it is right around when it tries to read from a pipe
(generated by popen). (I arrived at this guess by putting print
statements all over the place, but I'm not sure this is a very
reliable way to pinpoint the error.)

So the first thing I'd like to do is find out exactly where the
script is hanging. It would be nice if I could just hit Ctrl-C
(which sends SIGINT from the terminal) when the script is hanging,
to kill it and get a backtrace, but when this script hangs it
becomes unresponsive to Ctrl-C!

Pretty often, this kind of behaviour is related to a catch-all (or at
least a 'catch-too-much') except clause that doesn't re-raise.


wrt/ debugging, there's a command-line debugger named pdb in the stdlib.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top