[libc-commits] [libc] 1756657 - [libc] Resolve race condition in sub-process test runner.

Paula Toth via libc-commits libc-commits at lists.llvm.org
Tue Mar 17 13:00:15 PDT 2020


Author: Paula Toth
Date: 2020-03-17T13:00:00-07:00
New Revision: 17566573b2997e468cb4190c6853b1ebea125962

URL: https://github.com/llvm/llvm-project/commit/17566573b2997e468cb4190c6853b1ebea125962
DIFF: https://github.com/llvm/llvm-project/commit/17566573b2997e468cb4190c6853b1ebea125962.diff

LOG: [libc] Resolve race condition in sub-process test runner.

Summary:
There seems to be a race condition between the pipe closing and the child process death. Likely these two events are not atomic on some versions of linux.

With the removal of `WNOHANG` we eliminate the race condition, however if the child closes the pipe intentionally then it could result in the test runner hanging. I find this situation less likely, where as I experience failures locally with this race condition rather consistently.

Reviewers: sivachandra, abrachet

Reviewed By: sivachandra, abrachet

Subscribers: MaskRay, jfb, tschuett, libc-commits

Tags: #libc-project

Differential Revision: https://reviews.llvm.org/D76267

Added: 
    

Modified: 
    libc/utils/testutils/ExecuteFunctionUnix.cpp

Removed: 
    


################################################################################
diff  --git a/libc/utils/testutils/ExecuteFunctionUnix.cpp b/libc/utils/testutils/ExecuteFunctionUnix.cpp
index e920e50f7147..1ddb24d812d7 100644
--- a/libc/utils/testutils/ExecuteFunctionUnix.cpp
+++ b/libc/utils/testutils/ExecuteFunctionUnix.cpp
@@ -67,8 +67,12 @@ ProcessStatus invokeInSubprocess(FunctionCaller *Func, unsigned timeoutMS) {
   }
 
   int WStatus = 0;
-  int status = ::waitpid(Pid, &WStatus, WNOHANG);
-  assert(status == Pid && "wait call should not block here");
+  // Wait on the pid of the subprocess here so it gets collected by the system
+  // and doesn't turn into a zombie.
+  pid_t status = ::waitpid(Pid, &WStatus, 0);
+  if (status == -1)
+    return ProcessStatus::Error("waitpid(2) failed");
+  assert(status == Pid);
   (void)status;
   return {WStatus};
 }


        


More information about the libc-commits mailing list