What causes a Python segmentation fault?

xiaolong picture xiaolong · Apr 5, 2012 · Viewed 155k times · Source

I am implementing Kosaraju's Strong Connected Component(SCC) graph search algorithm in Python.

The program runs great on small data set, but when I run it on a super-large graph (more than 800,000 nodes), it says "Segmentation Fault".

What might be the cause of it? Thank you!


Additional Info: First I got this Error when running on the super-large data set:

"RuntimeError: maximum recursion depth exceeded in cmp"

Then I reset the recursion limit using

sys.setrecursionlimit(50000)

but got a 'Segmentation fault'

Believe me it's not a infinite loop, it runs correct on relatively smaller data. It is possible the program exhausted the resources?

Answer

Shiplu Mokaddim picture Shiplu Mokaddim · Apr 5, 2012

This happens when a python extension (written in C) tries to access a memory beyond reach.

You can trace it in following ways.

  • Add sys.settrace at the very first line of the code.
  • Use gdb as described by Mark in this answer.. At the command prompt

    gdb python
    (gdb) run /path/to/script.py
    ## wait for segfault ##
    (gdb) backtrace
    ## stack trace of the c code