Skip to content

fix: refuse to bootstrap a second bootstrapper on one application - #167

Merged
lesnik512 merged 5 commits into
mainfrom
fix/double-bootstrap-guard
Aug 10, 2026
Merged

fix: refuse to bootstrap a second bootstrapper on one application#167
lesnik512 merged 5 commits into
mainfrom
fix/double-bootstrap-guard

Conversation

@lesnik512

Copy link
Copy Markdown
Member

Implements planning/changes/2026-08-10.04-double-bootstrap-guard.md, filed in #165.

_attach_teardown_once guarded the teardown hook and nothing else, so the losing bootstrapper still applied every instrument on bootstrap(). Reproduced on main: Litestar died with ImproperlyConfiguredException: 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 raises ConfigurationError naming 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 FreeBootstrapper test 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:

  • The error message originally claimed the other bootstrapper "has already applied its instruments", which the code cannot know — the marker is set at construction, so the owner may never have bootstrapped. It now states ownership instead.
  • The marker is never cleared, including by 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 in architecture/bootstrappers.md and the release note, and planning/decisions/2026-06-24-teardown-marker-accepted-limits.md carries an update noting both of its accepted limits now fail loudly.

235 tests, 100% coverage, just lint-ci and just check-planning clean.

🤖 Generated with Claude Code

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.
@lesnik512
lesnik512 merged commit 2a1066d into main Aug 10, 2026
11 checks passed
@lesnik512
lesnik512 deleted the fix/double-bootstrap-guard branch August 10, 2026 18:28
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.

1 participant