1

The fastAPI that I am working on does not return traceback whenever the request failed, instead, it returns 500 Internal Server Error with error :

ERROR:    ASGI callable returned without starting response.
2021-05-14 16:12:08 - uvicorn.error:409 - ERROR - ASGI callable returned without starting response.

Anyone experienced this problem before and know the fix to it ?

Serena Xu
  • 219
  • 5
  • 10
  • can you include the endpoint api code you're trying to call? as well as the request/response headers with a `curl -Lv http:///`? As well as the version of fastapi, uvicorn, and python being used? – jmunsch May 14 '21 at 20:43
  • also try `uvicorn your.app:app --log-level debug` adding the log level debug to uvicorn, any changes? – jmunsch May 14 '21 at 20:45
  • @jmunsch it is fairly a big code base, that is why I did not include the code in the question. For versions, fastapi==0.63.0, uvicorn==0.13.3, python 3.8 – Serena Xu May 14 '21 at 20:46
  • @jmunsch I do have logging level set to debug. It would log out debugging info. However, I want to print out traceback whenever the server failed. It is kind of impossible to log out all possible errors. – Serena Xu May 14 '21 at 20:47
  • It'll be hard to find an answer if it's not reproducible. But basically somewhere in the code the server is returning without starting a response. https://github.com/encode/uvicorn/issues/201 it's suggested here that maybe explicitly sending back an empty string might be something to look at? Follow up question would be what endpoints were called just before the error in the logs? – jmunsch May 14 '21 at 20:50
  • 1
    @jmunsch I found out the reason fastAPI does not return traceback, but a general error. It was due a a middleware I created for the this API. The middleware subclassed from PrometheusMiddleware, add an extra label into Prometheus metrics and add an attribute into http request scope. I am not sure why this caused the problem of fastAPI not being able to return traceback properly. – Serena Xu May 14 '21 at 21:10

2 Answers2

2

Most probably the __ call __ method of your middleware is not working well. I would advise checking a build in middleware and comparing with yours. E.g:

async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
    if scope["type"] != "http":
        await self.app(scope, receive, send)
        return

    request = Request(scope, receive=receive)
    response = await self.dispatch_func(request, self.call_next)
    await response(scope, receive, send)
raghavsikaria
  • 867
  • 17
  • 30
0

My issue was caused by a customize middleware. I've been able to update the middleware and fix this issue.

Serena Xu
  • 219
  • 5
  • 10