Skip to content

Commit

Permalink
Fix dangling socket causing uvicorn shutdown failure
Browse files Browse the repository at this point in the history
When using urllib3<=1.26.18 with uvicorn<=0.23.2 and python>=3.12,
the socket shutdown method of urllib3 combines with the uvicorn
server shutdown to leave the server stuck for many seconds:

encode/uvicorn#2145

Updating urllib3 or uvicorn (or downgrading Python) fixes the bug,
but Fedora 39 shipped Python 3.12 with old urllib3 and uvicorn.

Since we (sadly) open a connection just for a single request every
time, we can disable the HTTP keep-alive. This allows the server to
close the connection earlier, without waiting for the client.

This fixes the failing tests/system/test_astacus.py::test_astacus on
Fedora 39.
  • Loading branch information
kmichel-aiven committed Apr 11, 2024
1 parent ad5ee5a commit 68bf182
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions astacus/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ def http_request(url, *, caller, method="get", timeout=10, ignore_status_code: b
# using passwords in urls here.
logger.info("request %s %s by %s", method, url, caller)
try:
# There is a combination of bugs between urllib3<2 and uvicorn<0.24.0
# where the abrupt socket shutdown of urllib3 causes the uvicorn server
# to wait for a long time when shutting down, only on Python>=3.12.
# Since we don't use multi-requests sessions, disable Keep-Alive to
# allow the server to shut down the connection on its side as soon as
# it is done replying.
kw["headers"] = {"Connection": "close", **kw.get("headers", {})}
r = requests.request(method, url, timeout=timeout, **kw)
if r.ok:
return r.json()
Expand Down

0 comments on commit 68bf182

Please sign in to comment.