Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index f0ef30e..6cc3fa1 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -1016,7 +1016,17 @@
def terminate(self):
"""Terminates the process
"""
- _subprocess.TerminateProcess(self._handle, 1)
+ try:
+ _subprocess.TerminateProcess(self._handle, 1)
+ except OSError as e:
+ # ERROR_ACCESS_DENIED (winerror 5) is received when the
+ # process already died.
+ if e.winerror != 5:
+ raise
+ rc = _subprocess.GetExitCodeProcess(self._handle)
+ if rc == _subprocess.STILL_ACTIVE:
+ raise
+ self.returncode = rc
kill = terminate