Skip to content

feat: add manage_lifespan to setup_di - #78

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 #77

setup_di(app, container) gets a keyword-only manage_lifespan: bool = True, matching modern-di-fastmcp. With the default nothing changes. With manage_lifespan=False the app never opens or closes the root container, so the user can own it, for example with async with container: around running the app. Providers are still registered and the middleware still builds and closes a request container per message.

The container.open hook now registers after install_middleware instead of before it. Both run on startup before the broker consumes, so the order only shows if install_middleware raises for a missing broker.

README: the flag is in the API table, plus a short "Sharing a container" section. CONTEXT.md: the root container entry now says its app-tracked lifetime is the default.

Tests in tests/test_lifespan.py, written first and failing before the change:

  • with the flag off, a full startup, one message and shutdown leave the root open and its APP finalizer unrun
  • with the flag off, a request-scoped dependency still resolves and its request container is closed (its finalizer runs once)
  • with the flag off and the root in async with, the APP finalizer runs once, at the user's exit

The existing restart test covers the default.

@lesnik512
lesnik512 merged commit 060cc65 into main Oct 17, 2026
12 checks passed
@lesnik512
lesnik512 deleted the feat/manage-lifespan branch October 17, 2026 12:55
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