From dd495b0b4b7953462f616f36ac4b6dcdbbe66b06 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Fri, 20 Nov 2026 18:04:49 +0530 Subject: [PATCH] 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 --- Lib/asyncio/subprocess.py | 15 +++++------ Lib/test/test_asyncio/test_subprocess.py | 25 +++++++++++++++++++ ...-11-20-09-30-00.gh-issue-162217.wC7sHd.rst | 4 +++ 3 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-20-09-30-00.gh-issue-162217.wC7sHd.rst diff --git a/Lib/asyncio/subprocess.py b/Lib/asyncio/subprocess.py index 043359bbd03f8aa..b89db539414ab06 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 041748734573b68..9fae38bba7d4510 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 000000000000000..be12568cb8f7fec --- /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() ` was +cancelled.