summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorIevgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io>2021-09-24 13:14:05 +0200
committerIevgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io>2021-10-05 15:30:32 +0200
commit594791fc54054e2f4e221f218b3b2d56f8fec4f1 (patch)
treedd29f794ce7d9cfe0f117a154ea8c7fdc2b479d6 /tests
parent4851da1509a3f1bb79ff2d9943e23dec37f781bf (diff)
QThread: Reset the system thread ID when thread exits on Unix
Unix QThread implementation stores pthread_t as a system thread ID when the thread is created, but never resets the system ID when those threads are destroyed. Some implementations may reuse the same thread IDs for new threads, and this may cause QThread::wait() to erroneously complain that "Thread tried to wait on itself". This patch sets the system thread ID to nullptr when the thread is about to exit and be destroyed by the system. A regression test is added to tst_qthread. Fixes: QTBUG-96846 Change-Id: I0850425dd0e09af50e59c9038e7e662a2a624beb Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> (adapted from commit 52ad59f9eabbe1fc8ca49d117e4955f2d21d50a7)
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/thread/qthread/tst_qthread.cpp67
1 files changed, 67 insertions, 0 deletions
diff --git a/tests/auto/corelib/thread/qthread/tst_qthread.cpp b/tests/auto/corelib/thread/qthread/tst_qthread.cpp
index bfcb66d15e..b868f65268 100644
--- a/tests/auto/corelib/thread/qthread/tst_qthread.cpp
+++ b/tests/auto/corelib/thread/qthread/tst_qthread.cpp
@@ -107,6 +107,7 @@ private slots:
void quitLock();
void create();
+ void threadIdReuse();
};
enum { one_minute = 60 * 1000, five_minutes = 5 * one_minute };
@@ -1633,5 +1634,71 @@ void tst_QThread::requestTermination()
QVERIFY(!thread.isInterruptionRequested());
}
+/*
+ This is a regression test for QTBUG-96846.
+
+ Incorrect system thread ID cleanup can cause QThread::wait() to report that
+ a thread is trying to wait for itself.
+*/
+void tst_QThread::threadIdReuse()
+{
+ class Thread1 : public QThread {
+ public:
+ // It's important that those thread ID's are not accessed concurrently
+ Qt::HANDLE savedThreadId;
+
+ void run() override { savedThreadId = QThread::currentThreadId(); }
+ };
+
+ class Thread2 : public Thread1 {
+ public:
+ bool waitOk;
+ Thread2(QThread *otherThread) : Thread1(), waitOk(false), otherThread(otherThread) {}
+
+ void run() override {
+ Thread1::run();
+ waitOk = otherThread->wait();
+ }
+
+ private:
+ QThread *const otherThread;
+ };
+
+ Thread1 thread1;
+ thread1.start();
+ QVERIFY(thread1.wait());
+
+ // If the system thread allocated for thread1 is destroyed before thread2 is
+ // started, at least on some versions of Linux the system thread ID for
+ // thread2 would be the same as one that was used for thread1.
+
+ // The system thread may be alive for some time after returning from
+ // QThread::wait() because the implementation is using detachable threads, so
+ // some additional time is required for the system thread to terminate. Not
+ // waiting long enough here would result in a new system thread ID being
+ // allocated for thread2 and this test passing even without a fix for
+ // QTBUG-96846.
+ bool threadIdReused = false;
+
+ for (int i = 0; i < 42; i++) {
+ QThread::msleep(1);
+
+ Thread2 thread2(&thread1);
+ thread2.start();
+ QVERIFY(thread2.wait());
+ QVERIFY(thread2.waitOk);
+
+ if (thread1.savedThreadId == thread2.savedThreadId) {
+ qDebug("Thread ID reused at iteration %d", i);
+ threadIdReused = true;
+ break;
+ }
+ }
+
+ if (!threadIdReused) {
+ QSKIP("Thread ID was not reused");
+ }
+}
+
QTEST_MAIN(tst_QThread)
#include "tst_qthread.moc"