Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues using interactive mode with uvicorn #269

Open
Jackevansevo opened this issue Apr 22, 2023 · 2 comments
Open

Issues using interactive mode with uvicorn #269

Jackevansevo opened this issue Apr 22, 2023 · 2 comments

Comments

@Jackevansevo
Copy link

Jackevansevo commented Apr 22, 2023

I'm not familiar enough with the stack to determine whether this is an issue with ipdb, or with the specific frameworks I'm using.

But I'm able to reproduce this using flask and uvicorn (and starlette/fastapi by association).

Taking the following flask code

from flask import Flask

app = Flask(__name__)


@app.route("/")
async def hello_world():
    breakpoint()
    return "<p>Hello, World!</p>"

Or the following uvicorn app

async def app(scope, receive, send):
    assert scope["type"] == "http"

    breakpoint()

    await send(
        {
            "type": "http.response.start",
            "status": 200,
            "headers": [
                [b"content-type", b"text/plain"],
            ],
        }
    )
    await send(
        {
            "type": "http.response.body",
            "body": b"Hello, world!",
        }
    )

Setting a breakpoint and attempting to drop into interactive mode whilst in an async view causes the server to crash, with the error:

RuntimeError: This event loop is already running

The screencast below demonstrates the issue described

2023-04-22.17-57-51.mp4
Traceback (most recent call last):
  File "/home/jack/code/uvicorn-example/.direnv/python-3.11.2/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 436, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jack/code/uvicorn-example/.direnv/python-3.11.2/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jack/code/uvicorn-example/example.py", line 6, in app
    await send(
          ^^^^
  File "/home/jack/code/uvicorn-example/example.py", line 6, in app
    await send(
          ^^^^
  File "/home/jack/.pyenv/versions/3.11.2/lib/python3.11/bdb.py", line 90, in trace_dispatch
    return self.dispatch_line(frame)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jack/.pyenv/versions/3.11.2/lib/python3.11/bdb.py", line 114, in dispatch_line
    self.user_line(frame)
  File "/home/jack/.pyenv/versions/3.11.2/lib/python3.11/pdb.py", line 340, in user_line
    self.interaction(frame, None)
  File "/home/jack/code/uvicorn-example/.direnv/python-3.11.2/lib/python3.11/site-packages/IPython/core/debugger.py", line 335, in interaction
    OldPdb.interaction(self, frame, traceback)
  File "/home/jack/.pyenv/versions/3.11.2/lib/python3.11/pdb.py", line 435, in interaction
    self._cmdloop()
  File "/home/jack/.pyenv/versions/3.11.2/lib/python3.11/pdb.py", line 400, in _cmdloop
    self.cmdloop()
  File "/home/jack/code/uvicorn-example/.direnv/python-3.11.2/lib/python3.11/site-packages/IPython/terminal/debugger.py", line 140, in cmdloop
    stop = self.onecmd(line)
           ^^^^^^^^^^^^^^^^^
  File "/home/jack/.pyenv/versions/3.11.2/lib/python3.11/pdb.py", line 500, in onecmd
    return cmd.Cmd.onecmd(self, line)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jack/.pyenv/versions/3.11.2/lib/python3.11/cmd.py", line 217, in onecmd
    return func(arg)
           ^^^^^^^^^
  File "/home/jack/code/uvicorn-example/.direnv/python-3.11.2/lib/python3.11/site-packages/IPython/terminal/debugger.py", line 153, in do_interact
    ipshell(
  File "/home/jack/code/uvicorn-example/.direnv/python-3.11.2/lib/python3.11/site-packages/IPython/terminal/embed.py", line 245, in __call__
    self.mainloop(
  File "/home/jack/code/uvicorn-example/.direnv/python-3.11.2/lib/python3.11/site-packages/IPython/terminal/embed.py", line 337, in mainloop
    self.interact()
  File "/home/jack/code/uvicorn-example/.direnv/python-3.11.2/lib/python3.11/site-packages/IPython/terminal/interactiveshell.py", line 873, in interact
    code = self.prompt_for_code()
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jack/code/uvicorn-example/.direnv/python-3.11.2/lib/python3.11/site-packages/IPython/terminal/interactiveshell.py", line 812, in prompt_for_code
    text = self.pt_app.prompt(
           ^^^^^^^^^^^^^^^^^^^
  File "/home/jack/code/uvicorn-example/.direnv/python-3.11.2/lib/python3.11/site-packages/prompt_toolkit/shortcuts/prompt.py", line 1035, in prompt
    return self.app.run(
           ^^^^^^^^^^^^^
  File "/home/jack/code/uvicorn-example/.direnv/python-3.11.2/lib/python3.11/site-packages/prompt_toolkit/application/application.py", line 961, in run
    return loop.run_until_complete(coro)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "uvloop/loop.pyx", line 1511, in uvloop.loop.Loop.run_until_complete
  File "uvloop/loop.pyx", line 1504, in uvloop.loop.Loop.run_until_complete
  File "uvloop/loop.pyx", line 1377, in uvloop.loop.Loop.run_forever
  File "uvloop/loop.pyx", line 518, in uvloop.loop.Loop._run
RuntimeError: this event loop is already running.

in flask the traceback is similar

Traceback (most recent call last):
  File "/home/jack/code/flask-async/.direnv/python-3.11.2/lib/python3.11/site-packages/flask/app.py", line 2551, in __call__
    return self.wsgi_app(environ, start_response)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jack/code/flask-async/.direnv/python-3.11.2/lib/python3.11/site-packages/flask/app.py", line 2531, in wsgi_app
    response = self.handle_exception(e)
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jack/code/flask-async/.direnv/python-3.11.2/lib/python3.11/site-packages/flask/app.py", line 2528, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jack/code/flask-async/.direnv/python-3.11.2/lib/python3.11/site-packages/flask/app.py", line 1825, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jack/code/flask-async/.direnv/python-3.11.2/lib/python3.11/site-packages/flask/app.py", line 1823, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jack/code/flask-async/.direnv/python-3.11.2/lib/python3.11/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jack/code/flask-async/.direnv/python-3.11.2/lib/python3.11/site-packages/asgiref/sync.py", line 240, in __call__
    return call_result.result()
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/jack/.pyenv/versions/3.11.2/lib/python3.11/concurrent/futures/_base.py", line 449, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/home/jack/.pyenv/versions/3.11.2/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/home/jack/code/flask-async/.direnv/python-3.11.2/lib/python3.11/site-packages/asgiref/sync.py", line 306, in main_wrap
    result = await self.awaitable(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jack/code/flask-async/app.py", line 9, in hello_world
    return "<p>Hello, World!</p>"
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jack/code/flask-async/app.py", line 9, in hello_world
    return "<p>Hello, World!</p>"
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jack/.pyenv/versions/3.11.2/lib/python3.11/bdb.py", line 90, in trace_dispatch
    return self.dispatch_line(frame)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jack/.pyenv/versions/3.11.2/lib/python3.11/bdb.py", line 114, in dispatch_line
    self.user_line(frame)
  File "/home/jack/.pyenv/versions/3.11.2/lib/python3.11/pdb.py", line 340, in user_line
    self.interaction(frame, None)
  File "/home/jack/code/flask-async/.direnv/python-3.11.2/lib/python3.11/site-packages/IPython/core/debugger.py", line 335, in interaction
    OldPdb.interaction(self, frame, traceback)
  File "/home/jack/.pyenv/versions/3.11.2/lib/python3.11/pdb.py", line 435, in interaction
    self._cmdloop()
  File "/home/jack/.pyenv/versions/3.11.2/lib/python3.11/pdb.py", line 400, in _cmdloop
    self.cmdloop()
  File "/home/jack/code/flask-async/.direnv/python-3.11.2/lib/python3.11/site-packages/IPython/terminal/debugger.py", line 140, in cmdloop
    stop = self.onecmd(line)
           ^^^^^^^^^^^^^^^^^
  File "/home/jack/.pyenv/versions/3.11.2/lib/python3.11/pdb.py", line 500, in onecmd
    return cmd.Cmd.onecmd(self, line)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jack/.pyenv/versions/3.11.2/lib/python3.11/cmd.py", line 217, in onecmd
    return func(arg)
           ^^^^^^^^^
  File "/home/jack/code/flask-async/.direnv/python-3.11.2/lib/python3.11/site-packages/IPython/terminal/debugger.py", line 153, in do_interact
    ipshell(
  File "/home/jack/code/flask-async/.direnv/python-3.11.2/lib/python3.11/site-packages/IPython/terminal/embed.py", line 245, in __call__
    self.mainloop(
  File "/home/jack/code/flask-async/.direnv/python-3.11.2/lib/python3.11/site-packages/IPython/terminal/embed.py", line 337, in mainloop
    self.interact()
  File "/home/jack/code/flask-async/.direnv/python-3.11.2/lib/python3.11/site-packages/IPython/terminal/interactiveshell.py", line 873, in interact
    code = self.prompt_for_code()
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jack/code/flask-async/.direnv/python-3.11.2/lib/python3.11/site-packages/IPython/terminal/interactiveshell.py", line 812, in prompt_for_code
    text = self.pt_app.prompt(
           ^^^^^^^^^^^^^^^^^^^
  File "/home/jack/code/flask-async/.direnv/python-3.11.2/lib/python3.11/site-packages/prompt_toolkit/shortcuts/prompt.py", line 1035, in prompt
    return self.app.run(
           ^^^^^^^^^^^^^
  File "/home/jack/code/flask-async/.direnv/python-3.11.2/lib/python3.11/site-packages/prompt_toolkit/application/application.py", line 961, in run
    return loop.run_until_complete(coro)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jack/.pyenv/versions/3.11.2/lib/python3.11/asyncio/base_events.py", line 629, in run_until_complete
    self._check_running()
  File "/home/jack/.pyenv/versions/3.11.2/lib/python3.11/asyncio/base_events.py", line 588, in _check_running
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running
@gotcha
Copy link
Owner

gotcha commented Apr 24, 2023

Which versions of ipdb and IPython are you using ?

@Jackevansevo
Copy link
Author

Which versions of ipdb and IPython are you using ?

Currently I'm using

ipdb==0.13.13
    # via -r requirements.in
ipython==8.12.0
    # via ipdb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants