From c45e309cee3580b5c7f0632e2993107827f08515 Mon Sep 17 00:00:00 2001 From: zousar <2936246+zousar@users.noreply.github.com> Date: Tue, 27 Jun 2023 01:50:28 -0600 Subject: Fix IsProcessRunning on Windows (#335) IsProcessRunning on Windows would only consider if we could get a handle to a process. It is possible to get a handle to a process even if it is terminated in Windows. To actually know if the process is running, a further call to GetExitCodeProces is required. Addressing this issue ensures that the ZenServerState::Sweep method doesn't keep terminated processes in the state table. --- src/zencore/thread.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src/zencore/thread.cpp') diff --git a/src/zencore/thread.cpp b/src/zencore/thread.cpp index 67a0878d0..a44c0ee90 100644 --- a/src/zencore/thread.cpp +++ b/src/zencore/thread.cpp @@ -1065,9 +1065,20 @@ IsProcessRunning(int pid) ThrowSystemError(Error, fmt::format("failed to open process with pid {}", pid)); } + bool bStillActive = true; + DWORD ExitCode = 0; + if (0 != GetExitCodeProcess(hProc, &ExitCode)) + { + bStillActive = ExitCode == STILL_ACTIVE; + } + else + { + ZEN_WARN("Unable to get exit code from handle for process '{}', treating the process as active", pid); + } + CloseHandle(hProc); - return true; + return bStillActive; #elif ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC return (kill(pid_t(pid), 0) == 0); #endif -- cgit v1.2.3