title: How to debug a Python segmentation fault?
body: <p>How can I debug a Python segmentation fault?</p>
  
  <p>We are trying to run our python code on SuSE 12.3. We get reproducible segmentation faults. The python code has been working on other platforms without segmentation faults, for years.</p>
  
  <p>We only code Python, no C extension ....</p>
  
  <p>What is the best way to debug this? I know a bit ansi c, but that was ten years ago ....</p>
  
  <p>Python 2.7.5</p>
  
  <p><strong>Update</strong></p>
  
  <p>The segmentation fault happens on interpreter shutdown.</p>
  
  <p>I can run the script several times:</p>
  
  <pre><code>python -m pdb myscript.py arg1 arg1
  continue
  run
  continue
  run
  </code></pre>
  
  <p>But the segmentation faults happen, if I leave the pdb with ctrl-d.</p>
  
  <p><strong>Update 2</strong></p>
  
  <p>I now try to debug it with gdb:</p>
  
  <pre><code>gdb 
  &gt; file python
  &gt; run myscript.py arg1 arg2
  Program received signal SIGSEGV, Segmentation fault.
  [Switching to Thread 0x7fffefbe2700 (LWP 15483)]
  0x00007ffff7aef93c in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
  (gdb) bt
  #0  0x00007ffff7aef93c in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
  #1  0x00007ffff7af5303 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.7.so.1.0
  #2  0x00007ffff7adc858 in ?? () from /usr/lib64/libpython2.7.so.1.0
  #3  0x00007ffff7ad840d in PyObject_Call () from /usr/lib64/libpython2.7.so.1.0
  #4  0x00007ffff7af1082 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
  #5  0x00007ffff7af233d in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
  #6  0x00007ffff7af233d in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
  #7  0x00007ffff7af5303 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.7.so.1.0
  #8  0x00007ffff7adc5b6 in ?? () from /usr/lib64/libpython2.7.so.1.0
  #9  0x00007ffff7ad840d in PyObject_Call () from /usr/lib64/libpython2.7.so.1.0
  #10 0x00007ffff7ad9171 in ?? () from /usr/lib64/libpython2.7.so.1.0
  #11 0x00007ffff7ad840d in PyObject_Call () from /usr/lib64/libpython2.7.so.1.0
  #12 0x00007ffff7aeeb62 in PyEval_CallObjectWithKeywords () from /usr/lib64/libpython2.7.so.1.0
  #13 0x00007ffff7acc757 in ?? () from /usr/lib64/libpython2.7.so.1.0
  #14 0x00007ffff7828e0f in start_thread () from /lib64/libpthread.so.0
  #15 0x00007ffff755c7dd in clone () from /lib64/libc.so.6
  </code></pre>
  
  <p><strong>Update 3</strong></p>
  
  <p>I installed gdbinit from <a href="http://hg.python.org/cpython/file/default/Misc/gdbinit" rel="noreferrer">http://hg.python.org/cpython/file/default/Misc/gdbinit</a>
  and the debugging symbols from <a href="http://download.opensuse.org/debug/distribution/12.3/repo/oss/suse/x86_64/" rel="noreferrer">http://download.opensuse.org/debug/distribution/12.3/repo/oss/suse/x86_64/</a></p>
  
  <pre><code>(gdb) pystack
  No symbol "_PyUnicode_AsString" in current context.
  </code></pre>
  
  <p>What now?</p>
  
  <p><strong>Update 4</strong>
  We installed the a new RPM (python-2.7.5-3.1.x86_64). We get less segfaults, but they still happen.
  Here is the link to repository:</p>
  
  <p><a href="http://download.opensuse.org/repositories/devel:/languages:/python:/Factory/openSUSE_12.3/x86_64/" rel="noreferrer">http://download.opensuse.org/repositories/devel:/languages:/python:/Factory/openSUSE_12.3/x86_64/</a> </p>
  
  <p><strong>Update 5</strong>
  Solved my initial problem:</p>
  
  <p>It was <a href="http://bugs.python.org/issue1856" rel="noreferrer">http://bugs.python.org/issue1856</a> (shutdown (exit) can hang or segfault with daemon threads running)</p>
  
  <p>Related: <a href="https://stackoverflow.com/questions/18098475/detect-interpreter-shut-down-in-daemon-thread">Detect Interpreter shut down in daemon thread</a></p>