mirror of
https://github.com/python/cpython.git
synced 2025-09-01 06:28:36 +00:00
gh-113009: Fix multiprocessing Process.terminate() on Windows (#113128)
On Windows, Process.terminate() no longer sets the returncode attribute to always call WaitForSingleObject() in Process.wait(). Previously, sometimes the process was still running after TerminateProcess() even if GetExitCodeProcess() is not STILL_ACTIVE.
This commit is contained in:
parent
d1a2adfb08
commit
4026ad5b2c
2 changed files with 34 additions and 23 deletions
|
@ -101,7 +101,9 @@ class Popen(object):
|
||||||
return reduction.duplicate(handle, self.sentinel)
|
return reduction.duplicate(handle, self.sentinel)
|
||||||
|
|
||||||
def wait(self, timeout=None):
|
def wait(self, timeout=None):
|
||||||
if self.returncode is None:
|
if self.returncode is not None:
|
||||||
|
return self.returncode
|
||||||
|
|
||||||
if timeout is None:
|
if timeout is None:
|
||||||
msecs = _winapi.INFINITE
|
msecs = _winapi.INFINITE
|
||||||
else:
|
else:
|
||||||
|
@ -120,7 +122,9 @@ class Popen(object):
|
||||||
return self.wait(timeout=0)
|
return self.wait(timeout=0)
|
||||||
|
|
||||||
def terminate(self):
|
def terminate(self):
|
||||||
if self.returncode is None:
|
if self.returncode is not None:
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
_winapi.TerminateProcess(int(self._handle), TERMINATE)
|
_winapi.TerminateProcess(int(self._handle), TERMINATE)
|
||||||
except PermissionError:
|
except PermissionError:
|
||||||
|
@ -129,9 +133,11 @@ class Popen(object):
|
||||||
code = _winapi.GetExitCodeProcess(int(self._handle))
|
code = _winapi.GetExitCodeProcess(int(self._handle))
|
||||||
if code == _winapi.STILL_ACTIVE:
|
if code == _winapi.STILL_ACTIVE:
|
||||||
raise
|
raise
|
||||||
self.returncode = code
|
|
||||||
else:
|
# gh-113009: Don't set self.returncode. Even if GetExitCodeProcess()
|
||||||
self.returncode = -signal.SIGTERM
|
# returns an exit code different than STILL_ACTIVE, the process can
|
||||||
|
# still be running. Only set self.returncode once WaitForSingleObject()
|
||||||
|
# returns WAIT_OBJECT_0 in wait().
|
||||||
|
|
||||||
kill = terminate
|
kill = terminate
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
:mod:`multiprocessing`: On Windows, fix a race condition in
|
||||||
|
``Process.terminate()``: no longer set the ``returncode`` attribute to
|
||||||
|
always call ``WaitForSingleObject()`` in ``Process.wait()``. Previously,
|
||||||
|
sometimes the process was still running after ``TerminateProcess()`` even if
|
||||||
|
``GetExitCodeProcess()`` is not ``STILL_ACTIVE``. Patch by Victor Stinner.
|
Loading…
Add table
Add a link
Reference in a new issue