Details
Description
Using Python threads inside a PySide application causes memory corruption, random crashes, deadlocks, asserts, etc.
To reproduce (on vanilla Ubuntu 16.04): crash1.py
Stack trace: [^stack1.txt]
Both import thread/threading and QThread Python threads are affected.
This bug means that Python threads cannot be used safely inside a PySide application at all.
The problem stems from the fact that the Python garbage collector run can be triggered by the Python interpreter on any Python thread and at any point in time.
The garbage collector calls into SbkDeallocWrapper which in turn calls the QWidget destructor (QWidget::~QWidget) on a secondary thread which causes memory corruption, crashes & deadlocks as QWidget methods must not be called from any secondary threads as per definition.
If you run a Qt debug build, you'll see the following assert:
ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 7f5af8001180. Receiver '' (of type 'QWidget') was created in thread 2749e30", file kernel/qcoreapplication.cpp, line 539
See also https://riverbankcomputing.com/pipermail/pyqt/2011-August/030378.html
Recommended fix: Defer de-allocation of QWidget C++ objects inside SbkDeallocWrapper to the main UI thread (e.g. via deleteLater or via tracking them in an internal list).
The suggested work-around to disable the Python garbage collector and only manually collect in the main UI thread is neither practical nor performant.
Attachments
Issue Links
- relates to
-
PYSIDE-933 QtWebEngine crashes (QWebEngineUrlScheme)
- Closed