Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

middleware causes exceptions to not be raised/handled silently #1977

Closed
Kludex opened this issue Dec 6, 2022 Discussed in #1976 · 3 comments · Fixed by #1940
Closed

middleware causes exceptions to not be raised/handled silently #1977

Kludex opened this issue Dec 6, 2022 Discussed in #1976 · 3 comments · Fixed by #1940
Labels
bug Something isn't working

Comments

@Kludex
Copy link
Sponsor Member

Kludex commented Dec 6, 2022

Discussed in #1976

Originally posted by fraser-langton December 6, 2022
From issue in FastAPI - issue was said to be from starlette

Was initially noticed in FastAPI 0.74.0, was fixed after 0.79.0 but has since regressed (FastAPI 0.88.0 it isn't working)

When exceptions are raised on a subapp, the exceptions are not propagated all the way to see in console
In the example code
/info raises an exception and the full stacktrace is seen in console
/private/info does not raise the exception and only INFO: 127.0.0.1:56308 - "GET /info HTTP/1.1" 500 Internal Server Error is shown in console

import uvicorn
from fastapi import FastAPI
from starlette.middleware.base import BaseHTTPMiddleware


app = FastAPI()


@app.get("/info")
def info():
    # raises Exception as expected, the traceback is seen in console
    raise Exception


private_api = FastAPI()


@private_api.get("/info")
def info():
    # exception is handled silently, no traceback is seen in console
    raise Exception


app.mount("/private", private_api)


class Middleware(BaseHTTPMiddleware):

    async def dispatch(self, request, call_next):
        return await call_next(request)


app.add_middleware(Middleware)  # when this is removed, the exceptions are raised for all routes


if __name__ == '__main__':
    uvicorn.run(app, port=8000)
@Kludex Kludex added the bug Something isn't working label Dec 6, 2022
@Kludex
Copy link
Sponsor Member Author

Kludex commented Dec 6, 2022

Probably introduced by #1715, if not... Maybe #1649?

cc @adriangb

@Kludex
Copy link
Sponsor Member Author

Kludex commented Dec 6, 2022

It was none of those PRs.

This was cased by #1609. Solution on #1940.

@odelmarcelle
Copy link

I'm experiencing this issue as well since the upgrade to starlette >= 0.28. I suspect this is linked to #2026?

I just ran the example above for those two cases:

  • starlette 0.36.3, fastapi 0.109.2: silently fails and only prints INFO: 127.0.0.1:51560 - "GET /private/info HTTP/1.1" 500 Internal Server Error to the console
  • starlette 0.27.0, fastapi 0.104.1: prints full traceback INFO: 127.0.0.1:52001 - "GET /info HTTP/1.1" 500 Internal Server Error, ERROR: Exception in ASGI application, Traceback (most recent call last): ...

I note that in the second case, the INFO logger does not show the full path (/private/info). I'm not sure why, but I was for sure sending a request to /private/info.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants