mirror of
https://github.com/python/cpython.git
synced 2025-08-15 14:20:55 +00:00
bpo-37380: subprocess: don't use _active on win (GH-14360) (GH-15707)
As noted by @eryksun in [1] and [2], using _cleanup and _active(in
__del__) is not necessary on Windows, since:
> Unlike Unix, a process in Windows doesn't have to be waited on by
> its parent to avoid a zombie. Keeping the handle open will actually
> create a zombie until the next _cleanup() call, which may be never
> if Popen() isn't called again.
This patch simply defines `subprocess._active` as `None`, for which we already
have the proper logic in place in `subprocess.Popen.__del__`, that prevents it
from trying to append the process to the `_active`. This patch also defines
`subprocess._cleanup` as a noop for Windows.
[1] https://bugs.python.org/issue37380GH-msg346333
[2] https://bugs.python.org/issue36067GH-msg336262
Signed-off-by: Ruslan Kuprieiev <ruslan@iterative.ai>
(cherry picked from commit 042821ae3c
)
Co-authored-by: Ruslan Kuprieiev <kupruser@gmail.com>
This commit is contained in:
parent
b8c66779c7
commit
4d1abedce9
3 changed files with 58 additions and 24 deletions
|
@ -218,22 +218,38 @@ else:
|
||||||
_PopenSelector = selectors.SelectSelector
|
_PopenSelector = selectors.SelectSelector
|
||||||
|
|
||||||
|
|
||||||
# This lists holds Popen instances for which the underlying process had not
|
if _mswindows:
|
||||||
# exited at the time its __del__ method got called: those processes are wait()ed
|
# On Windows we just need to close `Popen._handle` when we no longer need
|
||||||
# for synchronously from _cleanup() when a new Popen object is created, to avoid
|
# it, so that the kernel can free it. `Popen._handle` gets closed
|
||||||
# zombie processes.
|
# implicitly when the `Popen` instance is finalized (see `Handle.__del__`,
|
||||||
_active = []
|
# which is calling `CloseHandle` as requested in [1]), so there is nothing
|
||||||
|
# for `_cleanup` to do.
|
||||||
|
#
|
||||||
|
# [1] https://docs.microsoft.com/en-us/windows/desktop/ProcThread/
|
||||||
|
# creating-processes
|
||||||
|
_active = None
|
||||||
|
|
||||||
def _cleanup():
|
def _cleanup():
|
||||||
for inst in _active[:]:
|
pass
|
||||||
res = inst._internal_poll(_deadstate=sys.maxsize)
|
else:
|
||||||
if res is not None:
|
# This lists holds Popen instances for which the underlying process had not
|
||||||
try:
|
# exited at the time its __del__ method got called: those processes are
|
||||||
_active.remove(inst)
|
# wait()ed for synchronously from _cleanup() when a new Popen object is
|
||||||
except ValueError:
|
# created, to avoid zombie processes.
|
||||||
# This can happen if two threads create a new Popen instance.
|
_active = []
|
||||||
# It's harmless that it was already removed, so ignore.
|
|
||||||
pass
|
def _cleanup():
|
||||||
|
if _active is None:
|
||||||
|
return
|
||||||
|
for inst in _active[:]:
|
||||||
|
res = inst._internal_poll(_deadstate=sys.maxsize)
|
||||||
|
if res is not None:
|
||||||
|
try:
|
||||||
|
_active.remove(inst)
|
||||||
|
except ValueError:
|
||||||
|
# This can happen if two threads create a new Popen instance.
|
||||||
|
# It's harmless that it was already removed, so ignore.
|
||||||
|
pass
|
||||||
|
|
||||||
PIPE = -1
|
PIPE = -1
|
||||||
STDOUT = -2
|
STDOUT = -2
|
||||||
|
|
|
@ -52,10 +52,14 @@ class BaseTestCase(unittest.TestCase):
|
||||||
support.reap_children()
|
support.reap_children()
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
for inst in subprocess._active:
|
if not mswindows:
|
||||||
inst.wait()
|
# subprocess._active is not used on Windows and is set to None.
|
||||||
subprocess._cleanup()
|
for inst in subprocess._active:
|
||||||
self.assertFalse(subprocess._active, "subprocess._active not empty")
|
inst.wait()
|
||||||
|
subprocess._cleanup()
|
||||||
|
self.assertFalse(
|
||||||
|
subprocess._active, "subprocess._active not empty"
|
||||||
|
)
|
||||||
self.doCleanups()
|
self.doCleanups()
|
||||||
support.reap_children()
|
support.reap_children()
|
||||||
|
|
||||||
|
@ -2672,8 +2676,12 @@ class POSIXProcessTestCase(BaseTestCase):
|
||||||
with support.check_warnings(('', ResourceWarning)):
|
with support.check_warnings(('', ResourceWarning)):
|
||||||
p = None
|
p = None
|
||||||
|
|
||||||
# check that p is in the active processes list
|
if mswindows:
|
||||||
self.assertIn(ident, [id(o) for o in subprocess._active])
|
# subprocess._active is not used on Windows and is set to None.
|
||||||
|
self.assertIsNone(subprocess._active)
|
||||||
|
else:
|
||||||
|
# check that p is in the active processes list
|
||||||
|
self.assertIn(ident, [id(o) for o in subprocess._active])
|
||||||
|
|
||||||
def test_leak_fast_process_del_killed(self):
|
def test_leak_fast_process_del_killed(self):
|
||||||
# Issue #12650: on Unix, if Popen.__del__() was called before the
|
# Issue #12650: on Unix, if Popen.__del__() was called before the
|
||||||
|
@ -2694,8 +2702,12 @@ class POSIXProcessTestCase(BaseTestCase):
|
||||||
p = None
|
p = None
|
||||||
|
|
||||||
os.kill(pid, signal.SIGKILL)
|
os.kill(pid, signal.SIGKILL)
|
||||||
# check that p is in the active processes list
|
if mswindows:
|
||||||
self.assertIn(ident, [id(o) for o in subprocess._active])
|
# subprocess._active is not used on Windows and is set to None.
|
||||||
|
self.assertIsNone(subprocess._active)
|
||||||
|
else:
|
||||||
|
# check that p is in the active processes list
|
||||||
|
self.assertIn(ident, [id(o) for o in subprocess._active])
|
||||||
|
|
||||||
# let some time for the process to exit, and create a new Popen: this
|
# let some time for the process to exit, and create a new Popen: this
|
||||||
# should trigger the wait() of p
|
# should trigger the wait() of p
|
||||||
|
@ -2707,7 +2719,11 @@ class POSIXProcessTestCase(BaseTestCase):
|
||||||
pass
|
pass
|
||||||
# p should have been wait()ed on, and removed from the _active list
|
# p should have been wait()ed on, and removed from the _active list
|
||||||
self.assertRaises(OSError, os.waitpid, pid, 0)
|
self.assertRaises(OSError, os.waitpid, pid, 0)
|
||||||
self.assertNotIn(ident, [id(o) for o in subprocess._active])
|
if mswindows:
|
||||||
|
# subprocess._active is not used on Windows and is set to None.
|
||||||
|
self.assertIsNone(subprocess._active)
|
||||||
|
else:
|
||||||
|
self.assertNotIn(ident, [id(o) for o in subprocess._active])
|
||||||
|
|
||||||
def test_close_fds_after_preexec(self):
|
def test_close_fds_after_preexec(self):
|
||||||
fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
|
fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Don't collect unfinished processes with ``subprocess._active`` on Windows to
|
||||||
|
cleanup later. Patch by Ruslan Kuprieiev.
|
Loading…
Add table
Add a link
Reference in a new issue