aio-libs / aiohttp

Asynchronous HTTP client/server framework for asyncio and Python
https://docs.aiohttp.org
Other
15.03k stars 2k forks source link

Wrong response for simultaneous requests with a long processing time #7635

Open ulysses500 opened 1 year ago

ulysses500 commented 1 year ago

Describe the bug

I have a bug where two separate requests, a few seconds apart, consistently receive the response from the first request. Here is the client-side request:

async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=1200)) as aiohttp_session:
    PS_extension = "/" + str(random.randint(1, 1000000))
                        headers = {
                        'Cache-Control': 'no-cache'
                    }
    async with aiohttp_session.post(PS_URL + "/task_execution" + PS_extension,
                                    data=pp_data, timeout=1200, headers=headers) as aiohttp_response:

And this is the custom server-side endpoint with FastAPI:

@app.post('/task_execution/{random_number}')
async def task_execution(request: Request, random_number):
                        headers = { 'Cache-Control': 'no-cache'  }

                    ...

                return Response(content= json_data, headers=headers)

Unfortunatelly, the response is not specific to the request, even if there is a random number. It's worth noting that the response takes a long time due to a lengthy Deep Learning generation process (30 seconds to 1 minute), so it's not a latency or performance issue.

To Reproduce

  1. Send 2 simultaneous requests with json data from 2 different devices
  2. Wait 30 seconds to 1 minute
  3. Both devices receive the first answer

Expected behavior

Every request should have its corresponding answer.

Logs/tracebacks

None.

Python Version

$ python --version
3.9

aiohttp Version

$ python -m pip show aiohttp
Version: 3.8.3

multidict Version

$ python -m pip show multidict
Version: 6.0.4

yarl Version

$ python -m pip show yarl
Version: 1.8.2

OS

Windows 11

Related component

Server, Client

Additional context

Google App Engine

Code of Conduct

Dreamsorcerer commented 1 year ago

You'll need to create a full reproducer, it seems incredibly unlikely that this is an issue with aiohttp, as it seems like it'd affect 1000s of users if that was really happening.

ulysses500 commented 1 year ago

You are right. I have noticed that I have made a mistake in FastAPI, in the main function that calls aiohttp:

@app.get('/get_data/{random_ref}')
   async def get_data(request: Request):

instead of:

@app.get('/get_data/{random_ref}')
    async def get_data(request: Request,random_ref):

It could have mistaken the main request and not the one from aiohttp.

ulysses500 commented 1 year ago

The problem still happens. Could be a problem from FastAPI.

Note: Not sure that many aiohttp users have long processes to deal with.

Dreamsorcerer commented 4 weeks ago

It still looks very unlikely to be an issue with aiohttp. I just can't imagine how 2 requests could resolve the same response, they have separate connections and are isolated objects. Unless you can reproduce this with an aiohttp server, I feel like this is an issue with FastAPI (or your code).