From b7288decdf534391b3f917cfb11ec62580407c3f Mon Sep 17 00:00:00 2001 From: Haowen Liu <35328328+lunacd@users.noreply.github.com> Date: Thu, 1 May 2025 22:15:57 +0100 Subject: [PATCH] subprocess: Proper implementation of wait() on Windows This commit makes sure: 1. WaitForSingleObject returns with expected code before proceeding. 2. Process handle is properly closed. Github-Pull: arun11299/cpp-subprocess#116 Rebased-From: 625a8775791e62736f20f3fa3e6cc4f1b24aa89a --- src/util/subprocess.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/util/subprocess.h b/src/util/subprocess.h index 6fa84af2af7..e8b7989ce87 100644 --- a/src/util/subprocess.h +++ b/src/util/subprocess.h @@ -1061,11 +1061,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;