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

QFuture::cancel doesn't cancel chained QFutures

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Incomplete
    • Icon: P2: Important P2: Important
    • None
    • 6.9.1
    • Core: Threads
    • None
    • All

      There are two issues here:

      1. is described in the attached example.
      2. also about canceling chained qfutures, but is a bit tricky, let me give you an example for better understanding:
      QFuture<int> cancelableOperation()
      {
          QPromise<int> promise{QFutureInterface<int>{QFutureInterfaceBase::State::Pending}};
      
          QFuture<int> future = promise.future();    
          auto leakingThread = QThread::create(
              [](QPromise<int> promise) {
                  promise.start();
                  QThread::sleep(1);
                  Q_ASSERT(promise.isCanceled());
                  promise.addResult(42);
                  promise.finish(); 
              },
              std::move(promise));
          leakingThread->start();
          return future;
      }
      
      int main(int argc, char *argv[])
      {
          auto chainedFutures = cancelableOperation()
                                    .then([](int value) { Q_ASSERT(false); })
                                    .onCanceled([] { Q_ASSERT(true); });
          QThread::msleep(500);
          chainedFutures.cancel();
          chainedFutures.waitForFinished();
           return 0;
      }
       

      The expectation for 2. is that chainedFutures.cancel(); will cancel also cancelableOperation.

      Each example has Q_ASSERTS which should pass.

       

        1. main.cpp
          1 kB
          BogDan Vatra
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            cnn Qt Core & Network
            taipan BogDan Vatra
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes