Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).

## [UNRELEASED]

### Fixed
- [#3805](https://github.com/plotly/dash/pull/3805) Fix FastAPI POST routes deadlock caused by middleware consuming request body. Fixes [#3801](https://github.com/plotly/dash/issues/3801).

## [4.2.0] - 2026-06-01 - *The Freedom Update*

This release marks a major milestone for Dash, bringing unprecedented flexibility to how you build and deploy your applications.
Expand Down
14 changes: 13 additions & 1 deletion dash/backends/_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,19 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
await self.app(scope, receive, send)
return

# HTTP/WebSocket request handling
# Non-Dash routes pass through to avoid consuming body stream
path = scope["path"]
prefix = self.dash_app.config.routes_pathname_prefix
dash_prefix = prefix.rstrip("/") + "/_dash-"
if (
not path.startswith(dash_prefix)
and path != prefix
and path != prefix.rstrip("/")
):
await self.app(scope, receive, send)
return
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning here skips _run_before_hooks and _run_after_hooks. Is that intentional? You could move the check inside the try/except block and only call _setup_timing if it's a Dash route.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes intentional, those are only for emulating the flask api for the dash setup, not intended for the user to use.


# HTTP request handling
request = Request(scope, receive=receive)
token = set_current_request(request)

Expand Down
43 changes: 43 additions & 0 deletions tests/backend_tests/test_preconfig_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,46 @@ def test_silence_routes_logging(backend, expected_loggers):
assert (
logger.level == logging.ERROR
), f"Logger {logger_name} should be set to ERROR level for {backend} backend"


def test_fastapi_custom_post_route(dash_duo):
"""Test that user-defined POST routes work with FastAPI backend.
Regression test for https://github.com/plotly/dash/issues/3801
The DashMiddleware was consuming the request body for all routes,
causing POST requests to user-defined routes to hang.
"""
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
import requests

fastapi_app = FastAPI()

@fastapi_app.get("/api/echo")
async def echo_get():
return JSONResponse({"method": "GET", "ok": True})

@fastapi_app.post("/api/echo")
async def echo_post(request: Request):
body = await request.json()
return JSONResponse({"echo": body})

app = Dash(__name__, server=fastapi_app)
app.layout = html.Div("Dash is running")

dash_duo.start_server(app)

# Test GET request
url = dash_duo.server_url
resp = requests.get(f"{url}/api/echo", timeout=5)
assert resp.status_code == 200
assert resp.json() == {"method": "GET", "ok": True}

# Test POST request - this was hanging before the fix
resp = requests.post(
f"{url}/api/echo",
json={"hello": "world"},
timeout=5,
)
assert resp.status_code == 200
assert resp.json() == {"echo": {"hello": "world"}}
Loading