Repository navigation
Open
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
For a finished anext() task, asyncio.print_call_graph() can show another task’s stack or raise AttributeError
Repro:
import asyncio
async def gen():
yield 1
await asyncio.sleep(0)
yield 2
async def main():
agen = gen()
done = asyncio.create_task(anext(agen), name="done")
await done
other = asyncio.create_task(anext(agen), name="other")
await asyncio.sleep(0)
asyncio.print_call_graph(done)
await other
await agen.aclose()
asyncio.print_call_graph(other)
asyncio.run(main())Actual output:
* Task(name='done', id=0x103c2c7d0)
+ Call stack:
| File '/Users/timofeiivankov/cpython/Lib/asyncio/tasks.py', line 718, in generator __sleep0()
| File '/Users/timofeiivankov/cpython/Lib/asyncio/tasks.py', line 724, in async sleep()
| File '/Users/timofeiivankov/cpython/repro.py', line 6, in async generator gen()
Traceback (most recent call last):
File "/Users/timofeiivankov/cpython/repro.py", line 24, in <module>
asyncio.run(main())
~~~~~~~~~~~^^^^^^^^
File "/Users/timofeiivankov/cpython/Lib/asyncio/runners.py", line 211, in run
return runner.run(main)
~~~~~~~~~~^^^^^^
File "/Users/timofeiivankov/cpython/Lib/asyncio/runners.py", line 130, in run
return self._loop.run_until_complete(task)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
File "/Users/timofeiivankov/cpython/Lib/asyncio/base_events.py", line 730, in run_until_complete
return future.result()
~~~~~~~~~~~~~^^
File "/Users/timofeiivankov/cpython/repro.py", line 21, in main
asyncio.print_call_graph(other)
~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
File "/Users/timofeiivankov/cpython/Lib/asyncio/graph.py", line 292, in print_call_graph
print(format_call_graph(future, depth=depth + 1, limit=limit), file=file)
~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/timofeiivankov/cpython/Lib/asyncio/graph.py", line 275, in format_call_graph
render_level(graph, buf, 0)
~~~~~~~~~~~~^^^^^^^^^^^^^^^
File "/Users/timofeiivankov/cpython/Lib/asyncio/graph.py", line 232, in render_level
if f.f_generator is None:
^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'f_generator'
There are two problems:
donejust yielded 1, is doesn't calledawait asyncio.sleep(0). it was done byotherlaterprint_call_graph(other)raisesAttributeErrorafter the generator is closed. it shouldn't traverse it
Expected output:
* Task(name='done', id=0x101c287d0)
* Task(name='other', id=0x101bf45c0)
Proposed fix: skip coroutine traversal for finished tasks in capture_call_graph().
I'll work on this
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 fieldsTodo
Activity