A FastAPI lifespan context manager runs code at startup and shutdown
Answer: Pass a lifespan async context manager to the FastAPI app for startup and shutdown work. Code before yield runs once, before the app starts receiving requests. Code after yield runs once, after the app has finished handling requests. Use this for shared resources such as a connection pool.
How to write it
Define an async function with yield. The function is decorated with @asynccontextmanager. Pass that async context manager to the lifespan parameter of the FastAPI app. The recommended way to handle startup and shutdown is this lifespan parameter. If you provide a lifespan parameter, the older startup and shutdown event handlers are not called.
Lifespan events run only for the main application, not for mounted sub-applications.
Starlette's lifespan
Starlette applications can register a lifespan handler for startup and shutdown work. Pass it as lifespan. Incoming requests are not served until that handler has run. Teardown happens after connections are closed and in-process background tasks have completed. Lifespan state is a dictionary used to share objects between the lifespan and the requests.
See also
Claims
- id: C1 claim: Startup code in the lifespan function runs before the app takes requests. source: S1 evidence: "executed before the application starts taking requests"
- id: C2 claim: The FastAPI lifespan parameter takes an async context manager. source: S1 evidence: "takes an async context manager"
- id: C3 claim: Starlette does not serve requests until the lifespan has run. source: S2 evidence: "will not start serving any incoming requests until the lifespan has been run"
- id: C4 claim: Starlette lifespan teardown runs after connections and background tasks finish. source: S2 evidence: "run once all connections have been closed"
Sources
- [S1] Lifespan Events. fastapi/fastapi (GitHub). https://github.com/fastapi/fastapi/blob/master/docs/en/docs/advanced/events.md . License: MIT. Accessed 2026-11-17.
- [S2] Lifespan (Starlette). Kludex/starlette (GitHub). https://github.com/Kludex/starlette/blob/main/docs/lifespan.md . License: BSD-3-Clause. Accessed 2026-11-17.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: fastapi/fastapi (GitHub), Lifespan Events, MIT (https://raw.githubusercontent.com/fastapi/fastapi/master/LICENSE). Copyright (c) 2018 Sebastián Ramírez; Kludex/starlette (GitHub), Lifespan (Starlette), BSD-3-Clause (https://raw.githubusercontent.com/Kludex/starlette/main/LICENSE.md). Copyright (c) 2018, Encode OSS Ltd. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: fastapi/fastapi (GitHub), Lifespan Events, MIT (https://raw.githubusercontent.com/fastapi/fastapi/master/LICENSE). Copyright (c) 2018 Sebastián Ramírez; Kludex/starlette (GitHub), Lifespan (Starlette), BSD-3-Clause (https://raw.githubusercontent.com/Kludex/starlette/main/LICENSE.md). Copyright (c) 2018, Encode OSS Ltd.
Cite this
iinventedlight. 2026. "A FastAPI lifespan context manager runs code at startup and shutdown." iinventedlight Library, iil:note:v0np6jsn, version of 2026-11-17. https://iinventedlight.com/library/notes/fastapi-lifespan/. License: CC-BY-4.0.
@misc{iil_note_v0np6jsn,
author = {{iinventedlight}},
title = {{A FastAPI lifespan context manager runs code at startup and shutdown}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-17},
url = {https://iinventedlight.com/library/notes/fastapi-lifespan/},
note = {Permanent ID iil:note:v0np6jsn. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/fastapi-lifespan.md.