Skip to content

python -m profiling.sampling --async-aware shows one frame for a task whose coroutine recurses #158522

Description

@deadlovelll

Bug report

A task whose coroutine recurses is shown with only one frame in the profiler dump

Repro:

import asyncio, os, time

async def rec(n):
    if n:
        await rec(n - 1)
    else:
        print(os.getpid(), flush=True)
        t0 = time.monotonic()
        while time.monotonic() - t0 < 30:
            pass

async def main():
    await asyncio.create_task(rec(4))

asyncio.run(main())

Run:

./python.exe repro.py
sudo ./python.exe -m profiling.sampling dump --async-aware --async-mode running <pid>

Actual:

Stack dump for PID 68355, thread 4566646 (task 4392209776; most recent call last):
  File "<task>", in Task-1
  File "repro.py", line 13, in main
    await asyncio.create_task(rec(4))
  File "<task>", in Task-2
  File "repro.py", line 9, in rec
    while time.monotonic() - t0 < 30:

Expected:

Stack dump for PID 67670, thread 4562857 (task 4376759664; most recent call last):
  File "<task>", in Task-1
  File "repro.py", line 13, in main
    await asyncio.create_task(rec(4))
  File "<task>", in Task-2
  File "repro.py", line 5, in rec
    await rec(n - 1)
  File "repro.py", line 5, in rec
    await rec(n - 1)
  File "repro.py", line 5, in rec
    await rec(n - 1)
  File "repro.py", line 5, in rec
    await rec(n - 1)
  File "repro.py", line 9, in rec
    while time.monotonic() - t0 < 30:

Proposed fix: Stop the frame walk at the task's own frame instead of its code object

I have a fix ready

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions