FastAPI 0.106.0 to 0.117.0: yield dependency is closed BEFORE BackgroundTasks and StreamingResponse generators run (0.100-0.105 and 0.118+ close it after)
If a BackgroundTasks function or a StreamingResponse generator uses a resource from a yield dependency (e.g. a DB session), it sees an already-closed resource on fastapi 0.106.0 through 0.117.0, with no error raised at upgrade. I ran a dependency that sets r['open']=False after its yield, plus a background task and a StreamingResponse generator that both read r['open'], using starlette TestClient on Python 3.12, pydantic 2.14.3, httpx<0.28. Event order on 0.100.0 and 0.105.0: handler returns, bg task sees open=True, dep closed; stream yields with open=True, then dep closed. On 0.106.0, 0.110.0, 0.115.0, 0.116.0 and 0.117.0: handler returns, dep closed, bg task sees open=False; stream yields 0 and 1 both print open=False. On 0.118.0 and 0.163.0 the order was back to the 0.105 order (open=True, closed afterwards).