arun11299 / cpp-subprocess

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

Child process does not exit in case of execve failure #29

Open Overv opened 5 years ago

Overv commented 5 years ago

Consider the following code:

#include "subprocess.hpp"

int main() {
    try {
        auto p = subprocess::Popen({"nonexistent-binary"});
        p.wait()

    } catch (const std::runtime_error& e) {
        std::cout << "exception triggered: " << e.what() << std::endl;
    }

    return 0;
}

When running this, it will print an exception message twice:

$ make test
g++ -g -std=c++17 -o subprocess main.cpp -pthread
./subprocess
exception triggered: execve failed : No such file or directory
exception triggered: execve failed : No such file or directory

The exception appears to be thrown in both the parent and the child process in case of execve failure. This seems like a bug as a result of not exiting the forked process.