Possible race condition leading to a connection reset if worker is gracefully terminating #2315
Unanswered
rbagd
asked this question in
Potential Issue
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We have encountered a relatively rare connection error which is probably due to a race condition as
uvicorn
worker is trying to shutdown.Here is the setup
uvicorn
workergunicorn
with--max-requests
for regularly restarting workersI can reproduce it with Python 3.11, both
uvloop
andasyncio
, but couldn't reproduce withasyncio
and Python 3.12.To reproduce I launch below app as
Then I stress test the application with many concurrent users constantly hitting the API.
After some waiting I eventually hit
Connection reset by peer
errors.I did some initial investigation into what is happening. Here's a
tcpdump
for one of these errors which I tried to correlate with some events in the code. It always happens around the time whenmax-requests
is reached and worker is shutting down.It seems that in certain cases worker doesn't shutdown gracefully despite data having just arrived in TCP stack.
After some deep-dive I noticed that every time that the error happens it is true that
self.cycle=None
inHttpToolsProtocol.shutdown()
whenever the error is triggered, and if I am correct the reverse is true as well. It seems to me that adding a blockinto
httptools_impl.py
orh11_impl.py
solves the issue but I am not really sure what this means.Beta Was this translation helpful? Give feedback.
All reactions