arun11299 / cpp-subprocess

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

is cpp-subprocess thread safe? #20

Closed lordadamson closed 5 years ago

lordadamson commented 5 years ago

Or does it provide the option of thread safety?

In my code I'm using an openmp loop where I would like to call sp::Popen.

It looks something like this:

#pragma omp parallel for
for(size_t i = 0; i < v.size(); i++)
{
    auto p = Popen({"subprocess"}, input{PIPE});
    p.communicate(v[i].c_str(), v[i].size() + 1);
}

where v is a std::vector<std::string> but it hangs randomly at the fork() call in subprocess.hpp:1159

Thanks in advance and thanks for the amazing library.

arun11299 commented 5 years ago

Hey! Thanks for reporting the issue. Also, Merry Christmas!! I will take a look at it and get back to you.

lordadamson commented 5 years ago

Merry Christmas to you too :smile:

arun11299 commented 5 years ago

I tried reproducing this with std::thread instead of Open MP:

void test_multithreaded_exec()
  {
    auto thread_fn = []() {
      auto p = sp::Popen({"cat", "-"}, sp::input{sp::PIPE}, sp::output{sp::PIPE});
      auto msg = "through stdin to stdout";
      auto res_buf = p.communicate(msg, strlen(msg)).first;
      assert(res_buf.length == strlen(msg));
    };

    std::vector<std::thread> threads;
    for (int i = 0; i < 100; i++) {
      threads.emplace_back(std::thread{thread_fn});
    }
    for (int i=0; i < 100; i++) {
      threads[i].join();
    }
    std::cout << "END_TEST" << std::endl;
  }

Popen object if used within a thread context should be thread safe. Is there any Open MP specific thing going on here ?

lordadamson commented 5 years ago

Hi @arun11299 I failed to reproduce the issue on an MCVE. That suggests that there is a memory corruption happening somewhere else in my code that's causing the issue.

Thanks a lot for looking into this. I'll investigate further and will let you know if I have any useful findings.

arun11299 commented 5 years ago

Sure. Let me know.