Repository navigation
Closed
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-asynciotype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
python -m asyncio pstree crashes with RecursionError on a deep chain of tasks awaiting each other
Repro:
import asyncio, os
async def chain(n):
if n:
await asyncio.create_task(chain(n - 1))
else:
print(os.getpid(), flush=True)
await asyncio.sleep(3600)
asyncio.run(chain(1000))Fails with:
Traceback (most recent call last):
File "<frozen runpy>", line 201, in _run_module_as_main
File "<frozen runpy>", line 87, in _run_code
File "/Users/timofeiivankov/cpython/Lib/asyncio/__main__.py", line 190, in <module>
asyncio.tools.display_awaited_by_tasks_tree(args.pid, retries=args.retries)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/timofeiivankov/cpython/Lib/asyncio/tools.py", line 283, in display_awaited_by_tasks_tree
result = build_async_tree(tasks)
File "/Users/timofeiivankov/cpython/Lib/asyncio/tools.py", line 161, in build_async_tree
cycles = _find_cycles(g)
File "/Users/timofeiivankov/cpython/Lib/asyncio/tools.py", line 142, in _find_cycles
dfs(v)
~~~^^^
File "/Users/timofeiivankov/cpython/Lib/asyncio/tools.py", line 133, in dfs
dfs(w)
~~~^^^
File "/Users/timofeiivankov/cpython/Lib/asyncio/tools.py", line 133, in dfs
dfs(w)
~~~^^^
File "/Users/timofeiivankov/cpython/Lib/asyncio/tools.py", line 133, in dfs
dfs(w)
~~~^^^
[Previous line repeated 990 more times]
File "/Users/timofeiivankov/cpython/Lib/asyncio/tools.py", line 132, in dfs
if color[w] == WHITE:
~~~~~^^^
RecursionError: maximum recursion depth exceeded
➜ cpython git:(main) ✗
But the expected behaviour, it should display the tree correctly
Proposed fix: rewrite the recursive dfs() and render() in Lib/asyncio/tools.py as loops
I have a fix ready
CPython versions tested on:
CPython main branch
Operating systems tested on:
macOS
Linked PRs
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-asynciotype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Projects
- StatusShow more project fieldsDone
Activity
gh-159168: Fix RecursionError in asyncio pstree on a deep chain of ta…
[3.14] gh-159168: Fix RecursionError in asyncio pstree on a deep chai…
[3.15] gh-159168: Fix RecursionError in asyncio pstree on a deep chai…
asvetlov commentedon Nov 2, 2026
Fixed