Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

"startup" and "shutdown" events not triggered for mounted APIs #1480

Closed
lazka opened this issue May 26, 2020 · 7 comments
Closed

"startup" and "shutdown" events not triggered for mounted APIs #1480

lazka opened this issue May 26, 2020 · 7 comments

Comments

@lazka
Copy link

lazka commented May 26, 2020

Describe the bug

"startup" and "shutdown" events not triggered for mounted APIs

To Reproduce

from fastapi import FastAPI
from fastapi.testclient import TestClient

app = FastAPI()
subapp = FastAPI()
app.mount("/prefix", subapp)

@app.on_event("startup")
async def startup_event_1() -> None:
    print("startup for", app)

@app.on_event("shutdown")
async def shutdown_event_1() -> None:
    print("shutdown for", app)

@subapp.on_event("startup")
async def startup_event_2() -> None:
    print("startup for", subapp)

@subapp.on_event("shutdown")
async def shutdown_event_2() -> None:
    print("shutdown for", subapp)

with TestClient(app):
    pass

startup/shutdown is triggered for app but not subapp

Expected behavior

Events are emitted in both cases

@lazka lazka added the bug Something isn't working label May 26, 2020
@phy25
Copy link

phy25 commented May 26, 2020

Duplicate with #811.

@leonh
Copy link

leonh commented May 30, 2020

on_startup and on_shutdown only triggered for the the top level app object. That is the expected behaviour in FastAPI.
These hooks are implementation of the ASGI spec provided by Starlette and not strictly part of FastAPI.
https://asgi.readthedocs.io/en/latest/specs/lifespan.html

@lazka
Copy link
Author

lazka commented May 30, 2020

That makes it hard to modularize the application though. At least on_event() should raise if the event can't be subscribed to.

What would be the downside of not making these events work on mounted apis?

@sm-Fifteen
Copy link
Contributor

See encode/starlette#649, it's an upstream issue.

@lazka
Copy link
Author

lazka commented May 31, 2020

thanks. Anyway, closing in favor of #811 then

@tiangolo
Copy link
Owner

tiangolo commented Nov 5, 2022

Thanks for coming back to close this one! ☕

Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I'm checking each one in order.

@tiangolo tiangolo added question Question or problem answered reviewed and removed bug Something isn't working labels Feb 22, 2023
@tiangolo tiangolo changed the title [BUG] "startup" and "shutdown" events not triggered for mounted APIs "startup" and "shutdown" events not triggered for mounted APIs Feb 24, 2023
@tiangolo tiangolo reopened this Feb 28, 2023
@github-actions
Copy link
Contributor

Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.

@tiangolo tiangolo reopened this Feb 28, 2023
Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #7435 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

5 participants