Skip to content

Commit 7443688

Browse files
committed
skip tasks before checking limit
1 parent 914e14e commit 7443688

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

‎Lib/test/test_external_inspection.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4850,6 +4850,29 @@ def _assert_unwinder_limit_error(self, unwind, expected_substring):
48504850
f"{last_error!r}"
48514851
)
48524852

4853+
@skip_if_not_supported
4854+
def test_get_all_awaited_by_at_waiter_limit(self):
4855+
# gh-161502
4856+
async def main():
4857+
task = asyncio.current_task()
4858+
limit = self.TASK_WAITER_WALK_TASKS - 1
4859+
for _ in range(limit):
4860+
future = asyncio.Future()
4861+
asyncio.future_add_to_awaited_by(task, future)
4862+
self.addCleanup(asyncio.future_discard_from_awaited_by, task, future)
4863+
4864+
release = asyncio.Event()
4865+
child = asyncio.create_task(release.wait())
4866+
waiter = asyncio.create_task(asyncio.wait([child]), eager_start=True)
4867+
snapshot = RemoteUnwinder(os.getpid()).get_all_awaited_by()
4868+
count = sum(node.is_future for thread in snapshot
4869+
for node in thread.awaited_by)
4870+
self.assertEqual(count, limit)
4871+
release.set()
4872+
await waiter
4873+
4874+
asyncio.run(main())
4875+
48534876
@skip_if_not_supported
48544877
@unittest.skipIf(
48554878
sys.platform == "linux" and not PROCESS_VM_READV_SUPPORTED,

‎Modules/_remote_debugging/asyncio.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -687,13 +687,6 @@ process_task_waiters(
687687
if (seen < 0 || PySet_Add(visited, task_id) < 0) {
688688
goto error;
689689
}
690-
if (PyList_GET_SIZE(result) - initial_count >= MAX_TASK_WAITER_WALK_TASKS) {
691-
PyErr_SetString(PyExc_RuntimeError,
692-
"Too many task waiters (possible infinite loop)");
693-
set_exception_cause(unwinder, PyExc_RuntimeError,
694-
"Task waiter walk size limit exceeded");
695-
goto error;
696-
}
697690
void *task_ptr = PyLong_AsVoidPtr(task_id);
698691
if (task_ptr == NULL && PyErr_Occurred()) {
699692
set_exception_cause(unwinder, PyExc_RuntimeError,
@@ -711,6 +704,13 @@ process_task_waiters(
711704
continue;
712705
}
713706
}
707+
if (PyList_GET_SIZE(result) - initial_count >= MAX_TASK_WAITER_WALK_TASKS) {
708+
PyErr_SetString(PyExc_RuntimeError,
709+
"Too many task waiters (possible infinite loop)");
710+
set_exception_cause(unwinder, PyExc_RuntimeError,
711+
"Task waiter walk size limit exceeded");
712+
goto error;
713+
}
714714
if (process_single_task_node(
715715
unwinder, (uintptr_t)task_ptr, NULL, result) < 0)
716716
{

0 commit comments

Comments
 (0)