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] "startup" and "shutdown" events not triggered for mounted APIs

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
leonhcommented, 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

0reactions
tiangolocommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Events: startup - shutdown - FastAPI
These functions can be declared with async def or normal def . Only event handlers for the main application will be executed, not...
Read more >
Troubleshoot Start menu errors - Windows Client
In this article. Basic troubleshooting; Check Event logs that record Start Issues: Common errors and mitigation. Applies to: Windows 10.
Read more >
How to troubleshoot kernel crashes, hangs, or reboots with ...
The crash dumps are captured from the context of a freshly booted kernel; not from the context of the crashed kernel. Kdump uses...
Read more >
View the time / date ranges that your computer was turned on
If you clear your system event log, TurnedOnTimesView will not be able to detect the shutdown/startup times. On systems prior to Windows Vista,...
Read more >
Control startup and shutdown order in Compose
However, for startup Compose does not wait until a container is “ready” (whatever that means for your particular application) - only until it's...
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