subprocess now emits a ResourceWarning warning
Issue #26741: subprocess.Popen destructor now emits a ResourceWarning warning
if the child process is still running.
diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index 41a9de1..b853f4d 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -1006,6 +1006,9 @@
if not self._child_created:
# We didn't get to successfully create a child process.
return
+ if self.returncode is None:
+ warnings.warn("running subprocess %r" % self, ResourceWarning,
+ source=self)
# In case the child hasn't been waited on, check if it's done.
self._internal_poll(_deadstate=_maxsize)
if self.returncode is None and _active is not None:
diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py
index 5239e5a..a8f0a64 100644
--- a/Lib/test/test_subprocess.py
+++ b/Lib/test/test_subprocess.py
@@ -2286,7 +2286,9 @@
self.addCleanup(p.stderr.close)
ident = id(p)
pid = p.pid
- del p
+ with support.check_warnings(('', ResourceWarning)):
+ p = None
+
# check that p is in the active processes list
self.assertIn(ident, [id(o) for o in subprocess._active])
@@ -2305,7 +2307,9 @@
self.addCleanup(p.stderr.close)
ident = id(p)
pid = p.pid
- del p
+ with support.check_warnings(('', ResourceWarning)):
+ p = None
+
os.kill(pid, signal.SIGKILL)
# check that p is in the active processes list
self.assertIn(ident, [id(o) for o in subprocess._active])