question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Seemingly random error `RuntimeError: No Reponse returned.`

See original GitHub issue

Same error though I don’t know if cause is the same, we have not been able to reproduce the error in our environments.

The issue

Our frontend is getting HTTP 500 errors from our FastAPI backend. The error is shown below. The error is seemingly random, we have not been able to reproduce it. The traceback does not lead anywhere to our own source code. It has happened on multiple different endpoint requests. We have wrapped our endpoint code in try/except blocks to catch any “hidden” error in our code to trigger this, but it did not catch anything, leading me to believe that this is an underlying error in the framework.

We discovered the error on FastAPI v0.71.0 (Starlette v0.17.1). We tried upgrading FastAPI to v0.76.0 (Starlette v0.18.0), the error became more frequent. We have now downgraded FastAPI to v0.68.2 hoping that the error dissipates.

python v3.8 uvicorn v0.15.0

INFO:   xxx - "POST /api/xxx HTTP/1.1" 500
ERROR:  Exception in ASGI application
Traceback (most recent call last):
  File "/xxx/venv/lib64/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py", line 375, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/xxx/venv/lib64/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
    return await self.app(scope, receive, send)
  File "/xxx/venv/lib64/python3.8/site-packages/fastapi/applications.py", line 208, in __call__
    await super().__call__(scope, receive, send)
  File "/xxx/venv/lib64/python3.8/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/xxx/venv/lib64/python3.8/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc
  File "/xxx/venv/lib64/python3.8/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/xxx/venv/lib64/python3.8/site-packages/starlette/middleware/base.py", line 63, in __call__
    response = await self.dispatch_func(request, call_next)
  File "/xxx/venv/lib64/python3.8/site-packages/prometheus_fastapi_instrumentator/instrumentation.py", line 172, in dispatch_middleware
    raise e from None
  File "/xxx/venv/lib64/python3.8/site-packages/prometheus_fastapi_instrumentator/instrumentation.py", line 169, in dispatch_middleware
    response = await call_next(request)
  File "/xxx/venv/lib64/python3.8/site-packages/starlette/middleware/base.py", line 45, in call_next
    raise RuntimeError("No response returned.")
RuntimeError: No Reponse returned.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Kludexcommented, May 13, 2022

We solved the GZipMiddleware issue a couple of days ago. You can look for my PRs, it’s one of the latest merged. It will be available on the next release - the issue was on the BaseHTTPMiddleware.

I’m away from the computer for some days, so I’ll not be able to help. What I can say is that the BaseHTTPMiddleware has more issues, which we are aware, but there shouldn’t be any regression on the latest versions related to that piece of code.

There are not that many issues, and PRs, you can see the issues it has.

EDIT: This was the fix for GZipMiddleware: https://github.com/encode/starlette/pull/1609/ .

0reactions
havardthomcommented, May 15, 2022

Some adjustments to the reproduced issue to give exception from original description, solutions (from my previous post) are still the same.

Edit: It would be nice to know what throws the exception, but i don’t know how to catch it, try/except in redis_dependency does not catch anything

redis_dependency start
INFO:     127.0.0.1:51642 - "GET / HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/home/havardthom/test_starlette_bug/py38/lib/python3.8/site-packages/starlette/middleware/base.py", line 43, in call_next
    message = await recv_stream.receive()
  File "/home/havardthom/test_starlette_bug/py38/lib/python3.8/site-packages/anyio/streams/memory.py", line 94, in receive
    return self.receive_nowait()
  File "/home/havardthom/test_starlette_bug/py38/lib/python3.8/site-packages/anyio/streams/memory.py", line 87, in receive_nowait
    raise EndOfStream
