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

QFuture: gracefully handle a destroyed context in continuations

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3: Somewhat important P3: Somewhat important
    • 6.6.0 FF
    • 6.5.0
    • Core: Threads
    • None
    • 86c044176 (dev), 07d6d31a4 (dev)

      QFuture::then() with a context object has this warning in the docs:

      Note: When calling this method, it should be guaranteed that the context stays alive throughout the execution of the chain.

      In QTBUG-86794, the reasoning is given:

      The problem is that when the thread invoking the continuation is different from the thread where the context object lives, theoretically it can happen that the context gets destroyed at the moment of trying to invoke the continuation in the given context (for example, if during the QMetaObject::invokeMethod(context, ...) call context gets destroyed, we will have problems)

      This makes sense. However, QFutureWatcher seems to be able to work around this. In particular, the following construct works fine:

      void SomeType::someFunc() {
          QFuture<void> future = // ...
          // future.then(this, ...) is bad, because "this" might be destroyed before the future finishes
          auto *watcher = new QFutureWatcher<void>(this);
          QtFuture::connect(watcher, &QFutureWatcherBase::finished).then(...); // works, even if "this" and the QFutureWatcher are destroyed early.
      }
      

      So could the context object functionality simply be implemented the with/similar to QFutureWatcher? Or does QFutureWatcher actually suffer from the same race condition as the continuation (without the assertions, so failing "silently")?

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

            cnn Qt Core & Network
            msarehn Arno Rehn
            Vladimir Minenko Vladimir Minenko
            Alex Blasche Alex Blasche
            Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes