Skip to content

profiling.sampling dump --async-aware crashes on a deep chain of awaiting tasks #158688

Description

@deadlovelll

Bug report

Dumping a program with ~5000 or more nested awaiting tasks kills the profiler with segfault

Repro:

import asyncio, os

async def spin():
    await asyncio.sleep(1.0)
    while True:
        pass

async def link(nxt):
    await nxt

async def main():
    prev = asyncio.create_task(spin(), name="inner")
    for i in range(5000):
        prev = asyncio.create_task(link(prev), name=f"L{i}")
    print(os.getpid(), flush=True)
    await asyncio.sleep(1000)

asyncio.run(main())

Run:

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

Output:

Fatal Python error: Segmentation fault

Current thread 0x00000001f3337840 (most recent call first):
  File "/Users/timofeiivankov/cpython/Lib/profiling/sampling/sample.py", line 119 in _get_stack_trace
  File "/Users/timofeiivankov/cpython/Lib/profiling/sampling/sample.py", line 124 in dump_stack
  File "/Users/timofeiivankov/cpython/Lib/profiling/sampling/sample.py", line 564 in dump_stack
  File "/Users/timofeiivankov/cpython/Lib/profiling/sampling/cli.py", line 1232 in _handle_dump
  File "/Users/timofeiivankov/cpython/Lib/profiling/sampling/cli.py", line 1167 in _main
  File "/Users/timofeiivankov/cpython/Lib/profiling/sampling/cli.py", line 1011 in main
  File "/Users/timofeiivankov/cpython/Lib/profiling/sampling/__main__.py", line 65 in <module>
  File "<frozen runpy>", line 87 in _run_code
  File "<frozen runpy>", line 201 in _run_module_as_main

Current thread's C stack trace (most recent call first):
  <system returned no stack trace>
[2]    29797 segmentation fault  sudo ./python.exe -X faulthandler -m profiling.sampling dump --async-aware

Proposed fix: walk the await graph iteratively instead of recursively, visiting each task once.

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