Index: trunk/src/corelib/io/qprocess_os2.cpp
===================================================================
--- trunk/src/corelib/io/qprocess_os2.cpp	(revision 968)
+++ trunk/src/corelib/io/qprocess_os2.cpp	(revision 969)
@@ -1259,29 +1259,32 @@
 }
 
-qint64 QProcessPrivate::bytesAvailableFromStdout() const
-{
-    if (!dying && pipeData[OutPipe].signaled) {
-        // reuse the number we stored in _q_notified()
-        DEBUG(("QProcessPrivate::bytesAvailableFromStdout() == %lld (reused)",
-               pipeData[OutPipe].bytes));
-        return pipeData[OutPipe].bytes;
-    }
-
+// static
+qint64 QProcessPrivate::bytesAvailableFromPipe(HPIPE hpipe, bool *closed)
+{
     qint64 bytes = 0;
-    if (stdoutChannel.pipe.server != HPIPE(~0) &&
-        (!dying || stdoutChannel.type == QProcessPrivate::Channel::Normal)) {
+    if (hpipe != HPIPE(~0)) {
         ULONG state, dummy;
         AVAILDATA avail;
-        APIRET arc = DosPeekNPipe(stdoutChannel.pipe.server, 0, 0, &dummy,
-                                  &avail, &state);
+        APIRET arc = DosPeekNPipe(hpipe, 0, 0, &dummy, &avail, &state);
         Q_ASSERT(arc == NO_ERROR);
         Q_UNUSED(arc);
         bytes = (qint64)avail.cbpipe;
-        if (!dying && state != NP_STATE_CONNECTED) {
-            // if the pipe is closed, we negate the result (to preserve the
-            // number of bytes remaining) and add -1 to it (to distinguish from
-            // the case when there's no data byt the pipe is not closed)
-            bytes = -1 - bytes;
-        }
+        if (closed) {
+            *closed = state != NP_STATE_CONNECTED;
+        }
+    }
+    return bytes;
+}
+
+qint64 QProcessPrivate::bytesAvailableFromStdout() const
+{
+    qint64 bytes = 0;
+    if (!dying) {
+        Q_ASSERT(pipeData[OutPipe].signaled);
+        // reuse the number we got in _q_notified()
+        bytes = pipeData[OutPipe].bytes;
+    } else {
+        if (stdoutChannel.type == QProcessPrivate::Channel::Normal)
+            bytes = bytesAvailableFromPipe(stdoutChannel.pipe.server);
     }
 
@@ -1292,27 +1295,12 @@
 qint64 QProcessPrivate::bytesAvailableFromStderr() const
 {
-    if (!dying && pipeData[ErrPipe].signaled) {
-        // reuse the number we stored in _q_notified()
-        DEBUG(("QProcessPrivate::bytesAvailableFromStderr() == %lld (reused)",
-               pipeData[ErrPipe].bytes));
-        return pipeData[ErrPipe].bytes;
-    }
-
     qint64 bytes = 0;
-    if (stderrChannel.pipe.server != HPIPE(~0) &&
-        (!dying || stderrChannel.type == QProcessPrivate::Channel::Normal)) {
-        ULONG state, dummy;
-        AVAILDATA avail;
-        APIRET arc = DosPeekNPipe(stderrChannel.pipe.server, 0, 0, &dummy,
-                                  &avail, &state);
-        Q_ASSERT(arc == NO_ERROR);
-        Q_UNUSED(arc);
-        bytes = (qint64)avail.cbpipe;
-        if (!dying && state != NP_STATE_CONNECTED) {
-            // if the pipe is closed, we negate the result (to preserve the
-            // number of bytes remaining) and add -1 to it (to distinguish from
-            // the case when there's no data byt the pipe is not closed)
-            bytes = -1 - bytes;
-        }
+    if (!dying) {
+        Q_ASSERT(pipeData[ErrPipe].signaled);
+        // reuse the number we got in _q_notified()
+        bytes = pipeData[ErrPipe].bytes;
+    } else {
+        if (stderrChannel.type == QProcessPrivate::Channel::Normal)
+            bytes = bytesAvailableFromPipe(stderrChannel.pipe.server);
     }
 
@@ -1657,12 +1645,14 @@
     // the remote end of the pipe indicated by bytes < 0
 
+    // note 2: the PipeData::signaled flag may be set when we enter this method
+    // which means that waitFor() found more than one posted _q_notified() call
+    // of the same type in the event queue; we need to gracefully handle them
+    // all (they may inform about separate payloads)
+
     if (flags & CanReadStdOut) {
-        qint64 bytes = bytesAvailableFromStdout();
-        if (bytes) {
-            bool closed = false;
-            if (bytes < 0) {
-                bytes = -1 -bytes;
-                closed = true;
-            }
+        Q_ASSERT(!pipeData[OutPipe].signaled || waitMode);
+        bool closed = false;
+        qint64 bytes = bytesAvailableFromPipe(stdoutChannel.pipe.server, &closed);
+        if (bytes || closed) {
             pipeData[OutPipe].bytes = bytes;
             pipeData[OutPipe].signaled = true;
@@ -1686,11 +1676,8 @@
 
     if (flags & CanReadStdErr) {
-        qint64 bytes = bytesAvailableFromStderr();
-        if (bytes) {
-            bool closed = false;
-            if (bytes < 0) {
-                bytes = -1 -bytes;
-                closed = true;
-            }
+        Q_ASSERT(!pipeData[ErrPipe].signaled || waitMode);
+        bool closed = false;
+        qint64 bytes = bytesAvailableFromPipe(stderrChannel.pipe.server, &closed);
+        if (bytes || closed) {
             pipeData[ErrPipe].bytes = bytes;
             pipeData[ErrPipe].signaled = true;
Index: trunk/src/corelib/io/qprocess_p.h
===================================================================
--- trunk/src/corelib/io/qprocess_p.h	(revision 968)
+++ trunk/src/corelib/io/qprocess_p.h	(revision 969)
@@ -252,4 +252,6 @@
     enum WaitCond { WaitReadyRead, WaitBytesWritten, WaitFinished };
     bool waitFor(WaitCond cond, int msecs);
+    static qint64 bytesAvailableFromPipe(HPIPE hpipe, bool *closed = 0);
+
 #endif
 
