You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am developing a FastAPI application with uvicorn, and I have run into an issue where I cannot seem to catch an exception thrown during startup.
The code is below. Yes, I am using FastAPI, but I am reasonably convinced that the issue with with uvicorn. When I run the below, I get this output:
INFO: Started server process [3478436]
INFO: Waiting for application startup.
ERROR: Traceback (most recent call last):
File "/home/madduck/.tmp/2025-07-09-172139-fastapi-lifespan/.direnv/python-3.13.3/lib/python3.13/site-packages/starlette/routing.py", line 692, in lifespan
async with self.lifespan_context(app) as maybe_state:
~~~~~~~~~~~~~~~~~~~~~^^^^^
File "/usr/lib/python3.13/contextlib.py", line 214, in __aenter__
return await anext(self.gen)
^^^^^^^^^^^^^^^^^^^^^
File "/home/madduck/.tmp/2025-07-09-172139-fastapi-lifespan/app.py", line 9, in lifespan
raise RuntimeError("BOOOOOM")
RuntimeError: BOOOOOM
ERROR: Application startup failed. Exiting.
Those error messages are coming from uvicorn.
I've raised this issue over at FastAPI, but I was told that FastAPI cannot do anything.
But can I do anything with Uvicorn? The following does not work. Where do I need to catch the exception?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am developing a FastAPI application with uvicorn, and I have run into an issue where I cannot seem to catch an exception thrown during startup.
The code is below. Yes, I am using FastAPI, but I am reasonably convinced that the issue with with uvicorn. When I run the below, I get this output:
Those error messages are coming from uvicorn.
I've raised this issue over at FastAPI, but I was told that FastAPI cannot do anything.
But can I do anything with Uvicorn? The following does not work. Where do I need to catch the exception?
Update FYI, the idea of handling lifespan outside of FastAPI provides a viable approach: fastapi/fastapi#13878 (reply in thread) . Better than nothing…
Beta Was this translation helpful? Give feedback.
All reactions