|
1 | 1 | from abc import ABC, abstractmethod |
2 | 2 |
|
| 3 | +from _remote_debugging import FrameInfo |
| 4 | + |
| 5 | + |
3 | 6 | # Enums are slow |
4 | 7 | THREAD_STATE_RUNNING = 0 |
5 | 8 | THREAD_STATE_IDLE = 1 |
@@ -31,3 +34,85 @@ def _iter_all_frames(self, stack_frames, skip_idle=False): |
31 | 34 | frames = thread_info.frame_info |
32 | 35 | if frames: |
33 | 36 | yield frames, thread_info.thread_id |
| 37 | + |
| 38 | + def _iter_async_frames(self, awaited_info_list): |
| 39 | + """ |
| 40 | + Iterate over all async frame stacks from awaited info. |
| 41 | + Yields one stack per task in LEAF→ROOT order: [task body, task marker, parent body, ..., Program Root] |
| 42 | + """ |
| 43 | + # Index all tasks by ID |
| 44 | + all_tasks = {} |
| 45 | + for awaited_info in awaited_info_list: |
| 46 | + for task_info in awaited_info.awaited_by: |
| 47 | + all_tasks[task_info.task_id] = (task_info, awaited_info.thread_id) |
| 48 | + |
| 49 | + cache = {} # Memoize parent chains |
| 50 | + |
| 51 | + def build_parent_chain(task_id, parent_id): |
| 52 | + """Build ancestor chain: [await-site frames, grandparent chain..., Program Root]""" |
| 53 | + if parent_id in cache: |
| 54 | + return cache[parent_id] |
| 55 | + |
| 56 | + if parent_id not in all_tasks: |
| 57 | + return [] |
| 58 | + |
| 59 | + parent_info, _ = all_tasks[parent_id] |
| 60 | + |
| 61 | + # Find the await-site frames for this parent relationship |
| 62 | + await_frames = [] |
| 63 | + for coro_info in all_tasks[task_id][0].awaited_by: |
| 64 | + if coro_info.task_name == parent_id: |
| 65 | + await_frames = list(coro_info.call_stack or []) |
| 66 | + break |
| 67 | + |
| 68 | + # Recursively build grandparent chain, or terminate with Program Root |
| 69 | + if (parent_info.awaited_by and parent_info.awaited_by[0].task_name and |
| 70 | + parent_info.awaited_by[0].task_name in all_tasks): |
| 71 | + grandparent_id = parent_info.awaited_by[0].task_name |
| 72 | + chain = await_frames + build_parent_chain(parent_id, grandparent_id) |
| 73 | + else: |
| 74 | + # Parent is root or grandparent not tracked |
| 75 | + root_frame = FrameInfo(("<thread>", 0, "Program Root")) |
| 76 | + chain = await_frames + [root_frame] |
| 77 | + |
| 78 | + cache[parent_id] = chain |
| 79 | + return chain |
| 80 | + |
| 81 | + # Find all parent task IDs (tasks that have children) |
| 82 | + parent_task_ids = { |
| 83 | + coro.task_name |
| 84 | + for task_info, _ in all_tasks.values() |
| 85 | + for coro in (task_info.awaited_by or []) |
| 86 | + if coro.task_name |
| 87 | + } |
| 88 | + |
| 89 | + # Yield one stack per leaf task (tasks that are not parents) |
| 90 | + for task_id, (task_info, thread_id) in all_tasks.items(): |
| 91 | + # Skip parent tasks - they'll be included in their children's stacks |
| 92 | + if task_id in parent_task_ids: |
| 93 | + continue |
| 94 | + # Collect task's coroutine frames |
| 95 | + body_frames = [ |
| 96 | + frame |
| 97 | + for coro in (task_info.coroutine_stack or []) |
| 98 | + for frame in (coro.call_stack or []) |
| 99 | + ] |
| 100 | + |
| 101 | + # Add synthetic task marker |
| 102 | + task_name = task_info.task_name or f"Task-{task_id}" |
| 103 | + synthetic = FrameInfo(("<task>", 0, f"running {task_name}")) |
| 104 | + |
| 105 | + # Build complete stack with parent chain |
| 106 | + if task_info.awaited_by and task_info.awaited_by[0].task_name: |
| 107 | + parent_id = task_info.awaited_by[0].task_name |
| 108 | + if parent_id in all_tasks: |
| 109 | + parent_chain = build_parent_chain(task_id, parent_id) |
| 110 | + yield body_frames + [synthetic] + parent_chain, thread_id, 0 |
| 111 | + else: |
| 112 | + # Parent not tracked, treat as root task |
| 113 | + root = FrameInfo(("<thread>", 0, "Program Root")) |
| 114 | + yield body_frames + [synthetic, root], thread_id, 0 |
| 115 | + else: |
| 116 | + # Root task (no parents or empty awaited_by) |
| 117 | + root = FrameInfo(("<thread>", 0, "Program Root")) |
| 118 | + yield body_frames + [synthetic, root], thread_id, 0 |
0 commit comments