Skip to content

fix: accept an app whose broker is created in an on_startup hook - #57

Merged
lesnik512 merged 1 commit into
mainfrom
fix/broker-created-on-startup
Sep 15, 2026
Merged

lesnik512 merged 1 commit into
mainfrom
fix/broker-created-on-startup

Conversation

@lesnik512

Copy link
Copy Markdown
Member

Closes #56. Option 3 from the issue.

What

setup_di raised Broker must be defined to setup DI when the app had no broker at call time. FastStream supports creating the broker inside an on_startup hook and attaching it with app.add_broker (its set_broker docstring names this as the intended use), and the guard rejected that app at import time.

How

  • The guard in setup_di is gone. Since fix: install the DI middleware on every broker, on startup #52 the middleware is installed by a startup hook that reads app.brokers, so nothing at setup_di time depends on a broker existing.
  • The check moves into that install hook. If app.brokers is still empty when it runs, it raises a RuntimeError naming both remedies: pass a broker to FastStream(...) or app.add_broker(...), and register a broker-adding on_startup hook before setup_di, because hooks run in registration order. This fires before FastStream's own assert self.brokers.
  • The FromDI message-time error from fix: name setup_di and app startup when FromDI finds no request container #55 gains one sentence for the other order: a broker added by a hook registered after setup_di is not covered.
  • README setup_di bullet and ADR-0002 updated. The ADR paragraph that deferred this decision now records it, including the one case that still surfaces at message time: a later hook adds a broker while another broker already exists.

Tests

Both written first and red on main with the old guard:

  • test_broker_added_by_a_startup_hook_registered_before_setup_di_gets_di: FastStream() with no broker, an on_startup hook that calls add_broker, then setup_di; a FromDI subscriber on that broker resolves under TestApp.
  • test_app_without_broker_at_startup_names_both_remedies: setup_di on a broker-less app succeeds, and app.start() raises the new error.

test_app_without_broker is replaced by these two, since setup_di no longer raises there. just lint-ci clean, just test-ci at 100% coverage.

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
@lesnik512
lesnik512 merged commit ab0eaee into main Sep 15, 2026
8 checks passed
@lesnik512
lesnik512 deleted the fix/broker-created-on-startup branch September 15, 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.

setup_di refuses an app whose broker is created in an on_startup hook

1 participant