Repository navigation
Add manage_lifespan flag to setup_di - #50
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #49
setup_di(app, container)takes a keyword-onlymanage_lifespan: bool = True, with the same name and default as modern-di-fastmcp. With the default nothing changes. Withmanage_lifespan=False,setup_didoes not append theon_startupandon_cleanuphooks, 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):async with, two requests share the APP instance and its finalizer runs once, at the user's exitThe 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_ditoday, with or without this flag: the second call re-registersaiohttp_request_providerand raisesDuplicateProviderTypeError. modern-di-fastmcp skips the registration whenfind_provideralready finds one. The README note avoids promising aiohttp-to-aiohttp sharing; that fix can be a separate issue.