arun11299 / cpp-subprocess

Subprocessing with modern C++
Other
456 stars 90 forks source link

Forked subprocess not exited if execve failed #54

Closed dilshodm closed 4 years ago

dilshodm commented 4 years ago

In lengthy processes (daemons, etc) this bug keeps open this subprocess. The subprocess continues to work (parallel to parent process) from after failed command start. For example, consider this simple test application:

#include <iostream>
#include <thread>
#include "subprocess.hpp"

using namespace std;
using namespace subprocess;

int main()
{
    std::vector<char> charVec;
    int retcode = 0;

    cout << "Hello World!" << endl;
    try {
        auto popen = Popen({"/bin/nonexistentcommand"}, output{PIPE}, error{PIPE}, shell{false});
        charVec = popen.communicate().first.buf;
        retcode = popen.retcode();
    } catch (const exception &) {
    }

    if (!charVec.empty()) {
        cout << charVec.data() << endl;
    }
    cout << "retcode " << retcode << endl;
    while (true)
        this_thread::sleep_for(0.5s);

    return 0;
}

if it tries to run nonexistentcommand, and stay in loop. When nonexistentcommand failed it will keep open second process. When main process killed, then second process still will be running.

dilshodm commented 4 years ago

Also deadlock on multithreaded applications fixed