anyio.EndOfStream

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/havardthom/test_starlette_bug/py38/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py", line 372, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/home/havardthom/test_starlette_bug/py38/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
    return await self.app(scope, receive, send)
  File "/home/havardthom/test_starlette_bug/py38/lib/python3.8/site-packages/fastapi/applications.py", line 269, in __call__
    await super().__call__(scope, receive, send)
  File "/home/havardthom/test_starlette_bug/py38/lib/python3.8/site-packages/starlette/applications.py", line 124, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/havardthom/test_starlette_bug/py38/lib/python3.8/site-packages/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/home/havardthom/test_starlette_bug/py38/lib/python3.8/site-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/home/havardthom/test_starlette_bug/py38/lib/python3.8/site-packages/starlette/middleware/base.py", line 68, in __call__
    response = await self.dispatch_func(request, call_next)
  File "/home/havardthom/test_starlette_bug/./test_starlette_issue.py", line 55, in dispatch
    return await call_next(request)
  File "/home/havardthom/test_starlette_bug/py38/lib/python3.8/site-packages/starlette/middleware/base.py", line 47, in call_next
    raise RuntimeError("No response returned.")
RuntimeError: No response returned.
# test_starlette_issue_1634.py
# start redis service: docker run -d -p 6379:6379 --name myredis redis
# start server: uvicorn --factory test_starlette_issue_1634:create_app --reload
# send requests: python test_starlette_issue_1634.py
import asyncio

from fastapi import FastAPI, status, Request, BackgroundTasks, Depends, APIRouter
from starlette.middleware.base import BaseHTTPMiddleware

import httpx
from redis import asyncio as aioredis

class TestClass():
    def __init__(self, redis):
        self.redis = redis
    
    async def __call__(self):
        value = await self.redis.get("my-key")
        return value

def get_redis(r: Request):
    return r.app.state.redis

async def redis_dependency(r: Request):
    print("redis_dependency start")
    value = await r.app.state.get_test_class()
    print("redis_dependency end")
    return value

async def redis_bg_task(redis):
    print("redis_bg_task start")
    await redis.publish("test", "test")
    print("redis_bg_task end")


router = APIRouter()

@router.get("/")
def root(value = Depends(redis_dependency)):
    print("value root endpoint", value)
    return {"Hello": "World"}


@router.get("/no_content", status_code=status.HTTP_204_NO_CONTENT)
async def no_content(
    background_tasks: BackgroundTasks, 
    redis = Depends(get_redis),
    value = Depends(redis_dependency)
):
    print("value no_content endpoint", value)
    background_tasks.add_task(redis_bg_task, redis)

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

def create_app():
    app = FastAPI()

    app.add_middleware(MyMiddleware)

    app.state.redis = aioredis.from_url("redis://localhost")
    app.state.get_test_class = TestClass(app.state.redis)

    app.include_router(router)

    return app


if __name__ == "__main__":
    async def send_requests():
        async with httpx.AsyncClient(base_url="http://127.0.0.1:8000") as client:
            print("GET /no_content")
            r = await client.get("/no_content")
            print(r)
            await asyncio.sleep(0.1)
            print("GET /")
            r = await client.get("/")
            print(r)
            await asyncio.sleep(0.1)
            print("GET /")
            r = await client.get("/")
            print(r)

    asyncio.run(send_requests())
Read more comments on GitHub >

github_iconTop Results From Across the Web

RuntimeError: No response returned in FastAPI when refresh ...
After many search and debugging just figured out that it happens when i refresh my request before getting response(cancel request and send ...
Read more >
[Project] Stanford-Cars with fastai v1 - Deep Learning
First notebook in a series on image classification for the Stanford-Cars data using the fastai v1 library. Goal is 90%+ accuracy, ...
Read more >
Error message | Doom Wiki - Fandom
Error messages appear when the Doom engine aborts due to a runtime error which it considers to be unrecoverable. In addition to any...
Read more >
Fixed Problem Reports - Siemens PLM
The problem with uf5360 (create bounded plane) failing but not returning an error message has been fixed in Unigraphics V11.0. The input had...
Read more >
arcpy.sa save random ERROR 999998: Unexpected Error
Solved: I am working on a pilot for a system that will eventually loop over a number of raster files to produce a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found