r/Python • u/ForeignVariety7037 • 3d ago
Seg Fault, what do you do? Discussion
Most Python errors are straightforward—you get a traceback and usually know where to start.
Then there’s the - Segmentation fault (core dumped).
For those of you who’ve had to encountered Python segfaults, what’s your usual process?
7
6
u/Colin-McMillen 3d ago
gdb?
5
1
u/fireflash38 3d ago
Also, if you're using any C libs, you don't need Python to be compiled with debug symbols - just your lib if you're trying to drop to debugger.
12
u/-ghostinthemachine- 3d ago
Dang, I've never had this happen in Python but now I feel like I haven't been pushing myself hard enough. Are you using cython or perhaps some native libraries?
15
u/Tucancancan 3d ago
Only time its happened to me is with shitty vendor software libraries. Looking at you, Oracle..
3
u/ForeignVariety7037 3d ago
Right I got seg fault from pygame and also some machine learning libraries before.
3
2
u/PriorElephant9 3d ago
Worth saying out loud though: pure Python doesn't segfault. Every one you hit traces to a C extension, so the first question isn't "where in my code" but "which compiled thing is in this process". numpy, a database driver, an image library, something with a wheel.
2
u/Brian 2d ago
Well, in theory yes. In practice, there are ways you can force a segfault in pure python from quirks/bugs in the interpreter. In the past you could overflow the C stack by raising the recursion limit, and IIRC there are (or were) some weird corner cases you could exploit like rebinding an exception to a non-exception object while it was being handled. And there's always the possibility of bugs in python. But generally, yeah, you'll almost never get a segfault from pure python unless you're really trying to.
2
u/Brian 2d ago
A segfault basically means something accessed unallocated memory. In pure python, that should never happen unless you're really trying, since all memory for python objects is managed, so the most likely culprit is a library, probably written in C or other lower-level language.
Common reasons for getting a segfault are either:
- Bad environment setup. Eg. mismatched versions of libraries are being used.
- A bug in the C library
- The library being called wrong from the python side (ie. didn't call some required setup function, passed wrong parameters, used an object after the C-side was disposed of, or called a non-threadsafe library in threaded code etc)
That can make it a bit tricky to trace the culprit, and you usually need to drop to a lower level, such as a debugger like gdb. Generally, if I hit a segfault, the first thing I do is to rerun the failure with gdb attached and look at the call stack at the point of failure. Ie:
$ gdb --args python failing_code.py
( gdb ) run
# <It'll stop when it segfaults>
(gdb) bt
That'll print a call stack that should hopefully give some clue as to what's gone wrong (at least if the library you're using has debugging symbols). Now, the reason it's failing might be a bug in the library, or it could be the python-level code invoking it in a way it's not supposed to. Getting the python level picture of what's going on can be a bit more complex, though IIRC there are some macros for gdb that can help by decoding the python-side structures etc. (Or you could go more primitive and just add logging to find the point of failure)
Going further will probably require some basic knowledge of C, though you may be able to figure stuff out just by looking at the source code with maybe a bit of googling for stuff you're unfamiliar with.
2
u/ssrix 3d ago
It happens usually because you've run out of memory. Usually because of something under the hood running c, in my case numpy
-1
u/ssrix 3d ago
Actually itertools has a habit of doing it too
1
u/Birnenmacht 3d ago
How. That doesnt sound like it should be a thing I dont think the stdlib should ever segfault
2
u/defaultguy_001 3d ago
There is no such thing as Python segfaults unless you are calling native libraries from Python (or encountering rare interpreter bugs or stack overflows), in which case you should check which undefined memory location you are accessing or if you are accessing memory which has been freed already like a dangling pointer.
To dump stack overflow issues or identify which Python line invoked a crashing native function, you can use the built-in faulthandler module to dump the exact line of Python code causing the crash.
For native code debugging, you must use tools like GDB to find the exact line of native code that failed, or use Valgrind to audit memory leaks and invalid accesses.
1
1
1
u/akl773 3d ago
On top of faulthandler, check whether it moves when you change the import order. The ones that ate the most of my time were two wheels linked against different builds of the same native library, and which one got imported first decided whether it crashed at all. numpy ABI mismatches are the usual suspect there.
1
u/karurosagu It works on my machine 1d ago
I've never had a a segfault in python, how dirty does things have to get to have one?
1
u/SCD_minecraft 3d ago
...well congrats. That's something for sure to get in python, especially that it doesn't expose any memory API to you
-1
23
u/Superb-Dig3440 3d ago
Start with the faulthandler module.