Set O_CLOEXEC by default when creating pipes to avoid race conditions resulting from...
authorRocky Liu <rockyliu@fb.com>
Tue, 13 May 2014 18:43:59 +0000 (11:43 -0700)
committerDave Watson <davejwatson@fb.com>
Tue, 20 May 2014 19:53:59 +0000 (12:53 -0700)
commit870912b89844e07283ff6d7e9602952f05ad3840
tree7b602642863e045d3ea881fc0f6ea9446bd7ef17
parentdb37af8437399c0578a30e5bf553f9fe231b5c82
Set O_CLOEXEC by default when creating pipes to avoid race conditions resulting from concurrent Subprocess creations

Summary:
[folly::Subprocess] Set O_CLOEXEC by default when creating pipes to avoid race conditions resulting from concurrent Subprocess creations

If multiple threads are creating Subprocess objects concurrently, the
write side file descriptor of the pipe created in the parent process
might be inherited into other child processes unintentionally and never
closed, causing the parent process to hang while reading from the read
side of its pipe, thinking the other side must have been closed.

The fix to the problem is to create the pipes and set O_CLOEXEC in
a single pipe2 call. Then the child could clear the O_CLOEXEC flag
selectively before calling exec().

Test Plan:
Existing unit tests of Subprocess
Added a new unit test which will hang in Subprocess constructor without
this fix.

Reviewed By: tudorb@fb.com

FB internal diff: D1267396
folly/Subprocess.cpp
folly/test/SubprocessTest.cpp