Skip to content

Commit 04b7fff

Browse files
authored
[3.14] gh-157788: Fix python -m asyncio ps failing on long task names (#157843)
gh-157788: Fix python -m asyncio ps failing on long task names
1 parent 484e025 commit 04b7fff

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎Lib/test/test_external_inspection.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
import asyncio
23
import os
34
import textwrap
45
import importlib
@@ -66,6 +67,22 @@ def get_all_awaited_by(pid):
6667
class TestGetStackTrace(unittest.TestCase):
6768
maxDiff = None
6869

70+
@skip_if_not_supported
71+
def test_long_task_name_is_truncated(self):
72+
# gh-157788
73+
async def main():
74+
asyncio.create_task(asyncio.sleep(10_000), name="x" * 300)
75+
await asyncio.sleep(0)
76+
return [
77+
task.task_name
78+
for info in RemoteUnwinder(os.getpid()).get_all_awaited_by()
79+
for task in info.awaited_by
80+
]
81+
82+
names = asyncio.run(main())
83+
self.assertIn("Task-1", names)
84+
self.assertEqual([len(n) for n in names if n.startswith("x")], [255])
85+
6986
@skip_if_not_supported
7087
@unittest.skipIf(
7188
sys.platform == "linux" and not PROCESS_VM_READV_SUPPORTED,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix ``python -m asyncio ps`` failing on a process that has a task with a
2+
name longer than 255 characters.

‎Modules/_remote_debugging_module.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,12 +1109,16 @@ read_py_str(
11091109
}
11101110

11111111
Py_ssize_t len = GET_MEMBER(Py_ssize_t, unicode_obj, unwinder->debug_offsets.unicode_object.length);
1112-
if (len < 0 || len > max_len) {
1112+
if (len < 0) {
11131113
PyErr_Format(PyExc_RuntimeError,
11141114
"Invalid string length (%zd) at 0x%lx", len, address);
11151115
set_exception_cause(unwinder, PyExc_RuntimeError, "Invalid string length in remote Unicode object");
11161116
return NULL;
11171117
}
1118+
if (len > max_len) {
1119+
// gh-157788: a long name must not fail the whole read
1120+
len = max_len;
1121+
}
11181122

11191123
buf = (char *)PyMem_RawMalloc(len+1);
11201124
if (buf == NULL) {

0 commit comments

Comments
 (0)