What causes a Python segmentation fault?

Go To StackoverFlow.com

64

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?

2012-04-05 20:28
by xiaolong
May be you can have a look CrashingPythonAbhijit 2012-04-05 20:32
Is this running in pure Python or are you using a C extension module? If it's pure Python then it's a bug there and congratulations. If you're using a c module, then the segfault is probably coming from there - aaronasterling 2012-04-05 20:32
it's Pure python. The program runs great on relatively small data set and it made me think that the code is correct - xiaolong 2012-04-05 20:44
According to the Python documentation - James Thiele 2012-04-05 21:07
According to the Python documentation::::::

The highest possible limit is platform-dependent. A user may need to set the limit higher when she has a program that requires deep recursion and a platform that supports a higher limit. This should be done with care, because a too-high limit can lead to a crash.::::::

You didn't specify an OS. The reference to crash might mean segmentaion fault on your OS. Try a smaller stack. But IIRC the algorithm you're using puts the rntire SSC on the stack so you may run out of stack - James Thiele 2012-04-05 21:17

>algoclass by any chance? - MattyW 2012-04-06 05:38
@MattyW Yup. Later I translated Python into C/C++ and didn't find the problem when I store the Graph as global variable. Seems like Python relies more on the system stack. see solution codexiaolong 2012-04-08 20:33


61

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
    
2012-04-05 20:32
by Shiplu Mokaddim
thanks, but my code is pure python, does it make a difference - xiaolong 2012-04-05 20:47
Check which python modules you are using? Some modules are written in python and other are in C. I think you need to report a bug - Shiplu Mokaddim 2012-04-05 20:49
similar, also helpful: stdlib's trace module just helped me get to the bottom of a segmentation fault on a staging server, without installing a new dependency, and without modifying code - hangtwenty 2015-05-06 02:53
on OSX Sierra, gdb was replaced by lld - kthouz 2016-12-01 18:19
On OS X see https://unconj.ca/blog/setting-up-gdb-for-debugging-python-on-os-x.htm - kilgoretrout 2018-10-26 20:24


46

I understand you've solved your issue, but for others reading this thread, here is the answer: you have to increase the stack that your operating system allocates for the python process.

The way to do it, is operating system dependant. In linux, you can check with the command ulimit -s your current value and you can increase it with ulimit -s <new_value>

Try doubling the previous value and continue doubling if it does not work, until you find one that does or run out of memory.

2012-07-06 19:19
by Davide
Also a good way to check if you are coming up against a ulimit max is to run lsof and use grep orwc -l to keep track of everything - cdated 2013-02-05 17:57
I concur. This actually worked for my Kosaraju's SCC implementation by fixing the segfault on both Python and C++ implementations.

For my MAC, I found out the possible maximum via - Rock 2016-11-18 03:59
note that the ulimit value is modified only for the particular shell it is executed in, so that you do not accidentally modify the value for your whole syste - Tanmay Garg 2016-12-04 06:40
@TanmayGarg thanks for pointing it ou - void 2017-07-01 07:38
I did this and ended up with ulimit -s 16384, however after running I still got a segmentation error - Sreehari R 2017-12-29 09:44
@SreehariR Try increasing it even more. However it could also be an issue with a python extension (if you are using any), which (this other answer)[https://stackoverflow.com/a/10035594/25891] suggests how to debu - Davide 2017-12-30 03:20


10

Segmentation fault is a generic one, there are many possible reasons for this:

  • Low memory
  • Faulty Ram memory
  • Fetching a huge data set from the db using a query (if the size of fetched data is more than swap mem)
  • wrong query / buggy code
  • having long loop (multiple recursion)
2013-11-04 21:04
by Sadheesh


1

Updating the ulimit worked for my Kosaraju's SCC implementation by fixing the segfault on both Python (Python segfault.. who knew!) and C++ implementations.

For my MAC, I found out the possible maximum via :

$ ulimit -s -H
65532
2016-11-18 04:08
by Rock


0

Google search found me this article, and I did not see the following "personal solution" discussed.


My recent annoyance with Python 3.7 on Windows Subsystem for Linux is that: on two machines with the same Pandas library, one gives me segmentation fault and the other reports warning. It was not clear which one was newer, but "re-installing" pandas solves the problem.

Command that I ran on the buggy machine.

conda install pandas

More details: I was running identical scripts (synced through Git), and both are Windows 10 machine with WSL + Anaconda. Here go the screenshots to make the case. Also, on the machine where command-line python will complain about Segmentation fault (core dumped), Jupyter lab simply restarts the kernel every single time. Worse still, no warning was given at all.

enter image description here

2019-02-10 18:19
by llinfeng
Ads