Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-128405

Unavoidable race condition when using QPromise::setException()?

    XMLWordPrintable

Details

    • 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:

      1. Thread A creates a QPromise + QFuture, then passes the QPromise to Thread B
      2. Thread B starts doing work
      3. Thread A requests cancellation
      4. 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.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            ivan.solovev Ivan Solovev
            skoh-qt Sze Howe Koh
            Vladimir Minenko Vladimir Minenko
            Alex Blasche Alex Blasche
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: