<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/59979>59979</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            sys::Wait may use closed handle on Windows when process statistics are requested
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            spavloff
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          andykaylor
      </td>
    </tr>
</table>

<pre>
    Support for process statistics was added to the sys::Wait function in this patch: https://reviews.llvm.org/D78901

If process statistics are needed, this code uses `GetProcessTimes(PI.Process, &CreationTime, &ExitTime, &KernelTime, &UserTime)` to get the program execution time. However, there are conditions under which this code may be reached after the `PI.Process` handle has been closed.

```
  DWORD WaitStatus = WaitForSingleObject(PI.Process, milliSecondsToWait);
  if (WaitStatus == WAIT_TIMEOUT) {
    if (SecondsToWait) {
      if (!TerminateProcess(PI.Process, 1)) {
 if (ErrMsg)
          MakeErrMsg(ErrMsg, "Failed to terminate timed-out program");

        // -2 indicates a crash or timeout as opposed to failure to execute.
        WaitResult.ReturnCode = -2;
 CloseHandle(PI.Process);
        return WaitResult;
      }
 **WaitForSingleObject(PI.Process, INFINITE);
 CloseHandle(PI.Process);**
    } else {
      // Non-blocking wait.
 return ProcessInfo();
    }
  }
```

I haven't seen this happen in a real scenario. It was reported by a static analysis tool.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVV1v6jgT_jXmZkQUHAhwkQtOKe9Br9oetRz1cjWxJ8RbY2dtpxz-_cpJKB-7q92oKnE888wzH36M3qu9ISrY7Bvj3Df4qW1VMc7ZbD3CNtTWFWjk6QNP2rpRaeWpeGubxroAlXXQOCvIe_ABg_JBCQ9H9IBSkoRgIdQE_uRZtmLZ6h1VgKo1IihrQBkItfLQYBA1y1ZQh9B0lnzD-MbRp6KjT7T-PCTW7RnfrOeLZTph6Zqlq_7_tvo7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