Issue 1:
First 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.

After submitting this, I commit to one of:

Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.

I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.

Implement a Pull Request for a confirmed bug.

Example

Here's a self-contained, minimal, reproducible, example with my use case:

server.py

from time import time

from fastapi import FastAPI, Request, Response

app = FastAPI()

@app.middleware("http")
async def middle(request: Request, call_next):
return await call_next(request)

@app.post("/")
async def handler(request: Request):
print("processing")
request_json = await request.json()

# Add processing time
s_time = time()
count = 0
while count < 10_000:
count += 1
print(f"done: {time() - s_time}")

return request_json

test_server.py

from os import getenv
from unittest import TestCase

from requests import Session
from requests.exceptions import HTTPError

class TestModelServer(TestCase):
def setUp(self):
host = getenv("HOST", "localhost")
port = getenv("PORT", "8000")
self.url = f"http://{host}:{port}"

def test_post(self):
with Session() as s:
for i in range(1000):
data = [i for i in range(5)]
resp = s.post(self.url, json=data)
resp.raise_for_status()
result = resp.json()
self.assertEqual(result, data)

docker-compose.yml

version: "3"
services:
test:
image: python:3.8.3
environment:
HOST: app
command:
- /bin/sh
- -c
- |
set -eu
cd /app
pip install --no-cache-dir requests
# Wait until app is ready
until $$(curl -s $$HOST:8000/metrics > /dev/null); do
echo "app not ready"
sleep 5
done
python -m unittest -v test_server.py
volumes:
- ./test_server.py:/app/test_server.py
app:
image: python:3.8.3
command:
- /bin/sh
- -c
- |
cd /app
pip install --no-cache-dir fastapi uvicorn
uvicorn --host "0.0.0.0" --workers 1 server:app
volumes:
- ./server.py:/app/server.py

Description

Put all 3 files in the same directory and run docker-compose up --abort-on-container-exit --build

test_server.py hangs after completing several POST requests.

But I expected it to run to completion.

If I remove the middleware, tests complete as expected. It also seems to work if I close the connection from client side after every request, (ie. use a new requests.Session object).

Environment

OS: Docker on macOS

FastAPI Version: 0.59.0

To know the FastAPI version use:

python -c "import fastapi; print(fastapi.__version__)"

Python version: 3.8.3

To know the Python version use:

python --version

Additional context

Issue 2:
First 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

router.add_api_route(
"/clear",
clear,
methods=["POST", "DELETE"]
)

Description

Seems to be caused by #4650.

The new generate_unique_id() function uses list(route.methods)[0].lower() as suffix for the operation_id. Therefore, in my example, both post and delete endpoints get _post suffix for operation_id, causing it to no longer be unique.

It then issues a "UserWarning: Duplicate Operation ID"

Operating System

Windows

Operating System Details

No response

FastAPI Version

0.75.0

Python Version

3.10.2

Additional Context

No response