fix: install the DI middleware on every broker, on startup - #52
Merged
Merged
Conversation
FastStream 0.7 apps hold a list of brokers and app.broker is only the first, so setup_di left brokers 2..N without DI. The first message to a FromDI subscriber on one of them failed with a None request container and nothing at setup time pointing at the cause. setup_di now registers an on_startup hook that adds the middleware to each broker in app.brokers not yet carrying it. Startup is when the broker list is complete, so a broker added via app.add_broker after setup_di is covered too, and the membership check keeps a restarted app from stacking a second copy. Rationale in docs/adr/0002-install-middleware-on-startup.md. Closes #42
This was referenced Sep 15, 2026
Closed
lesnik512
added a commit
that referenced
this pull request
Sep 15, 2026
setup_di refused an app with no broker at call time, which blocked the FastStream shape where the broker is built inside an on_startup hook and attached with app.add_broker. Since #52 the broker list is read at startup, so the guard was no longer needed for correctness. The guard moves into the startup install hook: it raises when the broker list is still empty when it runs, naming both remedies (pass a broker to FastStream or app.add_broker; register a broker-adding hook before setup_di, since hooks run in registration order). The FromDI message-time error names the other order. ADR-0002 records the change. Closes #56
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 #42
What
setup_diinstalled the DI middleware onapp.brokeronly. FastStream 0.7 apps hold a list of brokers (FastStream(*brokers),app.add_broker), andapp.brokeris justbrokers[0], so every other broker ran without DI. The failure was silent at setup: the first message to aFromDIsubscriber on another broker died withAttributeError: 'NoneType' object has no attribute 'resolve_dependency', becauseContextRepo.getreturnsNonefor the never-set request container key.How
setup_dino longer callsadd_middlewareitself. It registers anon_startuphook that walksapp.brokersand adds the middleware factory to each broker that does not already carry it.app.add_brokeraftersetup_diis covered without documenting an ordering rule (the issue's options 1 and 2 both miss that case; option 3 refuses a shape FastStream supports).config.broker_middlewares, so a middleware added in a startup hook applies to subscribers registered earlier.broker.config.broker_middlewares, the same sequence FastStream builds the stack from, so a stop/start cycle does not add a second copy.if not app.brokerguard is unchanged. Relaxing it for brokers created inside a user's ownon_startuphook is possible but depends on hook ordering, so it is left for its own issue.Recorded in
docs/adr/0002-install-middleware-on-startup.md, including the revisit trigger.Tests
test_di_resolves_on_every_broker, parametrized over both brokers at construction and the second broker added aftersetup_di. Both cases failed onmain.test_middleware_is_installed_once_per_broker_across_restartsguards the idempotency check across twoTestAppcycles.just lint-ciclean,just test-ciat 100% coverage.Follow-ups not in this PR
RuntimeErrornamingsetup_difromDependency.__call__when the request container is missing, instead of the currentAttributeErroronNone.