Proper implementation of wait() on Windows

This commit makes sure:
1. WaitForSingleObject returns with expected
code before proceeding.
2. Process handle is properly closed.
This commit is contained in:
Haowen Liu 2025-04-26 16:26:42 -04:00
parent ed313971c0
commit bdba84fdf6
No known key found for this signature in database
GPG Key ID: F65B4067F3357C78

View File

@ -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;