Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: Repost: Can't sys.exit() from SIGTERM handler?

Reply
Thread Tools

RE: Repost: Can't sys.exit() from SIGTERM handler?

 
 
Andrew Athan
Guest
Posts: n/a
 
      01-06-2004

Yes, but it gets stranger (see below). It seems if I try to catch
exceptions from within the SIGTERM handler, they are instead caught by the
main program.

Perhaps this is because a second signal is raised from within the SIGTERM
handler and python doesn't handle nested signals well?

---------------------------------------------------------
So #1:

But first, the "simple" question: It seems really onerous to have to
explicitly trap SystemExit and re-raise everywhere in my program, just so I
can make sure that proper exit handlers (atexit(), etc.) are called. What
is the idiom to "force" and exit while still "properly" shutting down the
interpreter?

----------------------------------------------------------
#2
Now the hard question...

It seems my problem is caused by my atexit() handler. It is meant to kill
the children, and then os.waitpid() for them to make sure they are dead. I
added this because killing the python process was not reliably killing the
children (this surprised me since I thought the shell sent the signal to all
processes in a process group??!!! perhaps cdparanoia has some incorrect
exception handling which causes it not to reliably die on SIGTERM)

I attribute my woes to the atexit() handler because when I changed my code
to explicitly call the function I register with atexit() (called
killChildren()) from within my SIGTERM handler, then the behavior got even
weirder.

The code I have basically looks something like this (pseudocode)

childrenList=[]
def sigterm(a,b):
killChildren()
childrenList=[]
sys.exit()

def sigchild(a,b):
pass

def killChildren():
try:
os.kill(pid,signal.SIGTERM)
os.waitpid(pid,0)
pass:
pass

signal.signal(sigchild,signal.SIGCHILD)
signal.signal(sigterm,signal.SIGTERM)
childrenList.append(fork(...))
sys.atexit(killChildren)
....
while 1:
try:
foo=read()
except Exception,e:
print 'Here: %s'%e
pass

If I send a SIGTERM, then I occasionally get 'Here: [Errno 10] No child
processes' printed out!!!

Stranger still, if I leave the normal atexit() handler alone by doing this
instead:

def sigterm(a,b):
sys.exit()

then *no* exception is printed out and the program keeps running. I *still*
don't get anything printed even if I explicitly catch SystemExit within the
while 1: loop and print a debug statement.

The only conclusion I can draw is that (a) atexit's exception handling is
strange and/or (b) if the child is still running when I os.kill() it in in
the atexit() handler that occurs after the sys.exit() in, the SIGCHILD that
gets raised as a result of the kill() is interfering in some very strange
way.

HELP! Any comments?

Thanks,
A.

-----Original Message-----
From: Jeff Epler [(E-Mail Removed)]
Sent: Monday, January 05, 2004 8:37 PM
To: Andrew Athan
Cc: http://www.velocityreviews.com/forums/(E-Mail Removed)
Subject: Re: Repost: Can't sys.exit() from SIGTERM handler?


If I had to guess, I'd bet you had a blanket exception handler
somewhere. sys.exit merely raises SystemExit, and if this exception
propogates all the way back up, Python treats it specially by exiting
instead of printing the exception's traceback.

:r term.py
import os, signal, time, sys, traceback

def sigterm(a, b):
print "sigterm"
sys.exit()

signal.signal(signal.SIGTERM, sigterm)

os.system("(sleep 1; kill %s) &" % os.getpid())

print "Sleeping (should be killed)"

try:
time.sleep(2)
except SystemExit:
traceback.print_exc()
raise

print "sleep finished (!?)"

:r!python -u term.py

Sleeping (should be killed)
sigterm
Traceback (most recent call last):
File "term.py", line 14, in ?
time.sleep(2)
File "term.py", line 5, in sigterm
sys.exit()
SystemExit


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Child process does not exit after receiving SIGTERM Teresa C Programming 2 04-06-2004 01:26 AM
[SOLVED] RE: Repost: Can't sys.exit() from SIGTERM handler? Andrew Athan Python 0 01-06-2004 03:11 AM
Repost: Can't sys.exit() from SIGTERM handler? Andrew Athan Python 0 01-06-2004 01:27 AM
popen3,4 and SIGTERM Zoran Bosnjak Python 0 10-15-2003 03:01 PM
robust clean-up with SIGTERM (was Re: Again, how to write a cleanup function for a module in C ) Jane Austine Python 2 08-16-2003 04:01 AM



Advertisments