Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a stuck asyncio coroutine in Python?

There are lots of coroutines in my production code, which are stuck at unknown position while processing request. I attached gdb with Python support extension to the process, but it doesn't show the exact line in the coroutine where the process is stuck, only primary stack trace. Here is a minimal example:

import asyncio

async def hello():
    await asyncio.sleep(30)
    print('hello world')

asyncio.run(hello())
(gdb) py-bt
Traceback (most recent call first):
  File "/usr/lib/python3.8/selectors.py", line 468, in select
    fd_event_list = self._selector.poll(timeout, max_ev)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 2335, in _run_once
  File "/usr/lib/python3.8/asyncio/base_events.py", line 826, in run_forever
    None, getaddr_func, host, port, family, type, proto, flags)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 603, in run_until_complete
    self.run_forever()
  File "/usr/lib/python3.8/asyncio/runners.py", line 299, in run
  File "main.py", line 7, in <module>

GDB shows a trace that ends on line 7, but the code is obviously stuck on line 4. How to make it show a more complete trace with nested coroutines?

like image 693
shoraii Avatar asked Jan 13 '21 15:01

shoraii


1 Answers

You can use the aiodebug.log_slow_callbacks.enable(0.05)

Follow for more : https://pypi.org/project/aiodebug/

like image 97
Praveen mundur Avatar answered Nov 19 '22 06:11

Praveen mundur