From 60d72c3713dd4e8df91efaa4ab8deaad02f1661f Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Fri, 23 May 2025 10:03:16 +0530 Subject: [PATCH] gh-114177: avoid calling connection lost callbacks when loop is already closed in asyncio subprocess (GH-134508) (cherry picked from commit 5804ee7b467d86131be3ff7d569443efb0d0f9fd) Co-authored-by: Kumar Aditya --- Lib/asyncio/base_subprocess.py | 7 ++++++- .../Library/2025-05-22-13-10-32.gh-issue-114177.3TYUJ3.rst | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2025-05-22-13-10-32.gh-issue-114177.3TYUJ3.rst diff --git a/Lib/asyncio/base_subprocess.py b/Lib/asyncio/base_subprocess.py index 9c2ba679ce2bf1..d40af422e614c1 100644 --- a/Lib/asyncio/base_subprocess.py +++ b/Lib/asyncio/base_subprocess.py @@ -104,7 +104,12 @@ def close(self): for proto in self._pipes.values(): if proto is None: continue - proto.pipe.close() + # See gh-114177 + # skip closing the pipe if loop is already closed + # this can happen e.g. when loop is closed immediately after + # process is killed + if self._loop and not self._loop.is_closed(): + proto.pipe.close() if (self._proc is not None and # has the child process finished? diff --git a/Misc/NEWS.d/next/Library/2025-05-22-13-10-32.gh-issue-114177.3TYUJ3.rst b/Misc/NEWS.d/next/Library/2025-05-22-13-10-32.gh-issue-114177.3TYUJ3.rst new file mode 100644 index 00000000000000..c98fde5fb04f5c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-05-22-13-10-32.gh-issue-114177.3TYUJ3.rst @@ -0,0 +1 @@ +Fix :mod:`asyncio` to not close subprocess pipes which would otherwise error out when the event loop is already closed.