← All notes

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

Sources

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

Permanent ID: iil:note:v0np6jsn

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.},
}

Markdown

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.