[BUG][DOCUMENTATION] Sub application startup event never triggered
See original GitHub issueDescribe the bug
Startup event handlers for sub-applications never trigger.
To Reproduce
from fastapi import FastAPI
sub_app = FastAPI(openapi_prefix="/subapp")
@sub_app.on_event("startup")
async def sub_app_startup():
print("++++ Sub-app startup event") # Never fires
app = FastAPI()
@app.on_event("startup")
async def app_startup():
print("++++ App startup event")
app.mount("/subapp", sub_app)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, port=8000, host="0.0.0.0") # nosec
Running the above shows:
$ python subapp_events.py
INFO: Started server process [38462]
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO: Waiting for application startup.
++++ App startup event
INFO: Application startup complete.
Notice that sub_app_startup()
is never run.
Expected behavior
I would expect the sub-application startup event handler to also run (although I am not sure what ordering I would expect).
Environment
- OS: macOS
- FastAPI Version: 0.45.0
- Python version: 3.7.5
Additional context
N/A
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:7 (6 by maintainers)
Top Results From Across the Web
Application.Startup event limitations (bug?) - Stack Overflow
By doing this, the window you just created has become the main window: the one specified in StartupUri will only be created right...
Read more >JavaScript tutorial - How To Create
So, we tell the form to detect the submit event. When the event is triggered, we tell it to run the function that...
Read more >Timers: possible bug/documentation error - MATLAB Answers ...
The timer appears to fire immediately after starting (as you would expect), and twice more during the pause before the loop starts (again...
Read more >Events: startup - shutdown - FastAPI
You can define event handlers (functions) that need to be executed before the application starts up, or when the application is shutting down....
Read more >I2C Repeated Start Behavior / RM bit / Accurate RX Count ...
Have you ever been able to send a repeated start when RM isn't set? ... This program requires an external I2C EEPROM connected...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@ClericPy it would have to be implemented in Starlette: https://github.com/encode/starlette/issues/649
Digging in to this a bit more, I can see that FastAPI merely exposes the Starlette lifecycle events and doesn’t do anything special about them.
In addition, this issue has been raised previously against Starlette.
Since it’s deemed out-of-scope by Starlette developers, I think the only action here would be to document this in the FastAPI docs i.e. call out explicitly that sub-application event handlers are ignored.