Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crash in Python native extension module: Can interpreter fail gracefully?

I develop an interactive data analysis tool in which C++ high-performance kernels are wrapped via Cython and exposed as Python objects. This works very well in conjunction with IPython. However, a crash in the native code of the extension module (e.g. segmentation fault) will also crash the interpreter so that the entire session is lost. Is it possible to avoid this in any way? Could an error like a segmentation fault be caught as a proper Python exception?

like image 514
clstaudt Avatar asked Nov 27 '25 16:11

clstaudt


1 Answers

The faulthandler module (new in 3.3, but available for older versions) can print tracebacks on system-level errors.

Trying to handle the errors from Python code apparently doesn't work. At the C level, you can install a signal handler for SIGSEGV, but from the information I can find, what you can do there is quite limited. The basic message seems to be that you can shut down gracefully, but you can't recover from a segfault.

like image 181
Thomas K Avatar answered Nov 30 '25 14:11

Thomas K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!