"request.json()" hangs indefinitely in middleware
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from fastapi import FastAPI, Request, Response
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
class Middleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
await request.json()
response = await call_next(request)
return response
app = FastAPI()
app.add_middleware(Middleware)
@app.post("/test")
async def test(test: dict) -> dict:
return {"data": "test"}
Description
- Run script with:
uvicorn main:app --reload
- Run CURL with POST request:
curl -X 'POST' 'http://localhost:8000/test' -H 'accept: application/json' -H 'Content-Type: application/json' -d '{"test": "test"}'
The application keeps hanging indefinitely and don’t send a response to the request. I tested the equivalent code using app = Starlette()
and it answers correctly.
Operating System
Linux
Operating System Details
Ubuntu 22.04
FastAPI Version
0.79.0
Python Version
Python 3.10.4
Additional Context
No response
Issue Analytics
- State:
- Created a year ago
- Comments:11 (2 by maintainers)
Top Results From Across the Web
Middleware Request parse Hangs forever · Issue #847 - GitHub
The problem I am seeing is that this triggers the request to hang forever. The following is how you can reproduce it. app...
Read more >Why does fastapi hang when adding a middleware to print the ...
json() is allowed only once in request life-cycle in API, and BaseHTTPMiddleware also creates a Request object on its own (which causes hanging...
Read more >Supporting Promises in Express Middleware - Mastering JS
Express doesn't support promises or async/await in middleware or routes. ... toString(); // Request will hang forever because `res.json()` ...
Read more >Request stuck on pending : r/node - Reddit
Here is my request code from React: ... Try add res.end() after the JSON output, that could end the ... Cors middleware is...
Read more >Supporting integration tests with WebApplicationFactory in ...
It's an edge case, but without it, the test could hang indefinitely. The DeferredHost is responsible for waiting for the application to properly ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@araujo88 It can’t be using
self.stream()
, for per request, only one time can useself.stream()
.Or you will hang forever because of
await self.event_message.wait()
You can see here: uvicorn/protocols/http/h11_impl.py#L543
This is because that when
self.stream()
done, there are no action to notifyself.event_message
Thisasyncio.Event
will be notified of four actions:And you can try this:
@araujo88 you can ref this issue: https://github.com/tiangolo/fastapi/issues/394
in summary it is a bug or design issue from starlette, and there is work around:
thus this middleware logic will work on every requests, we use it same way in our production env, works fine.
note:
Dependencies
will inject your auth logic into each request, for more detail ref: https://fastapi.tiangolo.com/tutorial/dependencies/