Skip to content

Commit ec28f88

Browse files
Account for function doing CPU work before/after spawning workers
1 parent af65c15 commit ec28f88

1 file changed

Lines changed: 7 additions & 18 deletions

File tree

Lib/profiling/sampling/collector.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,32 +78,21 @@ def build_parent_chain(task_id, parent_id):
7878
cache[parent_id] = chain
7979
return chain
8080

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)
81+
# Yield one stack per task (including parents doing their own work)
9082
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
9483
# Collect task's coroutine frames
9584
body_frames = [
9685
frame
9786
for coro in (task_info.coroutine_stack or [])
9887
for frame in (coro.call_stack or [])
9988
]
10089

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-
10590
# Build complete stack with parent chain
10691
if task_info.awaited_by and task_info.awaited_by[0].task_name:
92+
# Child task: add synthetic marker to distinguish from parent
93+
task_name = task_info.task_name or f"Task-{task_id}"
94+
synthetic = FrameInfo(("<task>", 0, f"running {task_name}"))
95+
10796
parent_id = task_info.awaited_by[0].task_name
10897
if parent_id in all_tasks:
10998
parent_chain = build_parent_chain(task_id, parent_id)
@@ -113,6 +102,6 @@ def build_parent_chain(task_id, parent_id):
113102
root = FrameInfo(("<thread>", 0, "Program Root"))
114103
yield body_frames + [synthetic, root], thread_id, 0
115104
else:
116-
# Root task (no parents or empty awaited_by)
105+
# Root task: no synthetic marker needed, just add Program Root
117106
root = FrameInfo(("<thread>", 0, "Program Root"))
118-
yield body_frames + [synthetic, root], thread_id, 0
107+
yield body_frames + [root], thread_id, 0

0 commit comments

Comments
 (0)