Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Unable to start a QProcess again
Qt 6.11 is out! See what's new in the release blog

Unable to start a QProcess again

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 1.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • PothosP Offline
    PothosP Offline
    Pothos
    wrote on last edited by
    #1

    I am an experimenting with different APIs of QT. In the private-section of my class I have a new process instance declared as follows:

    QProcess *myProcess;
    

    Then I initialize it in the constructor of said class as follows:

    myProcess = new QProcess();
    

    I have a button in the GUI that executes the program rsync when pressed. Here is the most relevant section of the code that it executes:

    if (myProcess->state() != QProcess::NotRunning) {
          qDebug() << "Can't start a new process while the old one is still running.";
          return;
    }
    
    connect(myProcess, &QProcess::readyReadStandardOutput, [=](){
            ui->consoleTextBrowser->append(myProcess->readAllStandardOutput());
    });
    
    connect(myProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), [=](int exitCode, QProcess::ExitStatus exitStatus) {
            qDebug() << exitCode;
            qDebug() << exitStatus;
            myProcess->deleteLater();
    });
    
    myProcess->start(programString, rsyncArguments);
    

    The purpose of the if-sentence in the beginning is to make sure that I am unable to start a new rsync process. Or at least that is my intention.

    Everything works fine the first time I run this. However, when I run this for the second time, the program crashes due to a segmentation fault. To my understanding the crash happens when the first connect is called.

    What am I doing wrong here? Am I using these connect-calls the wrong way? Should I somehow disconnect them before running rsync again?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      @Pothos said in Unable to start a QProcess again:

      connect(myProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), [=](int exitCode, QProcess::ExitStatus exitStatus) {
      qDebug() << exitCode;
      qDebug() << exitStatus;
      myProcess->deleteLater();
      });

      You're nuking your myProcess object and likely not recreating it.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • PothosP Offline
        PothosP Offline
        Pothos
        wrote on last edited by
        #3

        @SGaist
        Oh, well damn it. I thought I was supposed to do that. Seems to fix the issue...

        However, I have an additional question related to this exact same topic:
        How should I terminate an ongoing rsync-process started by this method, if I want to be able to run it again if I so choose? Is myProcess->terminate() safe to call mid-execution?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by SGaist
          #4

          Terminate sends a SIGKILL which is likely not what you want.

          My memory failed me (good to re-read the documentation from time to time). So SIGTERM is used by QProcess.

          You can use the standard kill method with SIGINT on the pid of the application that you can get from QProcess.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          PothosP 1 Reply Last reply
          2
          • SGaistS SGaist

            Terminate sends a SIGKILL which is likely not what you want.

            My memory failed me (good to re-read the documentation from time to time). So SIGTERM is used by QProcess.

            You can use the standard kill method with SIGINT on the pid of the application that you can get from QProcess.

            PothosP Offline
            PothosP Offline
            Pothos
            wrote on last edited by
            #5

            @SGaist
            Thanks. Does SIGKILLed rsync leave any unnecessary files left or cause memory leak though?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Memory leak should not happen however there will be no cleanup done. SIGKILL does not ask your process to stop in a nice and polite manner (that's the role of SIGINT), it executes it.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              JonBJ 1 Reply Last reply
              1
              • SGaistS SGaist

                Memory leak should not happen however there will be no cleanup done. SIGKILL does not ask your process to stop in a nice and polite manner (that's the role of SIGINT), it executes it.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @SGaist
                What do you think about sending SIGTERM rather than SIGINT?

                https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html

                Macro: int SIGTERM

                The SIGTERM signal is a generic signal used to cause program termination. Unlike SIGKILL, this signal can be blocked, handled, and ignored. It is the normal way to politely ask a program to terminate.

                The shell command kill generates SIGTERM by default.

                Macro: int SIGINT

                The SIGINT (“program interrupt”) signal is sent when the user types the INTR character (normally C-c). See Special Characters, for information about terminal driver support for C-c.

                https://www.quora.com/What-is-the-difference-between-the-SIGINT-and-SIGTERM-signals-in-Linux-What’s-the-difference-between-the-SIGKILL-and-SIGSTOP-signals

                SITERM
                According to ISO/IEC 9899:2011: a termination request sent to the program *

                SIGINT
                According to ISO/IEC 9899:2011: receipt of an interactive attention signal *

                @Pothos
                You can hope/assume rsync will clean itself up. So long as you don't actually nuke it!

                You should test which signals work best with it.

                SGaistS 1 Reply Last reply
                2
                • JonBJ JonB

                  @SGaist
                  What do you think about sending SIGTERM rather than SIGINT?

                  https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html

                  Macro: int SIGTERM

                  The SIGTERM signal is a generic signal used to cause program termination. Unlike SIGKILL, this signal can be blocked, handled, and ignored. It is the normal way to politely ask a program to terminate.

                  The shell command kill generates SIGTERM by default.

                  Macro: int SIGINT

                  The SIGINT (“program interrupt”) signal is sent when the user types the INTR character (normally C-c). See Special Characters, for information about terminal driver support for C-c.

                  https://www.quora.com/What-is-the-difference-between-the-SIGINT-and-SIGTERM-signals-in-Linux-What’s-the-difference-between-the-SIGKILL-and-SIGSTOP-signals

                  SITERM
                  According to ISO/IEC 9899:2011: a termination request sent to the program *

                  SIGINT
                  According to ISO/IEC 9899:2011: receipt of an interactive attention signal *

                  @Pothos
                  You can hope/assume rsync will clean itself up. So long as you don't actually nuke it!

                  You should test which signals work best with it.

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @JonB excellent point, that's indeed also a good if not better idea.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  PothosP 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    @JonB excellent point, that's indeed also a good if not better idea.

                    PothosP Offline
                    PothosP Offline
                    Pothos
                    wrote on last edited by
                    #9

                    @SGaist
                    @JonB
                    I see. So QProcess::terminate() is the way to go since it sends a SIGTERM to the process?

                    JonBJ 2 Replies Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      (Updated my answer above)

                      I would test and compare the two to see which one does what you want best.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      2
                      • PothosP Pothos

                        @SGaist
                        @JonB
                        I see. So QProcess::terminate() is the way to go since it sends a SIGTERM to the process?

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #11

                        @Pothos
                        Indeed:

                        On Unix and macOS the SIGTERM signal is sent.

                        1 Reply Last reply
                        1
                        • PothosP Pothos

                          @SGaist
                          @JonB
                          I see. So QProcess::terminate() is the way to go since it sends a SIGTERM to the process?

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #12

                          @Pothos
                          You should indeed test to see rsync's behaviour. QProcess::terminate() is easy to call, I think to send a SIGINT you will have to use the Linux kill(2) function, I think @SGaist was indicating that.

                          1 Reply Last reply
                          1

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved