Error: Segmentation fault (core dumped)

Python

Python Problem Overview


Im new in python and am getting a strange error:

Segmentation fault (core dumped)

When i execute the following code:

  class Workspace(QMainWindow, Ui_MainWindow):
    """ This class is for managing the whole GUI `Workspace'.
        Currently a Workspace is similar to a MainWindow
    """

    def __init__(self):
        #p= subprocess.Popen(["java -Xmx256m -jar bin/HelloWorld.jar"],cwd=r'/home/karen/sphinx4-1.0beta5-src/sphinx4-1.0beta5/', shell=True, stdout=subprocess.PIPE, bufsize= 4024)
        try:
            from Queue import Queue, Empty
        except ImportError:
            while True:
    #from queue import Queue, Empty  # python 3.x
                print "error"
        
        ON_POSIX = 'posix' in sys.builtin_module_names
       
        def enqueue_output(out, queue):
            for line in iter(out.readline, b''):
                queue.put(line)
            out.close()

        p= Popen(["java -Xmx256m -jar bin/HelloWorld.jar"],cwd=r'/home/karen/sphinx4-1.0beta5-src/sphinx4-1.0beta5/',stdout=PIPE, shell=True, bufsize= 4024)
        q = Queue()
        t = threading.Thread(target=enqueue_output, args=(p.stdout, q))
        #t = Thread(target=enqueue_output, args=(p.stdout, q))
        t.daemon = True # thread dies with the program
        t.start()
        
# ... do other things here
        def myfunc(q):
            while True:
	        
	            try: line = q.get_nowait()
		 # or q.get(timeout=.1)
	            except Empty:
    		        print('Vacio')
	            else: # got line
    # ... do something with line
		           
		            print line  
              
		        
        thread = threading.Thread(target=myfunc, args=(q,))
        thread.start()

This part of code is reading from a program's stdout.When i execute myfunc out of the thread it works ! But when i execute it in the thread fais... Any suggestion?

Python Solutions


Solution 1 - Python

"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 you're actually getting this error from running Python, this means the Python interpreter has crashed. There are only a few reasons this can happen:

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

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

  3. There's something wrong with your Python installation.

  4. You've 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, it's because you're doing something which is invalid, or at least uncommon and untested. But whether it's your "fault" in that sense or not - that doesn't 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 there's some different way to do what you want. Or switch to a different library.

On the other hand, since you're reading and printing out data from somewhere else, it's 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. (It's even possible that nobody crashed—if you fetched this page from the web and printed it out, you'd get that same line, right?) In your case, based on your comment, it's probably the Java program that crashed.

If you're not sure which case it is (and don't want to learn how to do process management, core-file inspection, or C-level debugging today), there's an easy way to test: After print line add a line saying print "And I'm OK". If you see that after the Segmentation fault line, then Python didn't crash, someone else did. If you don't see it, then it's probably Python that's crashed.

Solution 2 - Python

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.

Solution 3 - Python

It's 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

Solution 4 - Python

In my case: I forgot to activate virtualenv

I installed "pip install example" in the wrong virtualenv

Solution 5 - Python

In my case I imported pyxlsd module before module wich works with db Mysql. After I did put Mysql module first(upper in code) it became to work like a clock. Think there was some namespace issue.

Solution 6 - Python

Mildly unrelated to the question, but since this page appears whenever you search "(core dumped) python" then I might share a common problem that causes this error.

OpenCV cv2.imshow() sometimes raises this error on servers without graphical interfaces.

Hope I helped. Have a good day!

Solution 7 - Python

In my case, I was importing load_workbook from openpyxl before importing mysql.connector. I just switch the order and it started working again.

Segmentation fault (core dumped) error

from openpyxl import load_workbook
import mysql.connector

Solution

import mysql.connector
from openpyxl import load_workbook

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkarensantanaView Question on Stackoverflow
Solution 1 - PythonabarnertView Answer on Stackoverflow
Solution 2 - PythonCybersupernovaView Answer on Stackoverflow
Solution 3 - PythoncookiemonsterView Answer on Stackoverflow
Solution 4 - PythonHoangYellView Answer on Stackoverflow
Solution 5 - PythonАлександр ТарасовView Answer on Stackoverflow
Solution 6 - PythonGreg TarrView Answer on Stackoverflow
Solution 7 - PythonEduardoView Answer on Stackoverflow