Dependencies with yield not working with StreamingResponse #11444
Replies: 2 comments
-
@tiangolo could you please share your thoughts here? |
Beta Was this translation helpful? Give feedback.
-
I've just found out this issue as well. This behavior was introduced in version 0.106, when they changed how dependencies with yield work: https://fastapi.tiangolo.com/release-notes/#01060. Here's my snipped code with similar issue: class TempClient:
async def __aenter__(self):
self.x = "a"
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
self.x = "b"
async def get_x(self):
yield self.x
await asyncio.sleep(1)
yield self.x
await asyncio.sleep(1)
yield self.x
await asyncio.sleep(1)
async def get_tmp_client():
async with TempClient() as tmp_client:
yield tmp_client
@app.get("/logs")
async def get_stream_logs(tmp_client: TempClient = Depends(get_tmp_client)):
return StreamingResponse(
tmp_client.get_x(),
media_type="text/plain"
) Output with version 0.105.0: curl -X 'GET' \
'http://localhost:5000/logs' \
-H 'accept: application/json' --no-buffer
aaa% Output with version 0.106.0: curl -X 'GET' \
'http://localhost:5000/logs' \
-H 'accept: application/json' --no-buffer
bbb% I believe they have though that this would only impact Background tasks, without measuring the impact on Streaming responses. |
Beta Was this translation helpful? Give feedback.
-
First Check
Commit to Help
Example Code
Description
After updating to
0.110.0
, Dependencies with yield not working. Finally block has been executed before the streaming completed.Expected Behavior: Finally block should execute after the streaming completed.
Operating System
Windows
Operating System Details
No response
FastAPI Version
0.110.0
Pydantic Version
2.6.3
Python Version
3.11
Additional Context
starlette==0.36.3
Beta Was this translation helpful? Give feedback.
All reactions