Repository navigation
fix: close the root containers of mounted apps at shutdown - #70
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 #69
Starlette runs only the outermost app's lifespan. A mounted app that called
setup_dinever had its root container entered or closed, so its APP-scoped finalizers never ran.Change
The lifespan that
setup_dicomposes now walks the app's routes at startup. For everyMountandHostroute whose app is aFastAPIapp with a root container (recognised throughfetch_di_container), it enters that container. It recurses through each route'sroutes, so apps mounted more than one level deep are found, including underMount(routes=[...])and under mounted apps that did not callsetup_di.All containers go on one
AsyncExitStackinside the user lifespan, the app's own container first. They close in reverse order at shutdown, after the user's lifespan code. A container is entered once, compared by identity, so an app mounted twice or several apps sharing one container close it once. Because the walk runs at startup, mounts added aftersetup_diare included. The mounted app's own user lifespan still does not run.This is the same shape as modern-di-starlette#55: one
_mounted_containersgenerator and the same test file layout.Notes
FastAPIapps are checked, becausesetup_diandfetch_di_containertake aFastAPIapp. A plain Starlette app mounted inside is skipped, but its routes are still walked.Mount(app, middleware=[...])wraps the app, soroute.appis the middleware and not theFastAPIapp. Such a mounted app's container is not found. Reaching it would mean reading Starlette's privateMount._base_app. Its nested routes are still walked through the publicMount.routes.setup_ditwice with the same container raisesDuplicateProviderTypeError, because the connection providers are registered again. The shared-container test therefore shares the container by settingapp.state.di_containerdirectly. That test passes before the fix too; it guards the deduplication.Tests
tests/test_mount_lifespan.pycovers the issue repro, a container two levels deep, aHostroute, an app mounted twice, a shared container, a mount added aftersetup_di, ignored mounts (FastAPI app withoutsetup_di, plain Starlette app, raw ASGI app,Mount(routes=...)), and two lifespan cycles. Six of the eight fail on main. The issue repro now recordsinner finalizedat shutdown.