fix: refuse to bootstrap a second bootstrapper on one application - #167
Merged
Conversation
The teardown-attach guard only stopped the second teardown hook; the losing bootstrapper still applied every instrument on bootstrap(). Litestar then died with a duplicate-route error from inside the framework and FastAPI silently registered a second copy of its health and metrics routes. bootstrap() now raises ConfigurationError naming the bootstrapper class.
FastStream and FastMCP share the base-class seam; FreeBootstrapper has no application to own and must stay unaffected. The construction-time warning now says the second bootstrapper is unusable rather than merely untorn-down.
Record that the teardown-attach marker now gates instrument application too, and what the losing bootstrapper gets.
The ConfigurationError message claimed the other bootstrapper "has
already applied its instruments", which the marker cannot know — it is
set at construction, before bootstrap() ever runs. Reword to state
ownership instead. Extend ConfigurationError's docstring to cover the
new use, and pin the reworded half of the construction-time warning
("cannot be used ... bootstrap() will raise") on one of the four
sibling tests so it can't silently drift.
- planning/decisions/2026-06-24-teardown-marker-accepted-limits.md: record that both accepted marker limits now hard-fail at bootstrap() instead of warning and skipping, per the 2026-08-10.04 change. - architecture/bootstrappers.md: state the marker's dual gating as current fact rather than a changelog-style "now"; correct construction's effect — the losing bootstrapper is unusable from construction, only the failure is deferred; document that the marker is never cleared (not even by teardown()), so a bootstrapped application stays owned for the process's lifetime, and why clearing it on teardown wouldn't help for FastAPI. - planning/releases/1.4.0.md: point from Behavior change to the guard's bug fix entry, add the never-cleared-marker remedy, turn the FastAPI route count into an example rather than a fixed fact, and list the two missing change files under References. - CLAUDE.md: extend the "Teardown attaches once" invariant to mention the raise, not just the warn-and-skip. Also bring test_second_litestar_bootstrapper_bootstrap_raises in line with its three siblings by wrapping it in try/finally so a first.teardown() runs even if the assertion that second.bootstrap() raises fails.
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.
Implements
planning/changes/2026-08-10.04-double-bootstrap-guard.md, filed in #165._attach_teardown_onceguarded the teardown hook and nothing else, so the losing bootstrapper still applied every instrument onbootstrap(). Reproduced on main: Litestar died withImproperlyConfiguredException: Handler already registered for path '/health' and http method OPTIONS, which points nowhere near the real mistake, and FastAPI did not fail at all — the same app came back with its route count grown from 6 to 8, a shadowed duplicate of the health and metrics routes.bootstrap()now raisesConfigurationErrornaming the bootstrapper class. Construction still only warns, so the teardown seam's warn-and-skip invariant is unchanged; the warning's closing sentence now says the bootstrapper is unusable rather than merely untorn-down.Covered for all four app-bearing frameworks, plus a
FreeBootstrappertest pinning that the guard does not fire where there is no attach target. The FastAPI test asserts the route count is unchanged by a refused bootstrap — that is the assertion catching the silent half of the defect.Two things the review surfaced, both addressed here:
teardown(), so an application that has been bootstrapped stays owned for the life of the process. Behavior kept as-is per maintainer ruling; the remedy (construct a fresh application) is now documented inarchitecture/bootstrappers.mdand the release note, andplanning/decisions/2026-06-24-teardown-marker-accepted-limits.mdcarries an update noting both of its accepted limits now fail loudly.235 tests, 100% coverage,
just lint-ciandjust check-planningclean.🤖 Generated with Claude Code