Details
-
Bug
-
Resolution: Fixed
-
P2: Important
-
6.5.6, 6.7.2
-
6efec3850 (dev), 5a9c51323 (6.8), 7b47ce94d (tqtc/lts-6.5)
-
Foundation Sprint 118, Foundation Sprint 119
Description
https://doc.qt.io/qt-6/qpromise.html#setException says, "This method must not be used after QFuture::cancel() or QPromise::finish()". But how can we write code that guarantees that QPromise::setException() won't be called after QFuture::cancel()? The former is meant to be called from the "promise thread" while the latter is meant to be called from the "future thread".
Consider this sequence of events:
- Thread A creates a QPromise + QFuture, then passes the QPromise to Thread B
- Thread B starts doing work
- Thread A requests cancellation
- Thread B has an error and wants to call setException()
// Inside Thread B if (!promise->isCanceled()) { // isCanceled() returns false here // ... // Uh-oh, isCanceled() could return true here! promise->setException(MyException()); }
Suggestion
We need a function that "atomically" checks the cancellation flag and stores the exception if not cancelled. For example, bool setExceptionIfNotCanceled(const QException &) – return true if the exception was set, false otherwise.