From f6a73bf6c4433e8ae72204a6500d9b23396d9587 Mon Sep 17 00:00:00 2001 From: Timofei Ivankov <128279579+deadlovelll@users.noreply.github.com> Date: Sun, 20 Sep 2026 00:03:39 +0300 Subject: [PATCH] gh-157788: Fix python -m asyncio ps failing on long task names --- Lib/test/test_external_inspection.py | 17 +++++++++++++++++ ...26-09-19-14-10-18.gh-issue-157788.v70Lxd.rst | 2 ++ Modules/_remote_debugging_module.c | 6 +++++- 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-09-19-14-10-18.gh-issue-157788.v70Lxd.rst diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index 08779bdb0081397..957c4b91acbfcd1 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -1,4 +1,5 @@ import unittest +import asyncio import os import textwrap import importlib @@ -66,6 +67,22 @@ def get_all_awaited_by(pid): class TestGetStackTrace(unittest.TestCase): maxDiff = None + @skip_if_not_supported + def test_long_task_name_is_truncated(self): + # gh-157788 + async def main(): + asyncio.create_task(asyncio.sleep(10_000), name="x" * 300) + await asyncio.sleep(0) + return [ + task.task_name + for info in RemoteUnwinder(os.getpid()).get_all_awaited_by() + for task in info.awaited_by + ] + + names = asyncio.run(main()) + self.assertIn("Task-1", names) + self.assertEqual([len(n) for n in names if n.startswith("x")], [255]) + @skip_if_not_supported @unittest.skipIf( sys.platform == "linux" and not PROCESS_VM_READV_SUPPORTED, diff --git a/Misc/NEWS.d/next/Library/2026-09-19-14-10-18.gh-issue-157788.v70Lxd.rst b/Misc/NEWS.d/next/Library/2026-09-19-14-10-18.gh-issue-157788.v70Lxd.rst new file mode 100644 index 000000000000000..641ca0a82a0915e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-09-19-14-10-18.gh-issue-157788.v70Lxd.rst @@ -0,0 +1,2 @@ +Fix ``python -m asyncio ps`` failing on a process that has a task with a +name longer than 255 characters. diff --git a/Modules/_remote_debugging_module.c b/Modules/_remote_debugging_module.c index 4fe94d439242319..b51ed4dcb7951c5 100644 --- a/Modules/_remote_debugging_module.c +++ b/Modules/_remote_debugging_module.c @@ -1109,12 +1109,16 @@ read_py_str( } Py_ssize_t len = GET_MEMBER(Py_ssize_t, unicode_obj, unwinder->debug_offsets.unicode_object.length); - if (len < 0 || len > max_len) { + if (len < 0) { PyErr_Format(PyExc_RuntimeError, "Invalid string length (%zd) at 0x%lx", len, address); set_exception_cause(unwinder, PyExc_RuntimeError, "Invalid string length in remote Unicode object"); return NULL; } + if (len > max_len) { + // gh-157788: a long name must not fail the whole read + len = max_len; + } buf = (char *)PyMem_RawMalloc(len+1); if (buf == NULL) {