How to debug a Python segmentation fault? How to debug a Python segmentation fault? python python

How to debug a Python segmentation fault?


I got to this question because of the Segmentation fault, but not on exit, just in general, and I found that nothing else helped as effectively as faulthandler. It's part of Python 3.3, and you can install in 2.7 using pip.


tl;dr for python3 users.

Firstly, from the docs:

faulthandler is a builtin module since Python 3.3

Code usage:

import faulthandlerfaulthandler.enable()// bad code goes here

Shell usage:

$ python3 -q -X faulthandler>>> /// bad cod goes here


Maybe there is a daemon thread running? There is a reproduceable bug, which was fixed only for 3.x, but not for 2.x:

http://bugs.python.org/issue1856:

shutdown (exit) can hang or segfault with daemon threads running

This is the answer to my own question. It took some time to find the root of the problem.

Here is the next question: How to code around this bug: Detect Interpreter shut down in daemon thread