diff options
author | Jarek Kobus <jaroslaw.kobus@qt.io> | 2023-11-15 15:04:01 +0100 |
---|---|---|
committer | MÃ¥rten Nordheim <marten.nordheim@qt.io> | 2024-03-15 11:42:36 +0000 |
commit | 63b2cf8a457f0eea861fa060c610a74b35450ba6 (patch) | |
tree | 7450b5f92af5cd1fd3586bcf4385347d1c59ad76 /tests/auto/corelib/thread/qfuturewatcher | |
parent | f813b76fb2b4189fc18d0b5a75faf1eea2bb1de2 (diff) |
QFutureWatcher: Fix race for initial emission of resultReadyAt()
When connecting a QFutureWatcher to the QFuture it will connect to the
output interface, which will queue up events to notify about the current
state. This happens in the thread of the QFutureWatcher.
Since 07d6d31a4c0c17d8c897d783a9b0841df6834b02 unfortunately the sending
of those events was done outside the lock, meaning the worker-thread
could _also_ send events at the same time, leading to a race on which
events would be sent first.
To fix this we move the emission of the events back into the lock
and because it is now inside the lock again anyway, we will revert
back to posting the callout events immediately, so this patch also
partially reverts 07d6d31a4c0c17d8c897d783a9b0841df6834b02
Fixes: QTBUG-119169
Pick-to: 6.7 6.6
Change-Id: If29ab6712a82e7948c0ea4866340b6fac5aba5ef
Reviewed-by: Arno Rehn <a.rehn@menlosystems.com>
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
Diffstat (limited to 'tests/auto/corelib/thread/qfuturewatcher')
-rw-r--r-- | tests/auto/corelib/thread/qfuturewatcher/tst_qfuturewatcher.cpp | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/tests/auto/corelib/thread/qfuturewatcher/tst_qfuturewatcher.cpp b/tests/auto/corelib/thread/qfuturewatcher/tst_qfuturewatcher.cpp index a5b447470c..40aa89ded4 100644 --- a/tests/auto/corelib/thread/qfuturewatcher/tst_qfuturewatcher.cpp +++ b/tests/auto/corelib/thread/qfuturewatcher/tst_qfuturewatcher.cpp @@ -26,6 +26,7 @@ private slots: void cancelAndFinish(); void resultAt(); void resultReadyAt(); + void orderedResultReadyAt(); void futureSignals(); void watchFinishedFuture(); void watchCanceledFuture(); @@ -280,6 +281,28 @@ void tst_QFutureWatcher::resultReadyAt() QVERIFY(resultSpy.wait()); } +void tst_QFutureWatcher::orderedResultReadyAt() +{ + for (int i = 0; i < 1000; ++i) { + QObject context; + QFuture<QString> f = run([](QPromise<QString> &fi) { + fi.addResult("First"); + fi.addResult("Second"); + }); + QList<int> actualIndices; + + QFutureWatcher<QString> watcher; + connect(&watcher, &QFutureWatcherBase::resultReadyAt, &context, + [&actualIndices](int index) { actualIndices.append(index); }); + watcher.setFuture(f); + f.waitForFinished(); + QCoreApplication::processEvents(); + const QList<int> expectedIndices{0, 1}; + QCOMPARE(actualIndices.size(), expectedIndices.size()); + QCOMPARE(actualIndices, expectedIndices); + } +} + class SignalSlotObject : public QObject { Q_OBJECT |