Search code examples
c++qtqt5valgrindqthread

QThread: race condition with the simplest code


debugging the app i'm working over i've found some strange race conditions apparently not caused by our code. Making a test with the following code Helgrind reports in few seconds more than 5000 potential race conditions.

Here the code:

#include <QApplication>
#include <QThread>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    QThread thread;
    thread.start();

    return app.exec();
}

Here the initial part of hellgrind report:

==9856== Helgrind, a thread error detector
==9856== Copyright (C) 2007-2015, and GNU GPL'd, by OpenWorks LLP et al.
==9856== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==9856== Command: /home/gianks/NetBeansProjects/ThreadTest/dist/Debug/GNU-Linux/ThreadTest
==9856== 
==9856== ---Thread-Announcement------------------------------------------
==9856== 
==9856== Thread #1 is the program's root thread
==9856== 
==9856== ---Thread-Announcement------------------------------------------
==9856== 
==9856== Thread #2 was created
==9856==    at 0x6041B1E: clone (clone.S:74)
==9856==    by 0x6852189: create_thread (createthread.c:102)
==9856==    by 0x6853EC3: pthread_create@@GLIBC_2.2.5 (pthread_create.c:679)
==9856==    by 0x4C34BB7: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x5571DB7: QThread::start(QThread::Priority) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x40F9778: ??? (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40FF5E3: QXcbConnection::QXcbConnection(QXcbNativeInterface*, bool, unsigned int, char const*) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x41029EC: QXcbIntegration::QXcbIntegration(QStringList const&, int&, char**) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40293AC: ??? (in /usr/lib/x86_64-linux-gnu/qt5/plugins/platforms/libqxcb.so)
==9856==    by 0x63E3DD1: QPlatformIntegrationFactory::create(QString const&, QStringList const&, int&, char**, QString const&) (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x63F0003: QGuiApplicationPrivate::createPlatformIntegration() (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x63F0F0C: QGuiApplicationPrivate::createEventDispatcher() (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856== 
==9856== ----------------------------------------------------------------
==9856== 
==9856== Possible data race during read of size 4 at 0x599E9B0 by thread #1
==9856== Locks held: none
==9856==    at 0x556AD30: QBasicMutex::unlockInternal() (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x4C344E7: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x5571D23: QThread::start(QThread::Priority) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x40F9778: ??? (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40FF5E3: QXcbConnection::QXcbConnection(QXcbNativeInterface*, bool, unsigned int, char const*) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x41029EC: QXcbIntegration::QXcbIntegration(QStringList const&, int&, char**) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40293AC: ??? (in /usr/lib/x86_64-linux-gnu/qt5/plugins/platforms/libqxcb.so)
==9856==    by 0x63E3DD1: QPlatformIntegrationFactory::create(QString const&, QStringList const&, int&, char**, QString const&) (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x63F0003: QGuiApplicationPrivate::createPlatformIntegration() (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x63F0F0C: QGuiApplicationPrivate::createEventDispatcher() (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x5757A85: QCoreApplication::init() (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x5757AF5: QCoreApplication::QCoreApplication(QCoreApplicationPrivate&) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856== 
==9856== This conflicts with a previous write of size 4 by thread #2
==9856== Locks held: none
==9856==    at 0x556A977: QBasicMutex::lockInternal() (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x4C34377: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x5572754: ??? (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x4C34DB6: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x68536F9: start_thread (pthread_create.c:333)
==9856==    by 0x6041B5C: clone (clone.S:109)
==9856==  Address 0x599e9b0 is in the Data segment of /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1
==9856== 
==9856== ----------------------------------------------------------------
==9856== 
==9856== Possible data race during write of size 8 at 0xBF41830 by thread #1
==9856== Locks held: none
==9856==    at 0x556AD3A: QBasicMutex::unlockInternal() (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x4C344E7: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x5571D23: QThread::start(QThread::Priority) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x40F9778: ??? (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40FF5E3: QXcbConnection::QXcbConnection(QXcbNativeInterface*, bool, unsigned int, char const*) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x41029EC: QXcbIntegration::QXcbIntegration(QStringList const&, int&, char**) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40293AC: ??? (in /usr/lib/x86_64-linux-gnu/qt5/plugins/platforms/libqxcb.so)
==9856==    by 0x63E3DD1: QPlatformIntegrationFactory::create(QString const&, QStringList const&, int&, char**, QString const&) (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x63F0003: QGuiApplicationPrivate::createPlatformIntegration() (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x63F0F0C: QGuiApplicationPrivate::createEventDispatcher() (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x5757A85: QCoreApplication::init() (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x5757AF5: QCoreApplication::QCoreApplication(QCoreApplicationPrivate&) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856== 
==9856== This conflicts with a previous read of size 8 by thread #2
==9856== Locks held: none
==9856==    at 0x556A9A5: QMutex::lock() (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x4C34377: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x5572754: ??? (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x4C34DB6: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x68536F9: start_thread (pthread_create.c:333)
==9856==    by 0x6041B5C: clone (clone.S:109)
==9856==  Address 0xbf41830 is 112 bytes inside a block of size 168 alloc'd
==9856==    at 0x4C2F50F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x556D793: QThread::QThread(QObject*) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x40F96B2: ??? (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40FF5D4: QXcbConnection::QXcbConnection(QXcbNativeInterface*, bool, unsigned int, char const*) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x41029EC: QXcbIntegration::QXcbIntegration(QStringList const&, int&, char**) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40293AC: ??? (in /usr/lib/x86_64-linux-gnu/qt5/plugins/platforms/libqxcb.so)
==9856==    by 0x63E3DD1: QPlatformIntegrationFactory::create(QString const&, QStringList const&, int&, char**, QString const&) (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x63F0003: QGuiApplicationPrivate::createPlatformIntegration() (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x63F0F0C: QGuiApplicationPrivate::createEventDispatcher() (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x5757A85: QCoreApplication::init() (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x5757AF5: QCoreApplication::QCoreApplication(QCoreApplicationPrivate&) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x63F2D08: QGuiApplication::QGuiApplication(QGuiApplicationPrivate&) (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==  Block was alloc'd by thread #1
==9856== 
==9856== ----------------------------------------------------------------
==9856== 
==9856== Possible data race during read of size 8 at 0xBF41830 by thread #2
==9856== Locks held: none
==9856==    at 0x556A92D: QBasicMutex::lockInternal() (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x4C34377: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x5572754: ??? (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x4C34DB6: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x68536F9: start_thread (pthread_create.c:333)
==9856==    by 0x6041B5C: clone (clone.S:109)
==9856== 
==9856== This conflicts with a previous write of size 8 by thread #1
==9856== Locks held: none
==9856==    at 0x556AD3A: QBasicMutex::unlockInternal() (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x4C344E7: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x5571D23: QThread::start(QThread::Priority) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x40F9778: ??? (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40FF5E3: QXcbConnection::QXcbConnection(QXcbNativeInterface*, bool, unsigned int, char const*) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x41029EC: QXcbIntegration::QXcbIntegration(QStringList const&, int&, char**) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40293AC: ??? (in /usr/lib/x86_64-linux-gnu/qt5/plugins/platforms/libqxcb.so)
==9856==    by 0x63E3DD1: QPlatformIntegrationFactory::create(QString const&, QStringList const&, int&, char**, QString const&) (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==  Address 0xbf41830 is 112 bytes inside a block of size 168 alloc'd
==9856==    at 0x4C2F50F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==9856==    by 0x556D793: QThread::QThread(QObject*) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x40F96B2: ??? (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40FF5D4: QXcbConnection::QXcbConnection(QXcbNativeInterface*, bool, unsigned int, char const*) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x41029EC: QXcbIntegration::QXcbIntegration(QStringList const&, int&, char**) (in /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5.5.1)
==9856==    by 0x40293AC: ??? (in /usr/lib/x86_64-linux-gnu/qt5/plugins/platforms/libqxcb.so)
==9856==    by 0x63E3DD1: QPlatformIntegrationFactory::create(QString const&, QStringList const&, int&, char**, QString const&) (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x63F0003: QGuiApplicationPrivate::createPlatformIntegration() (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x63F0F0C: QGuiApplicationPrivate::createEventDispatcher() (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==    by 0x5757A85: QCoreApplication::init() (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x5757AF5: QCoreApplication::QCoreApplication(QCoreApplicationPrivate&) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.5.1)
==9856==    by 0x63F2D08: QGuiApplication::QGuiApplication(QGuiApplicationPrivate&) (in /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5.5.1)
==9856==  Block was alloc'd by thread #1

Is there actually a bug?

A note: i've also tried invoking start with a queued connection, same effect, so starting the event loop before the other thread is not a solution.

Thanks


Solution

  • helgrind does not understand the atomics used inside Qt itself. You'll need to suppress that warning (and ignore it). Fetch this:

    https://github.com/KDE/kde-dev-scripts/blob/master/kde.supp

    And add --suppressions=/path/to/kde.supp to the helgrind command line.

    For more information about using helgrind in a Qt application check out this blog post.


    (Side note: destroying a QThread which is still running will crash your program, so be sure to join it before returning from main.)