[issue8021] sys.exit() doesn't execute inside a signal handler while blocked inside an try/except

David Schere report at bugs.python.org
Thu Feb 25 23:47:39 CET 2010


New submission from David Schere <dschere at arinc.com>:

When doing an exit() within a signal handler for an alarm I am seeing something strange:


This code works, it exits within signal handler as expected. You never see the print statement executed.
import signal
import time
import sys
import traceback

def handler( *args ):
   sys.exit(-1) #<-- terminate program


signal.signal( signal.SIGALRM, handler ) 
# set alarm to go off in one second that calls handler()
signal.alarm( 1 )


time.sleep( 3 )


print 'You should never see this message'


This code results in sys.exit() begin ignored and the code inside the “except:” block being executed!

import signal
import time
import sys
import traceback

def handler( *args ):
   sys.exit(-1) #<-- terminate program


signal.signal( signal.SIGALRM, handler ) 
# set alarm to go off in one second that calls handler()
signal.alarm( 1 )

try:
   time.sleep( 3 )
except:
   print 'This message should not be seen'
   sys.exit()
   
print 'You should never see this message'


The work around is to raise an exception inside the 
handler() function.

----------
messages: 100120
nosy: david_schere
severity: normal
status: open
title: sys.exit() doesn't execute inside a signal handler while blocked inside an try/except
type: behavior
versions: Python 2.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8021>
_______________________________________


More information about the Python-bugs-list mailing list