[Python-checkins] bpo-35721: Close socket pair if Popen in _UnixSubprocessTransport fails (GH-11553)

Miss Islington (bot) webhook-mailer at python.org
Mon May 20 08:02:22 EDT 2019


https://github.com/python/cpython/commit/9932fd91e878b740704ff599522e945a4bbe2ae1
commit: 9932fd91e878b740704ff599522e945a4bbe2ae1
branch: master
author: Niklas Fiekas <niklas.fiekas at backscattering.de>
committer: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
date: 2019年05月20日T05:02:16-07:00
summary:
bpo-35721: Close socket pair if Popen in _UnixSubprocessTransport fails (GH-11553)
This slightly expands an existing test case `test_popen_error` to trigger a `ResourceWarning` and fixes it.
https://bugs.python.org/issue35721
files:
A Misc/NEWS.d/next/Library/2019-01-18-16-23-00.bpo-35721.d8djAJ.rst
M Lib/asyncio/unix_events.py
M Lib/test/test_asyncio/test_subprocess.py
M Misc/ACKS
diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py
index 73d4bda7c234..1aa3b396086c 100644
--- a/Lib/asyncio/unix_events.py
+++ b/Lib/asyncio/unix_events.py
@@ -757,12 +757,18 @@ def _start(self, args, shell, stdin, stdout, stderr, bufsize, **kwargs):
 # other end). Notably this is needed on AIX, and works
 # just fine on other platforms.
 stdin, stdin_w = socket.socketpair()
- self._proc = subprocess.Popen(
- args, shell=shell, stdin=stdin, stdout=stdout, stderr=stderr,
- universal_newlines=False, bufsize=bufsize, **kwargs)
- if stdin_w is not None:
- stdin.close()
- self._proc.stdin = open(stdin_w.detach(), 'wb', buffering=bufsize)
+ try:
+ self._proc = subprocess.Popen(
+ args, shell=shell, stdin=stdin, stdout=stdout, stderr=stderr,
+ universal_newlines=False, bufsize=bufsize, **kwargs)
+ if stdin_w is not None:
+ stdin.close()
+ self._proc.stdin = open(stdin_w.detach(), 'wb', buffering=bufsize)
+ stdin_w = None
+ finally:
+ if stdin_w is not None:
+ stdin.close()
+ stdin_w.close()
 
 
 class AbstractChildWatcher:
diff --git a/Lib/test/test_asyncio/test_subprocess.py b/Lib/test/test_asyncio/test_subprocess.py
index 3908aabf5a13..551974a1806a 100644
--- a/Lib/test/test_asyncio/test_subprocess.py
+++ b/Lib/test/test_asyncio/test_subprocess.py
@@ -468,9 +468,7 @@ def test_close_dont_kill_finished(self):
 isinstance(self, SubprocessFastWatcherTests)):
 asyncio.get_child_watcher()._callbacks.clear()
 
- def test_popen_error(self):
- # Issue #24763: check that the subprocess transport is closed
- # when BaseSubprocessTransport fails
+ def _test_popen_error(self, stdin):
 if sys.platform == 'win32':
 target = 'asyncio.windows_utils.Popen'
 else:
@@ -480,12 +478,23 @@ def test_popen_error(self):
 popen.side_effect = exc
 
 create = asyncio.create_subprocess_exec(sys.executable, '-c',
- 'pass', loop=self.loop)
+ 'pass', stdin=stdin,
+ loop=self.loop)
 with warnings.catch_warnings(record=True) as warns:
 with self.assertRaises(exc):
 self.loop.run_until_complete(create)
 self.assertEqual(warns, [])
 
+ def test_popen_error(self):
+ # Issue #24763: check that the subprocess transport is closed
+ # when BaseSubprocessTransport fails
+ self._test_popen_error(stdin=None)
+
+ def test_popen_error_with_stdin_pipe(self):
+ # Issue #35721: check that newly created socket pair is closed when
+ # Popen fails
+ self._test_popen_error(stdin=subprocess.PIPE)
+
 def test_read_stdout_after_process_exit(self):
 
 async def execute():
diff --git a/Misc/ACKS b/Misc/ACKS
index 8b3232551e1b..77f19909b300 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -486,6 +486,7 @@ Florian Festi
 John Feuerstein
 Carl Feynman
 Vincent Fiack
+Niklas Fiekas
 Anastasia Filatova
 Tomer Filiba
 Segev Finer
diff --git a/Misc/NEWS.d/next/Library/2019-01-18-16-23-00.bpo-35721.d8djAJ.rst b/Misc/NEWS.d/next/Library/2019-01-18-16-23-00.bpo-35721.d8djAJ.rst
new file mode 100644
index 000000000000..5af4b1b89994
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-01-18-16-23-00.bpo-35721.d8djAJ.rst
@@ -0,0 +1,3 @@
+Fix :meth:`asyncio.SelectorEventLoop.subprocess_exec()` leaks file descriptors
+if ``Popen`` fails and called with ``stdin=subprocess.PIPE``.
+Patch by Niklas Fiekas.


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /