Skip to content

Add manage_lifespan flag to setup_di - #50

Merged
lesnik512 merged 1 commit into
mainfrom
feat/manage-lifespan
Oct 17, 2026
Merged

lesnik512 merged 1 commit into
mainfrom
feat/manage-lifespan

Conversation

@lesnik512

Copy link
Copy Markdown
Member

Closes #49

setup_di(app, container) takes a keyword-only manage_lifespan: bool = True, with the same name and default as modern-di-fastmcp. With the default nothing changes. With manage_lifespan=False, setup_di does not append the on_startup and on_cleanup hooks, so the integration never opens or closes the root container. It still stores the container on the app, registers the connection providers and installs the middleware, so each connection still gets its own child container, closed when the connection ends.

Skipping the hooks also means the root is not reopened at startup. If the user hands over a closed container with the flag off, requests fail with ContainerClosedError. That matches the intent: the user owns the root.

Tests

New tests in tests/test_lifespan.py, confirmed failing before the change (both with the old signature and with the keyword accepted but ignored):

  • startup, one request and shutdown with the flag off leave the root open; the REQUEST finalizer runs, the APP finalizer does not
  • with the flag off, a closed root is not reopened at startup
  • with the user owning the root in async with, two requests share the APP instance and its finalizer runs once, at the user's exit

The existing default-path tests are unchanged.

Docs

README gets the flag in the API table and a "Sharing a container" section with a runner example. CONTEXT.md's definition of root container said it is opened on startup and closed on cleanup; it now names the exception.

Not covered

Two aiohttp apps cannot share one container through setup_di today, with or without this flag: the second call re-registers aiohttp_request_provider and raises DuplicateProviderTypeError. modern-di-fastmcp skips the registration when find_provider already finds one. The README note avoids promising aiohttp-to-aiohttp sharing; that fix can be a separate issue.

@lesnik512
lesnik512 merged commit b77fdaf into main Oct 17, 2026
12 checks passed
@lesnik512
lesnik512 deleted the feat/manage-lifespan branch October 17, 2026 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add manage_lifespan=False so the user can own a shared root container

1 participant