https://github.com/python/cpython/commit/0ff53c82c7e89bd37d9b8737c970d310eb44eea1 commit: 0ff53c82c7e89bd37d9b8737c970d310eb44eea1 branch: 3.15 author: Miss Islington (bot) <[email protected]> committer: kumaraditya303 <[email protected]> date: 2026-11-20T18:59:25+05:30 summary:
[3.15] gh-162217: Fix InvalidStateError when subprocess stdin is closed after a cancelled wait_closed() (GH-162317) (#162320) gh-162217: Fix InvalidStateError when subprocess stdin is closed after a cancelled wait_closed() (GH-162317) (cherry picked from commit a3ea5d29f5b0285f7ac1b0053f15b30f2c8daec9) Co-authored-by: Kumar Aditya <[email protected]> files: A Misc/NEWS.d/next/Library/2026-11-20-09-30-00.gh-issue-162217.wC7sHd.rst M Lib/asyncio/subprocess.py M Lib/test/test_asyncio/test_subprocess.py diff --git a/Lib/asyncio/subprocess.py b/Lib/asyncio/subprocess.py index 043359bbd03f8a..b89db539414ab0 100644 --- a/Lib/asyncio/subprocess.py +++ b/Lib/asyncio/subprocess.py @@ -77,13 +77,14 @@ def pipe_connection_lost(self, fd, exc): if pipe is not None: pipe.close() self.connection_lost(exc) - if exc is None: - self._stdin_closed.set_result(None) - else: - self._stdin_closed.set_exception(exc) - # Since calling `wait_closed()` is not mandatory, - # we shouldn't log the traceback if this is not awaited. - self._stdin_closed._log_traceback = False + if not self._stdin_closed.done(): + if exc is None: + self._stdin_closed.set_result(None) + else: + self._stdin_closed.set_exception(exc) + # Since calling `wait_closed()` is not mandatory, + # we shouldn't log the traceback if this is not awaited. + self._stdin_closed._log_traceback = False return if fd == 1: reader = self.stdout diff --git a/Lib/test/test_asyncio/test_subprocess.py b/Lib/test/test_asyncio/test_subprocess.py index 041748734573b6..9fae38bba7d451 100644 --- a/Lib/test/test_asyncio/test_subprocess.py +++ b/Lib/test/test_asyncio/test_subprocess.py @@ -324,6 +324,31 @@ async def write_stdin(proc, data): self.loop.run_until_complete, coro) self.loop.run_until_complete(proc.wait()) + def test_stdin_wait_closed_cancelled(self): + # gh-162217: cancelling a task waiting in stdin.wait_closed() must + # not make pipe_connection_lost() fail with InvalidStateError. + async def main(): + proc = await asyncio.create_subprocess_exec( + *PROGRAM_CAT, + stdin=subprocess.PIPE, + stdout=subprocess.DEVNULL, + ) + try: + task = asyncio.create_task(proc.stdin.wait_closed()) + await asyncio.sleep(0) + task.cancel() + with self.assertRaises(asyncio.CancelledError): + await task + finally: + proc.stdin.close() + await proc.wait() + + messages = [] + self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx)) + + self.loop.run_until_complete(main()) + self.assertEqual(messages, []) + def test_communicate_ignore_broken_pipe(self): # buffer large enough to feed the whole pipe buffer large_data = b'x' * support.PIPE_MAX_SIZE diff --git a/Misc/NEWS.d/next/Library/2026-11-20-09-30-00.gh-issue-162217.wC7sHd.rst b/Misc/NEWS.d/next/Library/2026-11-20-09-30-00.gh-issue-162217.wC7sHd.rst new file mode 100644 index 00000000000000..be12568cb8f7fe --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-20-09-30-00.gh-issue-162217.wC7sHd.rst @@ -0,0 +1,4 @@ +Fix :exc:`asyncio.InvalidStateError` being logged when the standard input of +an :mod:`asyncio` subprocess is closed after a task waiting in +:meth:`proc.stdin.wait_closed() <asyncio.StreamWriter.wait_closed>` was +cancelled. _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
