title: Qt Application stuck waiting for a wake event from the system
body: <p>I am in a bit of a pickle.</p>
  
  <p>My application has one main window (inherits from <code>QMainWindow</code>). The main window is parent to a QTabWidget, which acts as a sort of dialog that shows up when the user clicks a toolbar button on the main window, and hides when its 'Close' button is clicked. In addition, the main window has another toolbar button, which when clicked, calls exec() on a QDialog. While developing the app, I have noticed that once every 40 times or so, clicking one of the two buttons I have mentioned causes an empty widget to appear, but instead of the intended view being drawn, everything freezes. When I click the 'Close' button on the main window, my OS tells me that the application has stopped responding. There is never any way to recover from crashes like this, and I am usually forced to kill the app and restart.</p>
  
  <p>The vexing thing about all this is that I cannot reproduce the crashes at will. They are impossible to predict, and often catch me off-guard.</p>
  
  <p>I used gdb to attach to a frozen instance, and got the following back trace:</p>
  
  <pre><code>#0  pthread_cond_wait@@GLIBC_2.3.2 ()
      at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
  #1  0x00007f1ba3bf1b03 in QWaitCondition::wait(QMutex*, unsigned long) ()
     from /opt/Qt/5.4/gcc_64/lib/libQt5Core.so.5
  #2  0x00007f1ba3bea277 in QReadWriteLock::lockForRead() ()
     from /opt/Qt/5.4/gcc_64/lib/libQt5Core.so.5
  #3  0x00007f1b9bf5ee4e in ?? () from /opt/Qt/5.4/gcc_64/plugins/platforms/../../lib/libQt5DBus.so.5
  #4  0x00007f1b9bf61d71 in ?? () from /opt/Qt/5.4/gcc_64/plugins/platforms/../../lib/libQt5DBus.so.5
  #5  0x00007f1b9bf64e54 in ?? () from /opt/Qt/5.4/gcc_64/plugins/platforms/../../lib/libQt5DBus.so.5
  #6  0x00007f1b904159d6 in dbus_connection_dispatch () from /lib/x86_64-linux-gnu/libdbus-1.so.3
  #7  0x00007f1b9bf579f5 in ?? () from /opt/Qt/5.4/gcc_64/plugins/platforms/../../lib/libQt5DBus.so.5
  #8  0x00007f1b9bfa78f5 in ?? () from /opt/Qt/5.4/gcc_64/plugins/platforms/../../lib/libQt5DBus.so.5
  #9  0x00007f1ba3e5a326 in QObject::event(QEvent*) () from /opt/Qt/5.4/gcc_64/lib/libQt5Core.so.5
  #10 0x00007f1ba510b8f4 in QApplicationPrivate::notify_helper(QObject*, QEvent*) ()
     from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
  #11 0x00007f1ba510f506 in QApplication::notify(QObject*, QEvent*) ()
     from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
  #12 0x00007f1ba3e25c84 in QCoreApplication::notifyInternal(QObject*, QEvent*) ()
     from /opt/Qt/5.4/gcc_64/lib/libQt5Core.so.5
  #13 0x00007f1ba3e28868 in QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) ()
     from /opt/Qt/5.4/gcc_64/lib/libQt5Core.so.5
  #14 0x00007f1ba3e80123 in ?? () from /opt/Qt/5.4/gcc_64/lib/libQt5Core.so.5
  #15 0x00007f1ba283be04 in g_main_context_dispatch () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
  #16 0x00007f1ba283c048 in ?? () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
  #17 0x00007f1ba283c0ec in g_main_context_iteration () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
  #18 0x00007f1ba3e80554 in QEventDispatcherGlib::processEvents(QFlags&lt;QEventLoop::ProcessEventsFlag&gt;)
      () from /opt/Qt/5.4/gcc_64/lib/libQt5Core.so.5
  #19 0x00007f1ba3e23eab in QEventLoop::exec(QFlags&lt;QEventLoop::ProcessEventsFlag&gt;) ()
     from /opt/Qt/5.4/gcc_64/lib/libQt5Core.so.5
  #20 0x00007f1ba530a2ed in QDialog::exec() () from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
  #21 0x0000000000425fe4 in MainWindow::AddNewDownload() ()
  #22 0x00007f1ba3e57e7a in QMetaObject::activate(QObject*, int, int, void**) ()
     from /opt/Qt/5.4/gcc_64/lib/libQt5Core.so.5
  #23 0x00007f1ba50fee52 in QAction::triggered(bool) () from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
  #24 0x00007f1ba5100c77 in QAction::activate(QAction::ActionEvent) ()
     from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
  #25 0x00007f1ba520b0b5 in ?? () from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
  #26 0x00007f1ba520b374 in QAbstractButton::mouseReleaseEvent(QMouseEvent*) ()
     from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
  #27 0x00007f1ba52cf89a in QToolButton::mouseReleaseEvent(QMouseEvent*) ()
     from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
  #28 0x00007f1ba514723c in QWidget::event(QEvent*) () from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
  #29 0x00007f1ba52d0730 in QToolButton::event(QEvent*) ()
     from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
  #30 0x00007f1ba510b8f4 in QApplicationPrivate::notify_helper(QObject*, QEvent*) ()
     from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
  #31 0x00007f1ba510f071 in QApplication::notify(QObject*, QEvent*) ()
     from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
  #32 0x00007f1ba3e25c84 in QCoreApplication::notifyInternal(QObject*, QEvent*) ()
     from /opt/Qt/5.4/gcc_64/lib/libQt5Core.so.5
  #33 0x00007f1ba510df88 in QApplicationPrivate::sendMouseEvent(QWidget*, QMouseEvent*, QWidget*, QWidget*, QWidget**, QPointer&lt;QWidget&gt;&amp;, bool) () from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
  #34 0x00007f1ba5162387 in ?? () from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
  #35 0x00007f1ba5164e78 in ?? () from /opt/Qt/5.4/gcc_64/lib/libQt5Widgets.so.5
  ---Type &lt;return&gt; to continue, or q &lt;return&gt; to quit---
  </code></pre>
  
  <p>It appears that my app is stuck waiting to be woken up when a certain condition is met. The presence of <code>dbus</code> in the back trace suggests that it was trying to talk to another process, which I am guessing is a GUI-related process. I however do not know why the wait condition is met 98% of the time, but fails 2% of the time. When using <code>strace</code>, I was able to find that the system call that blocked is:</p>
  
  <p><code>futex(0x7f403c00690c, FUTEX_WAIT_PRIVATE, 1, NULL)</code></p>
  
  <p>I am not using <code>QThreads</code> or mutexes in my code, so I am guessing this problem is Qt-internal. Does anyone with knowledge of low-level workings of Qt know what is happening?</p>