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

QProcess does not output as expected

    XMLWordPrintable

Details

    • Windows

    Description

      The following piece of code starts a master process, then a slave from master. Master has a data stream attached to slave and also is reading from slave when ready. Slave reads data and writes it back. It is run on Windows, Qt 6.5.

      #include <QtCore>
      #define dumpval(x) qDebug()<<#x<<'='<<x
      void slave()
      {
          QCoreApplication::setApplicationName("slave");
          qDebug()<<"started";
          QFile input, output;
          QDataStream inputStream{&input}, outputStream{&output};
          dumpval(input.open(stdin, QFile::ReadOnly));
          dumpval(output.open(stdout, QFile::WriteOnly));
          QByteArray data;
          do {
              inputStream >> data;
              outputStream << data;
              dumpval(data);
          } while (inputStream.status() == QDataStream::Ok && !data.isEmpty());
          dumpval(inputStream.status());
      }
      void master()
      {
          QCoreApplication::setApplicationName("master");
          qDebug()<<"started";
          QProcess p;
          p.setProgram(QCoreApplication::applicationFilePath());
          p.setArguments({"slave"});
          p.setProcessChannelMode(QProcess::ForwardedChannels);
          p.start();
          p.waitForStarted();
          QDataStream stream(&p);
          QByteArray data;
          stream << "this is a test" << QByteArray{};
          while (true) {
              stream.startTransaction();
              stream >> data;
              if (stream.commitTransaction()) {
                  dumpval(data);
                  if (data.isEmpty())
                      break;
              } else
                  p.waitForReadyRead();
          }
          p.waitForFinished();
      }
      int main(int argc, char** argv)
      {
          QCoreApplication app(argc, argv);
          qSetMessagePattern("%{appname}: %{message}");
          if (app.arguments().size() < 2) master(); else slave();
          qDebug() << "stopped";
      }
      #include "main.moc"
      

      The output, from DebugView, is (the very first entry is supposedly irrelavent):

      00000001    0.00000000    [12452] "game restart 0"    
      00000002    6.64763594    [6324] master: started    
      00000003    6.65141678    [6324] master: process started    
      00000004    6.67225885    [3024] slave: started    
      00000005    6.67228985    [3024] slave: input.open(stdin, QFile::ReadOnly) = true    
      00000006    6.67232132    [3024] slave: output.open(stdout, QFile::WriteOnly) = true    
      00000007    80.25774384    [3024] slave: data = "this is a test\x00"    
      00000008    80.25778961    [3024] slave: data = ""    
      00000009    80.25782776    [3024] slave: inputStream.status() = 0    
      00000010    80.25794220    [3024] slave: stopped  
      

      3 strange things I cannot understand;
      1. Everything stops at [3024] slave: output.open(stdout, QFile::WriteOnly) = true. The rest of output comes after I manually stop master.
      2. No master outputting the result from stream (never readyRead?)
      3. Master does not even output 'stopped' at the end.

      What I expect from output:

      master: started
      slave: started
      slave: input.open(stdin, QFile::ReadOnly) = true
      slave: output.open(stdout, QFile::WriteOnly) = true
      slave: data = "this is a test\x00"
      slave: data = ""
      slave: inputStream.status() = 0
      slave: stopped
      master: data = "this is a test\x00"
      master: data = ""
      master: stopped
      

      The output obtained on Linux is as expected (complete output from master and no hanging during outputting).

      Attachments

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

        Activity

          People

            thiago Thiago Macieira
            luqiaochen Luqiao Chen
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes