From 258ee6eec72118403c78340a1d963078601573f9 Mon Sep 17 00:00:00 2001 From: kumarvishwajeettrivedi Date: Sat, 22 Nov 2025 18:20:33 +0530 Subject: [PATCH 1/3] Fix: Merge background tasks from dependency and custom response --- fastapi/routing.py | 21 +++++++++++++++++++++ tests/test_issue_11215.py | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 tests/test_issue_11215.py diff --git a/fastapi/routing.py b/fastapi/routing.py index a8e12eb60..012a79b4c 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -396,6 +396,27 @@ 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): + # 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] = { diff --git a/tests/test_issue_11215.py b/tests/test_issue_11215.py new file mode 100644 index 000000000..ce690a47f --- /dev/null +++ b/tests/test_issue_11215.py @@ -0,0 +1,21 @@ +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 From 3a84d397fb9807f099c5ed7a6840ad512f8959c4 Mon Sep 17 00:00:00 2001 From: kumarvishwajeettrivedi Date: Sun, 23 Nov 2025 00:08:20 +0530 Subject: [PATCH 2/3] Test: Add coverage for BackgroundTasks collection and pragma for unreachable code --- fastapi/routing.py | 2 +- tests/test_issue_11215_coverage.py | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 tests/test_issue_11215_coverage.py diff --git a/fastapi/routing.py b/fastapi/routing.py index 012a79b4c..9d6759ba4 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -412,7 +412,7 @@ def get_request_handler( # 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): + 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) diff --git a/tests/test_issue_11215_coverage.py b/tests/test_issue_11215_coverage.py new file mode 100644 index 000000000..a1134aecf --- /dev/null +++ b/tests/test_issue_11215_coverage.py @@ -0,0 +1,26 @@ +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 From d24bfcf22017e6ddafd8f9fcbe630759916835af Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 22 Nov 2025 18:46:28 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20for?= =?UTF-8?q?mat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/routing.py | 16 ++++++++++------ tests/test_issue_11215.py | 3 +++ tests/test_issue_11215_coverage.py | 7 +++++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/fastapi/routing.py b/fastapi/routing.py index 9d6759ba4..f7281f20b 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -401,21 +401,25 @@ def get_request_handler( # 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.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: diff --git a/tests/test_issue_11215.py b/tests/test_issue_11215.py index ce690a47f..00e4cfa82 100644 --- a/tests/test_issue_11215.py +++ b/tests/test_issue_11215.py @@ -4,6 +4,7 @@ from starlette.responses import BackgroundTask, Response app = FastAPI() + @app.get("/") def endpoint(tasks: BackgroundTasks): tasks.add_task(lambda: print("Dependency task executed")) @@ -12,8 +13,10 @@ def endpoint(tasks: BackgroundTasks): background=BackgroundTask(lambda: print("Response task executed")), ) + client = TestClient(app) + def test_issue_11215(capsys): client.get("/") captured = capsys.readouterr() diff --git a/tests/test_issue_11215_coverage.py b/tests/test_issue_11215_coverage.py index a1134aecf..1eaa96fc5 100644 --- a/tests/test_issue_11215_coverage.py +++ b/tests/test_issue_11215_coverage.py @@ -5,20 +5,23 @@ 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()