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

QFuture::then() with context object deadlocks

XMLWordPrintable

    • 59e21a536 (dev), f89d80c90 (6.7), d5528cdec (6.6)

      QFuture::then(context, ...) (and probably the other continuation attachers) can deadlock in Qt 6.6.0. Example:

      #include <QCoreApplication>
      #include <QFuture>
      #include <QPromise>
      #include <QtDebug>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication app(argc, argv);
      
          QPromise<int> p;
          auto f = p.future();
          f.then(&app, [](int i) {
              qDebug() << i;
          });
      
          QMetaObject::invokeMethod(&app, [&] { app.quit(); }, Qt::QueuedConnection);
      
          p.start();
          p.addResult(42);
          p.finish();  // Deadlocks. Remove to have the app quit.
      
          return app.exec();
      }
      

      I've tracked the cause to QFutureInterfaceBase::reportFinished(), which sends a QFutureCallOut without unlocking the mutex. The responders to this will want to access the future's result, which again tries to lock the mutex -> deadlock.
      This happens only if the context object lives in the same thread that calls QPromise::finish().

        For Gerrit Dashboard: QTBUG-119406
        # Subject Branch Project Status CR V

            ivan.solovev Ivan Solovev
            msarehn Arno Rehn
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes