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

Data race for lambda with Qt::QueuedConnection

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 5.0.1, 6.0.1
    • Core: Event loop
    • None
    • Windows
      Qt 5, Qt 6, both tested
      VS2019
      MinGW 11.2
    • Windows

    Description

      Seems like data race for lambda with Qt::QueuedConnection

      env:

      • Windows
      • Qt 5, Qt 6, both tested
      • VS2019
      • MinGW 11.2

      Lets take a look at code snippet:

      void Controller::doWork()
      {
          for (size_t ix = 0; ix < 2; ++ix) {
              QObject* ctx = new QObject;
              QObject::connect(this, &Controller::finished, ctx, [this, ctx] (QString id) {
                  this->disconnect(ctx);
                  ctx->deleteLater();
      
                  qDebug() << "Received value " << ctx << " :";
                  qDebug() << id;
              }, Qt::QueuedConnection);
      
              qDebug() << "Passed value: " << QString::number(ix);
              work(QString::number(ix));
          }
      }
      
      void Controller::work(QString id)
      {
          [[maybe_unused]] auto future = QtConcurrent::run([this, id] () {
              qDebug() << "Replied:  " << id;
              QThread::msleep(300);
              emit finished(id);
          });
      }
      

      EB:

      • lambda was called twice (see attachment)

      AB:

      • lambda was called 4 times, sometimes twice (see attachment)

      Minimal sample to test in attach.

      Detailed descrition.

      • In my main project i had issue that on such call i got EB from above. But, second call was with cached arguments from first call arguments.
        Example output:
        Passed value: "0"
        Passed value: "1"
        Replied: "0"
        Replied: "1"
        Received value "0"
        Received value "0"
      • I tried to reproduce argument caching on this code snippet, but i couldn`t, while i could steel reproduce it in main project env.
      • I could not reproduce AB in main project env while code architecture literally same.

      Attachments

        1. twice.bmp
          twice.bmp
          1.44 MB
        2. datarace.zip
          3 kB
        3. 4_times.bmp
          4_times.bmp
          1.44 MB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            thiago Thiago Macieira
            k0p4 Alex Kopnin
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes