From 4724f14145eba57d71c08cc89b6a6a1ae78d9dba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Wed, 21 Aug 2019 23:24:02 +0200 Subject: [PATCH] Avoid interference with other users of waitpid. Instead of using waitpid(-1), explicitly waits on all known processes. This is inefficient for large numbers of child processes, but seems to be the only way to ensure to not interfere with other code that uses waitpid(). --- source/eventcore/drivers/posix/processes.d | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/source/eventcore/drivers/posix/processes.d b/source/eventcore/drivers/posix/processes.d index 0b4724c..9b82a06 100644 --- a/source/eventcore/drivers/posix/processes.d +++ b/source/eventcore/drivers/posix/processes.d @@ -273,12 +273,18 @@ final class SignalEventDriverProcesses(Loop : PosixEventLoop) : EventDriverProce } // instead, use waitpid to determine all exited processes - while (true) { - int status; - auto ret = () @trusted { return waitpid(-1, &status, WNOHANG); } (); - if (ret <= 0) break; + ProcessID[] allprocs; + () @trusted { + try synchronized (StaticProcesses.m_mutex) + allprocs = StaticProcesses.m_processes.keys; + catch (Exception e) assert(false, e.msg); + } (); - onProcessExit(ret, () @trusted { return WEXITSTATUS(status); } ()); + foreach (pid; allprocs) { + int status; + auto ret = () @trusted { return waitpid(cast(int)pid, &status, WNOHANG); } (); + if (ret == cast(int)pid) + onProcessExit(ret, () @trusted { return WEXITSTATUS(status); } ()); } }