From cff911e9413300a4874c639baee1407e62087d3c Mon Sep 17 00:00:00 2001 From: Vladislav Tumko <56307628+vectorvp@users.noreply.github.com> Date: Mon, 28 Oct 2024 20:08:16 +0400 Subject: [PATCH] build: add warning about manual lifespan_context (#1878) --- faststream/broker/fastapi/router.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/faststream/broker/fastapi/router.py b/faststream/broker/fastapi/router.py index baf7ceff53..b7249491ff 100644 --- a/faststream/broker/fastapi/router.py +++ b/faststream/broker/fastapi/router.py @@ -1,4 +1,5 @@ import json +import warnings from abc import abstractmethod from contextlib import asynccontextmanager from enum import Enum @@ -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, @@ -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)