From fe709502f4ce922d9ee1e1cbebd0a3c41452406b Mon Sep 17 00:00:00 2001 From: Wiktor Garbacz Date: Thu, 5 Aug 2021 07:07:08 -0700 Subject: [PATCH] Wait for global forkserver when shutting it down Otherwise starting forkserver multiple times will result in zombie processes lingering around. PiperOrigin-RevId: 388926497 Change-Id: Ia9947cce3d9e909edd709b0d3525e1ae8b8bbc51 --- sandboxed_api/sandbox2/global_forkclient.cc | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/sandboxed_api/sandbox2/global_forkclient.cc b/sandboxed_api/sandbox2/global_forkclient.cc index 2107729..e3cec0a 100644 --- a/sandboxed_api/sandbox2/global_forkclient.cc +++ b/sandboxed_api/sandbox2/global_forkclient.cc @@ -19,6 +19,8 @@ #include #include #include +#include +#include #include #include @@ -192,9 +194,21 @@ void GlobalForkClient::ForceStart() { } void GlobalForkClient::Shutdown() { - absl::MutexLock lock(&GlobalForkClient::instance_mutex_); - delete instance_; - instance_ = nullptr; + pid_t pid = -1; + { + absl::MutexLock lock(&GlobalForkClient::instance_mutex_); + if (instance_) { + pid = instance_->fork_client_.pid(); + } + delete instance_; + instance_ = nullptr; + } + if (pid != -1) { + pid_t wpid = TEMP_FAILURE_RETRY(waitpid(pid, nullptr, 0)); + if (wpid != pid) { + SAPI_RAW_PLOG(ERROR, "Waiting for %d failed", pid); + } + } } pid_t GlobalForkClient::SendRequest(const ForkRequest& request, int exec_fd,