diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2019-05-20 05:35:56 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-20 05:35:56 -0700 |
commit | 3887932e1099931801876d53d05e25a43c3473b7 (patch) | |
tree | 63c561da239e65be4af8f9c9d2bd719e4548137c /Lib | |
parent | 2d94d4f1a5f54f73450d2982eab54a6301741a32 (diff) | |
download | cpython-git-3887932e1099931801876d53d05e25a43c3473b7.tar.gz |
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
(cherry picked from commit 9932fd91e878b740704ff599522e945a4bbe2ae1)
Co-authored-by: Niklas Fiekas <niklas.fiekas@backscattering.de>
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/asyncio/unix_events.py | 18 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_subprocess.py | 17 |
2 files changed, 25 insertions, 10 deletions
diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py index 639300f976..a05ebfd51b 100644 --- a/Lib/asyncio/unix_events.py +++ b/Lib/asyncio/unix_events.py @@ -758,12 +758,18 @@ class _UnixSubprocessTransport(base_subprocess.BaseSubprocessTransport): # 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 235813aa97..eeb6a73ffd 100644 --- a/Lib/test/test_asyncio/test_subprocess.py +++ b/Lib/test/test_asyncio/test_subprocess.py @@ -463,9 +463,7 @@ class SubprocessMixin: 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: @@ -475,12 +473,23 @@ class SubprocessMixin: 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(): |