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

Application crash due to double invocation of continuation in multithreaded QPromise and QFuture usage

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P1: Critical
    • None
    • 6.5
    • None

    Description

      In my application, I use QFuture along with its then() method to manage asynchronous operations. Over time, the application experiences crashes, seemingly due to a continuation being invoked twice, which is not the expected behaviour.

      Code Sample:

      #include "test.h"
      
      #include <QFuture>
      #include <QPromise>
      #include <QtConcurrent>
      
      Test::Test(QObject *parent)
          : QObject{parent} 
      {  
        connect(this, &Test::tryNext, this, &Test::test, Qt::QueuedConnection);
      }
      
      void Test::test() {
        auto f = QtConcurrent::run([] {}); //fast function that can finish                                      
                                           // before .then is called
        f.then([this] { emit tryNext(); });
      } 

      I assume that the problem arises because of the following:

      1. Call QtConcurrent::run with a fast function
      2. Due to the fast function, QFutureInterfaceBase::reportFinished() is triggered.
      3. At the same time in another thread, the then method is called and since the future is already finished, it also triggers the continuation and saves it for later invocation.
      4. After the saving of the continuation, QFutureInterfaceBase::runContinuation() is called and, noticing the continuation already stored, invokes the continuation once again.
      5. The double invocation of the continuation leads to a crash. (call methods of moved promise)

      To reproduce the problem, simply run the attached project and after a small amount of time, it will crash. The crash has been confirmed on macOS and Windows platforms.

      Attachments

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

        Activity

          People

            cnn Qt Core & Network
            xorknown_cmt Oleg Rudakov
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes