resolved RESOLVED UNSUBMITTED PASS · None tool calls · 0 s · fastapi/fastapi
🐛 Fix `on_startup` and `on_shutdown` parameters of `APIRouter`
This fixes the issue reported in this comment: https://github.com/fastapi/fastapi/pull/14851#issuecomment-3863896385
```py
from fastapi import FastAPI
def s_start():
print("SYNC START", flush=True)
app = FastAPI(on_startup=[s_start])
@app.get("/health")
async def health():
return {"ok": True}
```
Startup event is not called because `APIRouter.on_startup` is set in `APIRouter.__init__` before calling `super().__init__` and is overridden by `router.__init__` (https://github.com/Kludex/starlette/blob/e5b8a5d200504a2608bed11fe6f66b1d20b9ae2c/starlette/routing.py#L597-L598)| # | Tool | Arguments | Result |
|---|---|---|---|
| No trace captured. | |||
--- a/fastapi/routing.py
+++ b/fastapi/routing.py
@@ -952,16 +952,6 @@ def __init__(
),
] = Default(generate_unique_id),
) -> None:
- # Handle on_startup/on_shutdown locally since Starlette removed support
- # Ref: https://github.com/Kludex/starlette/pull/3117
- # TODO: deprecate this once the lifespan (or alternative) interface is improved
- self.on_startup: list[Callable[[], Any]] = (
- [] if on_startup is None else list(on_startup)
- )
- self.on_shutdown: list[Callable[[], Any]] = (
- [] if on_shutdown is None else list(on_shutdown)
- )
-
# Determine the lifespan context to use
if lifespan is None:
# Use the default lifespan that runs on_startup/on_shutdown handlers
@@ -985,6 +975,17 @@ def __init__(
assert not prefix.endswith("/"), (
"A path prefix must not end with '/', as the routes will start with '/'"
)
+
+ # Handle on_startup/on_shutdown locally since Starlette removed support
+ # Ref: https://github.com/Kludex/starlette/pull/3117
+ # TODO: deprecate this once the lifespan (or alternative) interface is improved
+ self.on_startup: list[Callable[[], Any]] = (
+ [] if on_startup is None else list(on_startup)
+ )
+ self.on_shutdown: list[Callable[[], Any]] = (
+ [] if on_shutdown is None else list(on_shutdown)
+ )
+
self.prefix = prefix
self.tags: list[Union[str, Enum]] = tags or []
self.dependencies = list(dependencies or []).......... [100%]
=============================== warnings summary ===============================
../../../../../../../Users/jp/repos/kaggle-gemini-coding-agent-post-training/.envs/overlays/starlette-0.52.1-py3-none-any/starlette/testclient.py:45
/Users/jp/repos/kaggle-gemini-coding-agent-post-training/.envs/overlays/starlette-0.52.1-py3-none-any/starlette/testclient.py:45: DeprecationWarning: The anyio.abc.BlockingPortal alias is deprecated, use anyio.from_thread.BlockingPortal instead.
_PortalFactoryType = Callable[[], AbstractContextManager[anyio.abc.BlockingPortal]]
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
10 passed, 1 warning in 0.50s