-
Notifications
You must be signed in to change notification settings - Fork 320
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
Running FastAPI app and getting a Starlette error #1104
Comments
Thanks to @bunny-therapist for providing a reproducer, summarised below... gh1100.py from fastapi import FastAPI
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from fastapi.responses import Response
class RequestBody(BaseModel):
number: int
app = FastAPI()
@app.middleware("http")
async def my_middleware(request, call_next):
response = await call_next(request)
return response
@app.post("/predict")
async def post_predict(body: RequestBody):
return JSONResponse(content={"got": body.number})
@app.get("/data")
async def get_data():
return Response() gh1100.json {
"listeners": {
"[::1]:8080": {
"pass": "applications/fastapi"
}
},
"applications": {
"fastapi": {
"type": "python",
"path": "/home/andrew/src/python",
"module": "gh1100",
"callable": "app",
"protocol": "asgi"
}
}
} gh1100-client.py #!/usr/bin/python
import requests
N = 2
with requests.Session() as session:
for _ in range(N):
r = session.post("http://localhost:8080/predict", json={"number": 1})
r.raise_for_status() Running it produces the following in the unit log
This was with Fedora 39 with Python 3.12.1 and fastapi 0.109.0 installed via pip(1) Looking through git-log(1) these look potentially interesting to look at
|
Tomorrow, I plan to test out specific starlette commits to pinpoint when exactly the problems appeared. (You can pip install specific commits from github, see, e.g., https://stackoverflow.com/questions/13685920/install-specific-git-commit-with-pip) |
Thanks, that would be helpful... |
I can confirm that the If I have that commit, I get the error. If i go one commit back in time, the error goes away. |
As for the "invalid state" error for the GET requests, they start with starlette commit encode/starlette@11a3f12 |
Thanks! I'll take a look... |
@ac000 bumping on this one, any updates? thanks! |
Not yet I'm afraid, I'm sure any updates will be posted here, but I |
Hi I am facing the exact same issue. Is there any progress, or has anyone found some workaround for it? |
I am not following this very closely anymore because I switched from FastAPI to litestar, which works great. Litestar even recommends NGINX Unit in their docs and it is very actively maintained. With FastAPI, the only way I found around it was to use an older version, fastapi==0.107.0 starlette==0.28.0 |
It helped me - tiangolo/fastapi#8187 (reply in thread) |
Discussed in #1100
Originally posted by bunny-therapist January 29, 2024
I am running a FastAPI app using nginx unit, and using a python script to send multiple sequential (wait for response before sending next) requests to it with the "requests" library. When I reuse the same
requests.Session
object for all the requests, so thatrequests.Session.close
is not called, then I get intermittent crashes in the application and thus status 500 back.The error is coming from this line: https://github.com/encode/starlette/blob/master/starlette/middleware/base.py#L56
which appears to be starlette waiting for a client disconnect but instead getting the next request. (No idea why starlette wants the request to be closed instead of allowing it to be reused for the next request, but maybe there is something I am not aware of.)
I would (and will) ask about this in the starlette github, but I am posting here because I did not encounter this issue when running my FastAPI app with uvicorn, it only happens with nginx unit, so at a minimum, it appears that something is different here which is causing problems, and this is probably something that should be identified.
I was really happy when I discovered nginx unit; this issue is currently the only thing that is preventing me from adopting it.
The text was updated successfully, but these errors were encountered: