From 362466c7d2e12c4587d5de1eb51cd537be9f7e2b Mon Sep 17 00:00:00 2001 From: Alex Trotsenko Date: Thu, 17 Aug 2017 09:16:16 +0300 Subject: [PATCH] QProcess/Unix: fix possible race condition inside waitForXXX() loops Calling QCoreApplication::processEvents() from a slot connected to the readyRead() signal might cause desynchronization in the waitForXXX() loop, if the process has been finished during the event processing. This results in unnecessary timeouts and causes waitForFinished() to fail unexpectedly. So, a proposed solution is to check the state on each iteration of the loop, as Windows implementation does. Given issue is tested by tst_QProcess::processEventsInAReadyReadSlot() which was unstable in CI. Task-number: QTBUG-62584 Change-Id: I7438cf67b0163bbf49314008a9dc660c0977fb7b Reviewed-by: Thiago Macieira --- src/corelib/io/qprocess_unix.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/corelib/io/qprocess_unix.cpp b/src/corelib/io/qprocess_unix.cpp index 6b7f187fee..318c633017 100644 --- a/src/corelib/io/qprocess_unix.cpp +++ b/src/corelib/io/qprocess_unix.cpp @@ -792,6 +792,10 @@ bool QProcessPrivate::waitForReadyRead(int msecs) if (qt_pollfd_check(poller.stdinPipe(), POLLOUT)) _q_canWrite(); + // Signals triggered by I/O may have stopped this process: + if (processState == QProcess::NotRunning) + return false; + if (qt_pollfd_check(poller.forkfd(), POLLIN)) { if (_q_processDied()) return false; @@ -838,6 +842,10 @@ bool QProcessPrivate::waitForBytesWritten(int msecs) if (qt_pollfd_check(poller.stderrPipe(), POLLIN)) _q_canReadStandardError(); + // Signals triggered by I/O may have stopped this process: + if (processState == QProcess::NotRunning) + return false; + if (qt_pollfd_check(poller.forkfd(), POLLIN)) { if (_q_processDied()) return false; @@ -883,6 +891,10 @@ bool QProcessPrivate::waitForFinished(int msecs) if (qt_pollfd_check(poller.stderrPipe(), POLLIN)) _q_canReadStandardError(); + // Signals triggered by I/O may have stopped this process: + if (processState == QProcess::NotRunning) + return true; + if (qt_pollfd_check(poller.forkfd(), POLLIN)) { if (_q_processDied()) return true;