-
-
Notifications
You must be signed in to change notification settings - Fork 948
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
Seemingly random error RuntimeError: No Reponse returned.
#1634
Comments
I was just about to report this very issue, seems like I'm not the only one who are struggling with that. I've been able to consistently reproduce it. fastapi==0.74.1 import asyncio
import contextlib
import threading
import time
import requests
import uvicorn
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import PlainTextResponse
from uvicorn import Config
class Server(uvicorn.Server):
"""Uvicorn server in a thread.
https://stackoverflow.com/a/66589593
"""
def install_signal_handlers(self):
pass
@contextlib.contextmanager
def run_in_thread(self):
thread = threading.Thread(target=self.run)
thread.start()
try:
while not self.started:
time.sleep(1e-3)
yield
finally:
self.should_exit = True
thread.join()
class MyMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request, call_next):
return await call_next(request)
# We need to add two middlewares to reproduce the issue
app = Starlette(middleware=[Middleware(MyMiddleware), Middleware(MyMiddleware)])
@app.route('/')
async def root(_):
"""Simulate a delay to be able to disconnect the client before the server responds."""
await asyncio.sleep(2)
return PlainTextResponse()
def main():
"""Start the server and make a request with timeout less than server response delay.
Observe "RuntimeError: No response returned" in the logs.
"""
server = Server(config=Config(app=app, host="0.0.0.0", port=8000))
with server.run_in_thread():
requests.get("http://0.0.0.0:8000/", timeout=1)
if __name__ == "__main__":
main() |
Alright this is a tough one, been debugging for a while. First off, i don't think the cause of my issue is the same as the discussion #1527 or the response above. My frontend client is awaiting the response from the server and showing the 500 internal server error in the GUI, so it is not disconnecting before the server responds. I've found a reproducible case for the bug in our application. As a temporary bugfix i have downgraded FastAPI to v0.68.2 (starlette v0.14.2), the error is introduced for us in v.0.69.0 (starlette v0.15.0). I believe this is caused by an unfortunate combination of middleware. We are using both GZipMiddleware and Prometheus FastAPI Instrumentator (middleware). GZipMiddleware has a bug which throws a uvicorn exception But when i add the Prometheus Instrumentator middleware, it seems that this exception is caught and reraised, ultimately leading to the What i don't understand:
Hoping some starlette/uvicorn/middleware experts have some insight here. Relevant issues: |
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 I'm away from the computer for some days, so I'll not be able to help. What I can say is that the There are not that many issues, and PRs, you can see the issues it has. EDIT: This was the fix for |
Great! Hopefully that fix will solve this issue. It does feel like the I have been trying to reproduce this bug in a simple app, but have not been able, so it is not just the combination of GZipMiddleware and Prometheus middleware, something else is needed in the mix. Looking forward to next release. |
I was able to reproduce and solve my issue. The last piece of the puzzle was an aioredis client used in both a dependency and a background task. This causes Recapping the reproduced issue:
Solution:
Reproduced issue:
|
The last message has a different error than the one in the description of this issue. As I said before, the issue caused between the interaction of EDIT: I've reread the last message... We are working on a solution for the interaction of bkg tasks and the |
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,
|
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
The text was updated successfully, but these errors were encountered: