From bdba84fdf62cd9916a289bcda59e52529194b6d8 Mon Sep 17 00:00:00 2001 From: Haowen Liu Date: Sat, 26 Apr 2025 16:26:42 -0400 Subject: [PATCH] Proper implementation of wait() on Windows This commit makes sure: 1. WaitForSingleObject returns with expected code before proceeding. 2. Process handle is properly closed. --- subprocess.hpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/subprocess.hpp b/subprocess.hpp index 3975eb6..073f075 100755 --- a/subprocess.hpp +++ b/subprocess.hpp @@ -1423,11 +1423,18 @@ inline int Popen::wait() noexcept(false) #ifdef __USING_WINDOWS__ int ret = WaitForSingleObject(process_handle_, INFINITE); + // WaitForSingleObject with INFINITE should only return when process has signaled + if (ret != WAIT_OBJECT_0) { + throw OSError("Unexpected return code from WaitForSingleObject", 0); + } + DWORD dretcode_; if (FALSE == GetExitCodeProcess(process_handle_, &dretcode_)) throw OSError("Failed during call to GetExitCodeProcess", 0); + CloseHandle(process_handle_); + return (int)dretcode_; #else int ret, status;