This commit is contained in:
vishwa 2025-12-16 21:09:33 +00:00 committed by GitHub
commit a2c649705d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 78 additions and 0 deletions

View File

@ -435,6 +435,31 @@ def get_request_handler(
if isinstance(raw_response, Response):
if raw_response.background is None:
raw_response.background = solved_result.background_tasks
elif solved_result.background_tasks:
from starlette.background import BackgroundTask, BackgroundTasks
# Create a new BackgroundTasks to hold both
combined_tasks = BackgroundTasks()
# Add existing response tasks
if isinstance(raw_response.background, BackgroundTasks):
combined_tasks.tasks.extend(raw_response.background.tasks)
elif isinstance(raw_response.background, BackgroundTask):
combined_tasks.tasks.append(raw_response.background)
# Add dependency tasks
# solved_result.background_tasks is always BackgroundTasks (from dependencies/utils.py)
if isinstance(solved_result.background_tasks, BackgroundTasks):
combined_tasks.tasks.extend(
solved_result.background_tasks.tasks
)
elif isinstance(
solved_result.background_tasks, BackgroundTask
): # pragma: no cover
# Should not happen for BackgroundTasks dependency but safe to handle
combined_tasks.tasks.append(solved_result.background_tasks)
raw_response.background = combined_tasks
response = raw_response
else:
response_args: Dict[str, Any] = {

24
tests/test_issue_11215.py Normal file
View File

@ -0,0 +1,24 @@
from fastapi import BackgroundTasks, FastAPI
from fastapi.testclient import TestClient
from starlette.responses import BackgroundTask, Response
app = FastAPI()
@app.get("/")
def endpoint(tasks: BackgroundTasks):
tasks.add_task(lambda: print("Dependency task executed"))
return Response(
content="Custom response",
background=BackgroundTask(lambda: print("Response task executed")),
)
client = TestClient(app)
def test_issue_11215(capsys):
client.get("/")
captured = capsys.readouterr()
assert "Dependency task executed" in captured.out
assert "Response task executed" in captured.out

View File

@ -0,0 +1,29 @@
from fastapi import BackgroundTasks, FastAPI
from fastapi.testclient import TestClient
from starlette.background import BackgroundTasks as StarletteBackgroundTasks
from starlette.responses import Response
app = FastAPI()
@app.get("/")
def endpoint(tasks: BackgroundTasks):
tasks.add_task(lambda: print("Dependency task"))
response_tasks = StarletteBackgroundTasks()
response_tasks.add_task(lambda: print("Response task"))
return Response(
content="Custom response",
background=response_tasks,
)
client = TestClient(app)
def test_issue_11215_response_background_tasks_collection(capsys):
client.get("/")
captured = capsys.readouterr()
assert "Dependency task" in captured.out
assert "Response task" in captured.out