Skip to content

Commit

Permalink
build: add warning about manual lifespan_context (#1878)
Browse files Browse the repository at this point in the history
  • Loading branch information
vectorvp authored Oct 28, 2024
1 parent 524af96 commit cff911e
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion faststream/broker/fastapi/router.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import warnings
from abc import abstractmethod
from contextlib import asynccontextmanager
from enum import Enum
Expand Down Expand Up @@ -165,6 +166,8 @@ def __init__(
self.contact = None

self.schema = None
# Flag to prevent double lifespan start
self._lifespan_started = False

super().__init__(
prefix=prefix,
Expand Down Expand Up @@ -316,7 +319,15 @@ async def start_broker_lifespan(
context = dict(maybe_context)

context.update({"broker": self.broker})
await self.broker.start()

if not self._lifespan_started:
await self.broker.start()
self._lifespan_started = True
else:
warnings.warn(
"Specifying 'lifespan_context' manually is no longer necessary with FastAPI >= 0.112.2.",
stacklevel=2,
)

for h in self._after_startup_hooks:
h_context = await h(app)
Expand Down

0 comments on commit cff911e

Please sign in to comment.