pythonpycharmpython-trio

Cannot debug script with trio_asyncio in PyCharm


I have this (very simplified) program running with trio as base async library and with trio_asyncio library allowing me to call asyncio methods too:

import asyncio
import trio
import trio_asyncio


async def async_main(*args):
  print('async_main start')
  async with trio_asyncio.open_loop() as loop:
    print('async_main before trio sleep')
    await trio.sleep(1)
    print('async_main before asyncio sleep')
    await trio_asyncio.aio_as_trio(asyncio.sleep)(2)
    print('async_main after sleeps')
  print('async_main stop')


if __name__ == '__main__':
  print('main start')
  trio.run(async_main)
  print('main stop')

It works well, if I run it from PyCharm:

main start
async_main start
async_main before trio sleep
async_main before asyncio sleep
async_main after sleeps
async_main stop
main stop

But if I run the same code from PyCharm in debug mode (menu Run / Debug), then it raises an exception:

Connected to pydev debugger (build 232.9559.58)
main start
async_main start
async_main before trio sleep
async_main before asyncio sleep
Traceback (most recent call last):
  File "/home/vaclav/.config/JetBrains/PyCharmCE2023.2/scratches/scratch_3.py", line 12, in async_main
    await trio_asyncio.aio_as_trio(asyncio.sleep)(2)
  File "/home/vaclav/.cache/pypoetry/virtualenvs/maybankwithoutselenium-RhkLw-zs-py3.11/lib/python3.11/site-packages/trio_asyncio/_adapter.py", line 54, in __call__
    return await self.loop.run_aio_coroutine(f)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vaclav/.cache/pypoetry/virtualenvs/maybankwithoutselenium-RhkLw-zs-py3.11/lib/python3.11/site-packages/trio_asyncio/_base.py", line 214, in run_aio_coroutine
    fut = asyncio.ensure_future(coro, loop=self)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pycharm-community/plugins/python-ce/helpers/pydev/_pydevd_asyncio_util/pydevd_nest_asyncio.py", line 156, in ensure_future
    return loop.create_task(coro_or_future)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/base_events.py", line 436, in create_task
    task = tasks.Task(coro, loop=self, name=name, context=context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pycharm-community/plugins/python-ce/helpers/pydev/_pydevd_asyncio_util/pydevd_nest_asyncio.py", line 390, in task_new_init
    self._loop.call_soon(self, context=self._context)
  File "/home/vaclav/.cache/pypoetry/virtualenvs/maybankwithoutselenium-RhkLw-zs-py3.11/lib/python3.11/site-packages/trio_asyncio/_base.py", line 312, in call_soon
    self._check_callback(callback, 'call_soon')
  File "/usr/lib/python3.11/asyncio/base_events.py", line 776, in _check_callback
    raise TypeError(
TypeError: a callable object was expected by call_soon(), got <Task pending name='Task-1' coro=<_call_defer() running at /home/vaclav/.cache/pypoetry/virtualenvs/maybankwithoutselenium-RhkLw-zs-py3.11/lib/python3.11/site-packages/trio_asyncio/_adapter.py:16>>
python-BaseException
sys:1: RuntimeWarning: coroutine '_call_defer' was never awaited
Exception in default exception handler
Traceback (most recent call last):
  File "/usr/lib/python3.11/asyncio/base_events.py", line 1797, in call_exception_handler
    self.default_exception_handler(context)
  File "/home/vaclav/.cache/pypoetry/virtualenvs/maybankwithoutselenium-RhkLw-zs-py3.11/lib/python3.11/site-packages/trio_asyncio/_async.py", line 42, in default_exception_handler
    raise RuntimeError(message)
RuntimeError: Task was destroyed but it is pending!

Process finished with exit code 1

The source code is copied from the official trio_asyncio documentation.

I have two questions:

  1. Why the code works well if it is run without debugging, and why it fails when it is run in debugger?
  2. How I should modify this code to be able still call both - trio and asyncio methods and it will be possible to use debugger with such code?

Solution

  • This helped me:

    1. open Actions search window (press shift twice and switch to Actions tab)
    2. type Registry, choose the Registry... item
    3. switch off (deselect) the python.debug.asyncio.repl property

    Thanks to Jetbrains support for help: PY-65970