Skip to content

gh-163217: Fix await cycle reporting in async-aware stack dumps - #163219

Open
deadlovelll wants to merge 3 commits into
python:mainfrom
deadlovelll:gh-163217-cycle
Open

deadlovelll wants to merge 3 commits into
python:mainfrom
deadlovelll:gh-163217-cycle

Conversation

@deadlovelll

Copy link
Copy Markdown
Contributor

Fix await cycle reporting in async-aware stack dumps

See gh-163217

Comment thread Lib/asyncio/tools.py Outdated
for awaited_info in result
for task in awaited_info.awaited_by
}
raise CycleFoundException(cycles, id2name)

@maurycy maurycy Dec 6, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An await cycle can be a valid state for the target. The example from the issue is valid (asyncio.wait() with timeout so it resolves on its own). I'm not sure if dump should refuse to print (it's similar to asyncio ps).

Could iter_async_frames() start from the leaves as it does now, then pick one task from each cycle it missed? There's already visited in _build_linear_stacks(). That'd cover dump and the collectors.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thnx! i have taken the comments into account and hope i have made the corrections correctly

@deadlovelll

Copy link
Copy Markdown
Contributor Author

found interesting edge case: if we have awaiter of the cycle profiler splits the stack into two parts:

import asyncio
import os

async def main():
    async def a():
        await task_b

    async def b():
        await asyncio.wait([task_a], timeout=60)

    asyncio.current_task().set_name("X")
    task_b = asyncio.create_task(b(), name="B")
    task_a = asyncio.create_task(a(), name="A")
    print(os.getpid(), flush=True)
    await task_a

asyncio.run(main())
➜  cpython git:(gh-163217-cycle) ✗ sudo ./python.exe -m profiling.sampling dump --async-aware --async-mode all 24905
Stack dump for PID 24905, thread 30993977 (task 4327670304; most recent call last):
  File "<task>", in X
  File "repro.py", line 15, in main
    await task_a
  File "<task>", in A (2 parents)
  File "repro.py", line 6, in main.<locals>.a
    await task_b

Stack dump, thread 30993977 (task 4327623088; most recent call last):
  File "<task>", in B
  File "repro.py", line 9, in main.<locals>.b
    await asyncio.wait([task_a], timeout=60)
  File "Lib/asyncio/tasks.py", line 446, in wait
    return await _wait(fs, timeout, return_when, loop)
  File "Lib/asyncio/tasks.py", line 533, in _wait
    await waiter

Just fixed it with last commit

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants