skystrife / procxx

A simple process management library for C++ on UNIX platforms.
MIT License
143 stars 28 forks source link

wait() should be documented as not thread-safe #19

Open moberhuber opened 5 years ago

moberhuber commented 5 years ago

We had a case where multiple Threads performed wait() on the same child process (one to just print the child's exit code, the other to do the next job after child was done). This worked most of the time, but sometimes the program would crash with a pipe_t:write failed exception from underneath flush():

pipe_t::write(): Bad file descriptor
terminate called after throwing an instance of 'procxx::pipe_t::exception'
  what():  failed to write
Aborted (core dumped)

I think the problem is, that inside wait(), the waited_ boolean is not mutex protected. So if 2 threads call wait() at almost the same time, the stdin_pipe is closed twice and the 2nd close fails.

I would not suggest adding a mutex for waited_ since I think waiting in different threads is not a good idea anyways; so performance should not be wasted here. But adding a line of comment to the wait() method saying that it is not thread-safe might help adopters avoid this trap in the future.