Starting an IPython interactive shell from pudb inside a co-routine fails

383 views Asked by At

I really like pudb and in combination with IPython I've been successfully debugging python code for years now.

Most of the time this combination just works, for example

# test1.py
import time

def do_something():
    import pudb
    pudb.set_trace()

    sleep(1)
    return 1

do_something()

If you execute python test1.py, pudb starts and when you press ! the IPython interactive shell starts without a problem.

However if you set a trace inside a co-routine, pudb starts without a problem, you can even step inside a await some_async_call() call without a problem. But if you want to start the interactive shell and press !, then the whole session breaks with an error. Fox example:

# test2.py
import asyncio

async def do_something():
    import pudb
    pudb.set_trace()

    await asyncio.sleep(1)

    return 1

asyncio.run(do_something())
$ python test2.py

pudb starts and halts before await asyncio.sleep(1), now press !

Hit Ctrl-D to return to PuDB.

In [1]:
Traceback (most recent call last):
  File "test2.py", line 11, in <module>
    asyncio.run(do_something())
  File "/home/shaoran/anaconda/py3/envs/ivct/lib/python3.8/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/home/shaoran/anaconda/py3/envs/ivct/lib/python3.8/asyncio/base_events.py", line 599, in run_until_complete
    self.run_forever()
  File "test2.py", line 7, in do_something
    await asyncio.sleep(1)
  File "test2.py", line 7, in do_something
    await asyncio.sleep(1)
...
  File "/home/shaoran/anaconda/py3/envs/ivct/lib/python3.8/asyncio/base_events.py", line 554, in run_forever
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running
Task was destroyed but it is pending!
task: <Task pending name='Task-5' coro=<Renderer.wait_for_cpr_responses.<locals>.wait_for_timeout() running at /home/shaoran/anaconda/py3/envs/ivct/lib/python3.8/site-packages/prompt_toolkit/renderer.py:505> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7fe05cd42fd0>()]>>

I suspect that this is an IPython issue, because if I change the shell from IPython to bpython and press !, then the bpython interactive shell starts without a problem. I've been searching for a solution to this but it seems that I'm unable to find anything that would help, I managed to find these issues/pull request on github #12028, #12140 and #12141, which seems to be related to it, but I don't know if that's the case and or whether they have been released yet.

So my question is: can anybody reproduce this error? Is this a pudb or a ipython error?

I use python 3.8.1 installed from anaconda 4.8.3 (linux 64bit) and IPython 7.13.0 (installed with conda install ipython -c conda-forge, but the same applies when installed via pip).

0

There are 0 answers