python – Error: Segmentation fault (core dumped)

python – Error: Segmentation fault (core dumped)

Segmentation fault (core dumped) is the string that Linux prints when a program exits with a SIGSEGV signal and you have core creation enabled. This means some program has crashed.

If youre actually getting this error from running Python, this means the Python interpreter has crashed. There are only a few reasons this can happen:

  1. Youre using a third-party extension module written in C, and that extension module has crashed.

  2. Youre (directly or indirectly) using the built-in module ctypes, and calling external code that crashes.

  3. Theres something wrong with your Python installation.

  4. Youve discovered a bug in Python that you should report.

The first is by far the most common. If your q is an instance of some object from some third-party extension module, you may want to look at the documentation.

Often, when C modules crash, its because youre doing something which is invalid, or at least uncommon and untested. But whether its your fault in that sense or not – that doesnt matter. The module should raise a Python exception that you can debug, instead of crashing. So, you should probably report a bug to whoever wrote the extension. But meanwhile, rather than waiting 6 months for the bug to be fixed and a new version to come out, you need to figure out what you did that triggered the crash, and whether theres some different way to do what you want. Or switch to a different library.

On the other hand, since youre reading and printing out data from somewhere else, its possible that your Python interpreter just read the line Segmentation fault (core dumped) and faithfully printed what it read. In that case, some other program upstream presumably crashed. (Its even possible that nobody crashed—if you fetched this page from the web and printed it out, youd get that same line, right?) In your case, based on your comment, its probably the Java program that crashed.

If youre not sure which case it is (and dont want to learn how to do process management, core-file inspection, or C-level debugging today), theres an easy way to test: After print line add a line saying print And Im OK. If you see that after the Segmentation fault line, then Python didnt crash, someone else did. If you dont see it, then its probably Python thats crashed.

There is one more reason for such failure which I came to know when mine failed

  • You might be working with a lot of data and your RAM is full

This might not apply in this case but it also throws the same error and since this question comes up on top for this error, I have added this answer here.

python – Error: Segmentation fault (core dumped)

Its worth trying faulthandler to identify the line or the library that is causing the issue as mentioned here https://stackoverflow.com/a/58825725/2160809 and in the comments by Karuhanga

import faulthandler


faulthandler.enable()
// bad code goes here

or

$ python3 -q -X faulthandler
>>> /// bad cod goes here

Leave a Reply

Your email address will not be published.