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

Windows: QThread::terminate() provokes a hang sometimes

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Reported
    • Priority: P3: Somewhat important
    • Resolution: Unresolved
    • Affects Version/s: 6.2.4
    • Fix Version/s: None
    • Component/s: Core: Threads
    • Labels:
      None
    • Platform/s:
      Windows

      Description

      Found this while investigating QTBUG-103922:

      Sample code:

      for (int i = 0; i < 1000; ++i)
      {
          QThread thread;
          thread.start();
          thread.terminate();
          thread.wait();
      }
      

      Sometimes this causes a hung at least on Windows - probably due to some internal race condition.
      Pay attention that terminate() sometimes may have no any effect here, it just returns early because termination is not yet enabled in QThreadPrivate::start().

      see src/corelib/thread/qthread_win.cpp:

      unsigned int __stdcall QT_ENSURE_STACK_ALIGNED_FOR_SSE QThreadPrivate::start(void *arg) noexcept
      {
      ...
          QThread::setTerminationEnabled(false); 
      
      ...
          QThread::setTerminationEnabled(true);
      ...
      }
      
      void QThread::terminate()
      {
      ...
          if (!d->terminationEnabled) {
              d->terminatePending = true;
              return; // <----
          }
      ...
      }
      

        Attachments

          Issue Links

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

            Activity

              People

              Assignee:
              thiago Thiago Macieira
              Reporter:
              studiosus Vladimir Belyavsky
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:

                  Gerrit Reviews

                  There are no open Gerrit changes