Skip to content

Commit 881ae38

Browse files
authored
Merge pull request #3819 from plotly/bugfix/3812-fastapi-catchall-context
Bugfix: Fix no context in catchall route
2 parents 60b7711 + cad27cc commit 881ae38

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
66

77
## Fixed
88
- [#3822](https://github.com/plotly/dash/pull/3822) Fix `UnboundLocalError` for `user_callback_output` in async background callbacks (Celery and Diskcache managers) when the callback raises `PreventUpdate` or another exception before the variable is assigned.
9+
- [#3819](https://github.com/plotly/dash/pull/3819) Fix `RuntimeError: No active request in context` when a non-Dash path falls through to the FastAPI catch-all route. Fixes [#3812](https://github.com/plotly/dash/issues/3812).
910

1011
## [4.3.0] - 2026-06-18
1112

dash/backends/_fastapi.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,12 @@ def _setup_catchall(self):
334334
try:
335335
dash_app = get_app()
336336

337-
async def catchall(_request: Request):
338-
return Response(content=dash_app.index(), media_type="text/html")
337+
async def catchall(request: Request):
338+
token = set_current_request(request)
339+
try:
340+
return Response(content=dash_app.index(), media_type="text/html")
341+
finally:
342+
reset_current_request(token)
339343

340344
# pylint: disable=protected-access
341345
self.add_url_rule("{path:path}", catchall, methods=["GET"])

tests/backend_tests/test_preconfig_backends.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,22 @@ async def echo_post(request: Request):
334334
)
335335
assert resp.status_code == 200
336336
assert resp.json() == {"echo": {"hello": "world"}}
337+
338+
339+
def test_fastapi_catchall_request_context(dash_duo):
340+
"""Test that non-Dash paths falling through to the catch-all route work.
341+
342+
Regression test for https://github.com/plotly/dash/issues/3812
343+
The catch-all route renders ``dash_app.index()``, which needs a request
344+
context; without it the request raised ``RuntimeError: No active request in
345+
context`` and returned a 500.
346+
"""
347+
import requests
348+
349+
app = Dash(__name__, backend="fastapi")
350+
app.layout = html.Div("Dash is running")
351+
352+
dash_duo.start_server(app)
353+
354+
resp = requests.get(f"{dash_duo.server_url}/some/non-dash/path", timeout=5)
355+
assert resp.status_code == 200

0 commit comments

Comments
 (0)