From 6ce9868a9c3555a2aa848da53bbc1a14f642d468 Mon Sep 17 00:00:00 2001 From: Ashod Nakashian Date: Thu, 19 May 2016 20:27:24 -0400 Subject: [PATCH] loolwsd: more frequent zombie reaping in forkit Change-Id: I10d859880cfd933c38572852f506eedf603e8fad Reviewed-on: https://gerrit.libreoffice.org/25232 Reviewed-by: Ashod Nakashian Tested-by: Ashod Nakashian --- loolwsd/LOOLForKit.cpp | 44 +++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/loolwsd/LOOLForKit.cpp b/loolwsd/LOOLForKit.cpp index f0e5dc3ca..4ff57cfc3 100644 --- a/loolwsd/LOOLForKit.cpp +++ b/loolwsd/LOOLForKit.cpp @@ -112,6 +112,26 @@ private: } }; +/// Check if some previously forked kids have died. +static void cleanupChildren() +{ + Process::PID exitedChildPid; + int status; + while ((exitedChildPid = waitpid(-1, &status, WNOHANG)) > 0) + { + if (childJails.find(exitedChildPid) != childJails.end()) + { + Log::info("Child " + std::to_string(exitedChildPid) + " has exited, removing its jail '" + childJails[exitedChildPid] + "'"); + Util::removeFile(childJails[exitedChildPid], true); + childJails.erase(exitedChildPid); + } + else + { + Log::error("Unknown child " + std::to_string(exitedChildPid) + " has exited"); + } + } +} + static int createLibreOfficeKit(const std::string& childRoot, const std::string& sysTemplate, const std::string& loTemplate, @@ -142,26 +162,10 @@ static int createLibreOfficeKit(const std::string& childRoot, else { // Parent - - // Check if some previously forked kids have died - Process::PID exitedChildPid; - int status; - while ((exitedChildPid = waitpid(-1, &status, WNOHANG)) > 0) - { - if (childJails.find(exitedChildPid) != childJails.end()) - { - Log::info("Child " + std::to_string(exitedChildPid) + " has exited, removing its jail '" + childJails[exitedChildPid] + "'"); - Util::removeFile(childJails[exitedChildPid], true); - childJails.erase(exitedChildPid); - } - else - { - Log::error("Unknown child " + std::to_string(exitedChildPid) + " has exited"); - } - } - if (pid < 0) + { Log::syserror("Fork failed."); + } else { Log::info("Forked kit [" + std::to_string(pid) + "]."); @@ -343,6 +347,10 @@ int main(int argc, char** argv) // If we need to spawn more, retry later. ForkCounter = (newInstances >= ForkCounter ? 0 : ForkCounter - newInstances); } + else + { + cleanupChildren(); + } } close(pipeFd);