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

QFuture::then() with context object deadlocks

    XMLWordPrintable

Details

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

    Description

      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().

      Attachments

        Issue Links

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

          Activity

            People

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

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes