superlou superlou - 1 year ago 234
Python Question

Simple PyQt5 QML application causes segmentation fault

In trying to get a very basic PyQt5 QML example to run, I found that I get a segmentation fault. I verified that it only seems deal with displaying QML since an example without a window runs fine. I tried the following minimial test:

#!/usr/bin/python3
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine

# Main Function
if __name__ == '__main__':
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine("simple.qml")
engine.quit.connect(app.quit)
sys.exit(app.exec_())


simple.qml:

import QtQuick 2.5
import QtQuick.Controls 1.4

ApplicationWindow {
width: 300
height: 200
title: "Simple"
visible: true
}


When I run this application, a window appears for a split second before closing like in the more detailed example, and I receive
Segmentation fault
in the console (and nothing more).

Running from GDB shows that the
QSGRenderThread
is receiving the SIGSEGV:

(gdb) run snowman_qt.py
Starting program: /usr/bin/python3 snowman_qt.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7fffe912b700 (LWP 17200)]
[New Thread 0x7fffe3dbb700 (LWP 17201)]
[New Thread 0x7fffe1442700 (LWP 17202)]
[New Thread 0x7fffdbfff700 (LWP 17203)]

Thread 5 "QSGRenderThread" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffdbfff700 (LWP 17203)]
__strstr_sse2 (haystack_start=0x0, needle_start=0x7fffe28c9dd0 "nouveau") at ../string/strstr.c:63
63 ../string/strstr.c: No such file or directory.


The backtrace follows:

#0 __strstr_sse2 (haystack_start=0x0, needle_start=0x7fffe28c9dd0 "nouveau") at ../string/strstr.c:63
#1 0x00007fffe27233ea in QSGRenderContext::initialize(QOpenGLContext*) ()
from /usr/local/lib/python3.5/dist-packages/PyQt5/Qt/qml/QtQuick.2/../../lib/libQt5Quick.so.5
#2 0x00007fffe273e979 in ?? ()
from /usr/local/lib/python3.5/dist-packages/PyQt5/Qt/qml/QtQuick.2/../../lib/libQt5Quick.so.5
#3 0x00007ffff56835f9 in ?? () from /usr/local/lib/python3.5/dist-packages/PyQt5/Qt/lib/libQt5Core.so.5
#4 0x00007ffff7bc16fa in start_thread (arg=0x7fffdbfff700) at pthread_create.c:333
#5 0x00007ffff78f7b5d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109


If I run the QML file from a C++ application, there is no segmentation fault and the application works. Note that I'm using PyQT without PyOtherside, but the symptoms seem similar.

Is there a way to get more information to continue debugging?

I am running Python 3.5.2 on Linux Mint 18. My QT version is 5.7.0, my PyQt version is 5.7, and my SIP version is 4.18.1.

Answer Source

The rabbit hole went deeper than I thought. There were a few problems.

  1. The version of SIP in the Linux Mint 18 (likely Ubuntu 16.04) repository was too old. I updated to 4.18.1 by installing from source.
  2. With an updated SIP version, I was able to compile the latest PyQt5 from source. Before compiling, however, I had to install libqt5opengl5-dev so that it would compile with the QtOpenGL module. After compiling, I installed using checkinstall to make rolling back easier.

With the latest SIP and PyQt5, I no longer received the segmentation fault. However, I had issues with the OpenGL shaders not being created properly and just received a plain white window.

The workaround for this bug is to include the following before loading PyQt in the Python program:

import ctypes
from ctypes import util
ctypes.CDLL(util.find_library('GL'), ctypes.RTLD_GLOBAL)

I believe this is unique to Ubuntu-based OSs with NVIDIA graphics cards and binary drivers.