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

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

Open
jonathanunderwood opened this issue Dec 22, 2019 · 4 comments
Open

Comments

@jonathanunderwood
Copy link
Contributor

@jonathanunderwood jonathanunderwood commented Dec 22, 2019

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

@jonathanunderwood
Copy link
Contributor Author

@jonathanunderwood jonathanunderwood commented 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.

@jonathanunderwood jonathanunderwood changed the title [BUG] Sub application startup event never triggered [BUG][DOCUMENTATION] Sub application startup event never triggered Dec 23, 2019
@dmontagu
Copy link
Collaborator

@dmontagu dmontagu commented Dec 31, 2019

I also think it would be good to document this.

PR welcome!

@tiangolo
Copy link
Owner

@tiangolo tiangolo commented Feb 12, 2020

Thanks for the investigation and for reporting back @jonathanunderwood , yep, it makes sense to have it documented.

@ClericPy
Copy link

@ClericPy ClericPy commented Mar 20, 2020

I found this issue this days, so what should I do to trigger subapp's start_up callbacks?
It seems that fastapi.APIRouter's on_startup param has a similar feature.

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

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.