Skip to content

[BUG]: Fastapi page routes trigger 500 internal server error #3834

Description

@joschrag

Thank you so much for helping improve the quality of Dash!

We do our best to catch bugs during the release process, but we rely on your help to find the ones that slip through.

Describe your context
Please provide us your environment, so we can easily reproduce the issue.

dash 4.3.0

Describe the bug

When creating a simple dash app with fastapi backend like this:

app.py

importdashfromdashimportDash, htmlapp=Dash(__name__, backend="fastapi", use_pages=True)
app.layout=html.Div([html.H1("MWE"), dash.page_container])
if__name__=="__main__":
app.run()

pages/index.py

importdashfromdashimporthtmldash.register_page(__name__, path="/", layout=html.Div("home"))

pages/home.py

importdashfromdashimporthtmldash.register_page(__name__, path="/home", layout=html.Div("home"))

When browsing these pages or with curl we get the following:

curl -w "%{http_code}" -o NUL -s http://127.0.0.1:8050/ -> 200
curl -w "%{http_code}" -o NUL -s http://127.0.0.1:8050/home -> 500

with the traceback:

Traceback (mostrecentcalllast):
File"...\uvicorn\protocols\http\httptools_impl.py", line421, inrun_asgiresult=awaitapp( # type: ignore[func-returns-value]^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"...\uvicorn\middleware\proxy_headers.py", line62, in__call__returnawaitself.app(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"...\fastapi\applications.py", line1159, in__call__awaitsuper().__call__(scope, receive, send)
File"...\starlette\applications.py", line90, in__call__awaitself.middleware_stack(scope, receive, send)
File"...\starlette\middleware\errors.py", line186, in__call__raiseexcFile"...\starlette\middleware\errors.py", line164, in__call__awaitself.app(scope, receive, _send)
File"...\dash\backends\_fastapi.py", line235, in__call__awaitself.app(scope, receive, send)
File"...\starlette\middleware\exceptions.py", line63, in__call__awaitwrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
File"...\starlette\_exception_handler.py", line53, inwrapped_appraiseexcFile"...\starlette\_exception_handler.py", line42, inwrapped_appawaitapp(scope, receive, sender)
File"...\fastapi\middleware\asyncexitstack.py", line18, in__call__awaitself.app(scope, receive, send)
File"...\starlette\routing.py", line660, in__call__awaitself.middleware_stack(scope, receive, send)
File"...\starlette\routing.py", line680, inappawaitroute.handle(scope, receive, send)
File"...\starlette\routing.py", line276, inhandleawaitself.app(scope, receive, send)
File"...\fastapi\routing.py", line134, inappawaitwrap_app_handling_exceptions(app, request)(scope, receive, send)
File"...\starlette\_exception_handler.py", line53, inwrapped_appraiseexcFile"...\starlette\_exception_handler.py", line42, inwrapped_appawaitapp(scope, receive, sender)
File"...\fastapi\routing.py", line120, inappresponse=awaitf(request)
^^^^^^^^^^^^^^^^File"...\fastapi\routing.py", line674, inappraw_response=awaitrun_endpoint_function(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"...\fastapi\routing.py", line328, inrun_endpoint_functionreturnawaitdependant.call(**values)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"...\dash\backends\_fastapi.py", line338, incatchallreturnResponse(content=dash_app.index(), media_type="text/html")
^^^^^^^^^^^^^^^^File"...\dash\dash.py", line1302, inindexrequest=self.backend.request_adapter()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"...\dash\backends\_fastapi.py", line842, in__init__self._request: Request=get_current_request()
^^^^^^^^^^^^^^^^^^^^^File"...\dash\backends\_fastapi.py", line109, inget_current_requestraiseRuntimeError("No active request in context")
RuntimeError: Noactiverequestincontext

#3805 seems to be the cause as the /home route does not match any conditions and exits before the request context is set in dash/backends/_fastapi.py

asyncdef__call_():
...
if (
notpath.startswith(dash_prefix)
andpath!=prefixandpath!=prefix.rstrip("/")
):
awaitself.app(scope, receive, send) # /home fails within herereturnrequest=Request(scope, receive=receive)
token=set_current_request(request)
...

Expected behavior

The /home path should return a 200 Response and its content.

Screenshots

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions