question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG][DOCUMENTATION] Sub application startup event never triggered

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
tiangolocommented, Jun 14, 2020

@ClericPy it would have to be implemented in Starlette: https://github.com/encode/starlette/issues/649

1reaction
jonathanunderwoodcommented, Dec 23, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found