Skip to content

unexpected keyword argument 'loop_factory' on uvicorn.run(), only when debugging #2737

@Kludex

Description

@Kludex

Discussed in #2736

Originally posted by sm-Fifteen October 21, 2025
I was about to file this as an issue, but it's not 100% clear if this is specifically Uvicorn's fault, here.

One way by which I commonly launch my apps is with a launch script that merely calls uvicorn.run(app, host="0.0.0.0", port=cfg.api_port, root_path=cfg.path_prefix), as per documentation documentation. I've recently been unable to use that path for debugging, as launching the Python debugger on this application causes an exception to be raised:

.venv\Scripts\python.exe PyCharm/plugins/python-ce/helpers/pydev/pydevd.py --multiprocess --qt-support=auto --client 127.0.0.1 --port 54848 --file launch.py 
Connected to: <socket.socket fd=1160, family=2, type=1, proto=0, laddr=('127.0.0.1', 54849), raddr=('127.0.0.1', 54848)>.
Connected to pydev debugger (build 252.26830.99)

Traceback (most recent call last):
  File "PyCharm\plugins\python-ce\helpers\pydev\pydevd.py", line 1648, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "PyCharm\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "launch.py", line 17, in <module>
    uvicorn.run(app, host="0.0.0.0", port=cfg.api_port, root_path=cfg.path_prefix, log_config="./logconfig.json")
  File ".venv\Lib\site-packages\uvicorn\main.py", line 593, in run
    server.run()
  File ".venv\Lib\site-packages\uvicorn\server.py", line 67, in run
    return asyncio_run(self.serve(sockets=sockets), loop_factory=self.config.get_loop_factory())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: _patch_asyncio.<locals>.run() got an unexpected keyword argument 'loop_factory'

The error is coming out of here:

def run(self, sockets: list[socket.socket] | None = None) -> None:
return asyncio_run(self.serve(sockets=sockets), loop_factory=self.config.get_loop_factory())

...and I'm using Python 3.12.11, so asyncio_run is supposed to be the regular one:

if sys.version_info >= (3, 12):
asyncio_run = asyncio.run

It's not 100% clear to me right now whether this is an issue with pydevd (or tweaks made to it by PyCharm) or something that needs to be fixed on Uvicorn's end, but I'm apparently not the first person to encounter this. This has also been raised recently as awslabs/amazon-bedrock-agentcore-samples#416.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions