[Python-Dev] Trap SIGSEGV and SIGFPE

Victor Stinner victor.stinner at haypocalc.com
Wed Dec 10 19:37:16 CET 2008


Oh, I forgot the issue URL:
   http://bugs.python.org/issue3999

I also attached an example of catching segfaults.

> > I published a new version of my fault handler: it installs an handler for
> > signals SIGFPE and SIGSEGV. Using it, it's possible to catch them and
> > continue the execution of your Python program. Example:
>
> This will of course leave the program in an undefined state.  It is
> very likely to crash again, emit garbage, hang, or otherwise be
> useless.

Recover after a segfault is dangerous, but my first goal was to get the Python 
backtrace instead just one line: "Segmentation fault". It helps a lot for 
debug!

I didn't try on real world application, but with a small script the program 
continues its execution without any problem.

But yes, there is a big risk of:
 - leak memory 
 - deadlock
 - context problem, eg. for the GIL, I call PyGILState_Ensure()
 - etc.

I choosed the exceptions MemoryError and ArithmeticError, but we could use 
specific exceptions based on BaseException instead of Exception to avoid 
catching them with "except Exception: ...".

-- 
Victor Stinner aka haypo
http://www.haypocalc.com/blog/


More information about the Python-Dev mailing list