diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-01 09:56:16 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-01 12:15:22 +0200 |
commit | fe36d47b371b71ad5fec30d4b5d7bf0baa0205ea (patch) | |
tree | 899fd0a5f4a6cf33a8a538aeb6ac4f6a0fd5362f /tests | |
parent | 4d943225eb7b8de22e33771b9389c919d59a4bd3 (diff) |
Fix race condition in QThreadPool::clear
Since we drop the lock while deleting threads, we need to handle
the queue possibly being accessed and changed by the pool threads
while clear() is running.
Pick-to: 5.15
Fixes: QTBUG-87092
Change-Id: I7611edab90520454278502a58621e299f9cd1f6e
Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp b/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp index 9b536aec00..3054e3c5a4 100644 --- a/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp +++ b/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp @@ -93,6 +93,7 @@ private slots: void priorityStart(); void waitForDone(); void clear(); + void clearWithAutoDelete(); void tryTake(); void waitForDoneTimeout(); void destroyingWaitsForTasksToFinish(); @@ -974,6 +975,31 @@ void tst_QThreadPool::clear() QCOMPARE(count.loadRelaxed(), threadPool.maxThreadCount()); } +void tst_QThreadPool::clearWithAutoDelete() +{ + class MyRunnable : public QRunnable + { + public: + MyRunnable() {} + void run() override { QThread::usleep(30); } + }; + + QThreadPool threadPool; + threadPool.setMaxThreadCount(4); + const int loopCount = 20; + const int batchSize = 500; + // Should not crash see QTBUG-87092 + for (int i = 0; i < loopCount; i++) { + threadPool.clear(); + for (int j = 0; j < batchSize; j++) { + auto *runnable = new MyRunnable(); + runnable->setAutoDelete(true); + threadPool.start(runnable); + } + } + QVERIFY(threadPool.waitForDone()); +} + void tst_QThreadPool::tryTake() { QSemaphore sem(0); |