Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5a48e21

Browse files
committed
subprocess now emits a ResourceWarning warning
Issue #26741: subprocess.Popen destructor now emits a ResourceWarning warning if the child process is still running.
1 parent a58e2c5 commit 5a48e21

5 files changed

Lines changed: 26 additions & 2 deletions

File tree

Doc/library/subprocess.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,10 @@ functions.
497497
.. versionchanged:: 3.2
498498
Added context manager support.
499499

500+
.. versionchanged:: 3.6
501+
Popen destructor now emits a :exc:`ResourceWarning` warning if the child
502+
process is still running.
503+
500504

501505
Exceptions
502506
^^^^^^^^^^

Doc/whatsnew/3.6.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,16 @@ protocol.
330330
(Contributed by Aviv Palivoda in :issue:`26404`.)
331331

332332

333+
subprocess
334+
----------
335+
336+
:class:`subprocess.Popen` destructor now emits a :exc:`ResourceWarning` warning
337+
if the child process is still running. Use the context manager protocol (``with
338+
proc: ...``) or call explicitly the :meth:`~subprocess.Popen.wait` method to
339+
read the exit status of the child process (Contributed by Victor Stinner in
340+
:issue:`26741`).
341+
342+
333343
telnetlib
334344
---------
335345

Lib/subprocess.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,9 @@ def __del__(self, _maxsize=sys.maxsize):
10061006
if not self._child_created:
10071007
# We didn't get to successfully create a child process.
10081008
return
1009+
if self.returncode is None:
1010+
warnings.warn("running subprocess %r" % self, ResourceWarning,
1011+
source=self)
10091012
# In case the child hasn't been waited on, check if it's done.
10101013
self._internal_poll(_deadstate=_maxsize)
10111014
if self.returncode is None and _active is not None:

Lib/test/test_subprocess.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,7 +2286,9 @@ def test_zombie_fast_process_del(self):
22862286
self.addCleanup(p.stderr.close)
22872287
ident = id(p)
22882288
pid = p.pid
2289-
del p
2289+
with support.check_warnings(('', ResourceWarning)):
2290+
p = None
2291+
22902292
# check that p is in the active processes list
22912293
self.assertIn(ident, [id(o) for o in subprocess._active])
22922294

@@ -2305,7 +2307,9 @@ def test_leak_fast_process_del_killed(self):
23052307
self.addCleanup(p.stderr.close)
23062308
ident = id(p)
23072309
pid = p.pid
2308-
del p
2310+
with support.check_warnings(('', ResourceWarning)):
2311+
p = None
2312+
23092313
os.kill(pid, signal.SIGKILL)
23102314
# check that p is in the active processes list
23112315
self.assertIn(ident, [id(o) for o in subprocess._active])

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Core and Builtins
1616
Library
1717
-------
1818

19+
- Issue #26741: subprocess.Popen destructor now emits a ResourceWarning warning
20+
if the child process is still running.
21+
1922
- Issue #27056: Optimize pickle.load() and pickle.loads(), up to 10% faster
2023
to deserialize a lot of small objects.
2124

0 commit comments

Comments
 (0)