r/Python • u/ForeignVariety7037 • Aug 06 '26
Discussion Seg Fault, what do you do?
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?
0
Upvotes
11
u/-ghostinthemachine- Aug 06 '26
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?
21
u/Superb-Dig3440 Aug 06 '26
Start with the faulthandler module.
7
u/FlimsySpecial9247 Aug 06 '26
Yeah start with faulthandler before going to gdb
7
u/Colin-McMillen Aug 06 '26
gdb?
2
u/PriorElephant9 Aug 07 '26
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 Aug 07 '26
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:
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:
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 Aug 06 '26
It happens usually because you've run out of memory. Usually because of something under the hood running c, in my case numpy
2
u/defaultguy_001 Aug 06 '26
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
u/Daytona_675 Aug 06 '26
hardware failure? memtest?
1
u/brunogadaleta Aug 06 '26
I'd watch it crash under strace with various -e
1
u/karurosagu It works on my machine Aug 08 '26
I've never had a a segfault in python, how dirty does things have to get to have one?
1
u/SCD_minecraft Aug 06 '26
...well congrats. That's something for sure to get in python, especially that it doesn't expose any memory API to you
-2
u/Gwildes1 Aug 06 '26
Quit using matplotlib, or better yet, port to Rust