Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Multiple (nested) StreamRouters are not initialized #1842

Open
andrewduckett opened this issue Oct 10, 2024 · 6 comments
Open

Bug: Multiple (nested) StreamRouters are not initialized #1842

andrewduckett opened this issue Oct 10, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@andrewduckett
Copy link

Describe the bug

Hey @Lancetnik I'm having trouble when using multiple nested StreamRouters in FastAPI. I tried following the example here with no luck #677 (reply in thread)_

The application starts just fine but when accessing the broker to publish a message I get an error that the I need to connect first.

In the discussion is sounds like a single broker connection will be shared between all of the stream routers, which is what I would like to happen. I assume this has something to do with the lifecycle of each StreamRouter.

And/Or steps to reproduce the behavior:

  1. Create multiple StreamRouters (RabbitRouter in my case)
  2. Include those in either a StreamRouter or APIRouter as a core_router
  3. Include the core_router in FastAPI
  4. In one of the stream router methods, use router.broker.publish(...)

Expected behavior
I would like to create multiple RabbitRouters, nested in an APIRouter, included in a FastAPI Application that share a connection.

Observed behavior

AssertionError: Please, `connect()` the broker first.

Screenshots
If applicable, attach screenshots to help illustrate the problem.

Environment

Running FastStream 0.5.25 with CPython 3.12.1 on Darwin

FastAPI version 0.115.0

@andrewduckett andrewduckett added the bug Something isn't working label Oct 10, 2024
@Lancetnik
Copy link
Member

Did you checked our dicumentation? https://faststream.airt.ai/latest/getting-started/integrations/fastapi/#multiple-routers

It's been a long time since this discussion. FastAPI and FastStream had some breaking changes

Now I reccomend to use our regular FastStream routers as nested for FastAPI one - it is the most predictable behavior

@andrewduckett
Copy link
Author

Yes, I have tried that doc as well. Am I supposed to provide connection details to all RabbitRouters? or just the "core"? Do they still share a connection?

@Lancetnik
Copy link
Member

If you are using our Router (not FastAPI-compatible ones) the core Router copies all publishers and subscribers to itself and nested Routers doesn't require a real connection

@Lancetnik
Copy link
Member

Anyway, can you show me reproducible example I can copy-past?

@andrewduckett
Copy link
Author

andrewduckett commented Oct 11, 2024

This is an example of what is not working. It seems as though the nested (a_router and b_router) do not go through lifespan correctly (or at all) and as a result .publish errors with AssertionError: Please, 'connect()' the broker first.

# consolidated ...
from fastapi import FastAPI, APIRouter
from faststream.rabbit.fastapi import RabbitRouter

# ./a/router.py

a_router = RabbitRouter()

@a_router.post("/foo")
async def foo():
    await a_router.broker.publish("foo")


# ./b/router.py

b_router = RabbitRouter()

# ./api.py

api_router = APIRouter()
api_router.include_router(a_router)
api_router.include_router(b_router)

# ./main.py

def create_app() -> FastAPI:
    app = FastAPI()
    app.include_router(api_router)
    return app

app = create_app()

Note, if I include a_router and b_router directly in FastAPI the broker connects just fine.

# ./main.py

def create_app() -> FastAPI:
    app = FastAPI()
    app.include_router(a_router)
    app.include_router(b_router)
    return app

app = create_app()

It's not what I expected, but it's not a deal breaker if that's how it works.

@Lancetnik
Copy link
Member

Lancetnik commented Oct 11, 2024

@andrewduckett please, use faststream.rabbit.RabbitRouter instead of faststream.rabbit.fastapi.RabbitRouter for the nested ones

This is the recommended in the documentation way. But, I'll try to investigate your problem and fix it as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants