Closed zanieb closed 7 months ago
With a more sophisticated testing approach that doesn't interleave HEAD
requests with others, you can see that HEAD
is the cause of this
import httpx
import anyio
from collections import defaultdict
ATTEMPTS = 2000
TARGET = "http://localhost:8000"
METHODS = ["HEAD", "GET", "POST"]
RESULTS = defaultdict(list)
async def query(client: httpx.AsyncClient, method: str, url: str):
try:
response = await getattr(client, method.lower())(url)
response.raise_for_status()
except Exception as exc:
RESULTS[method].append(exc)
print(".", end="")
async def main():
for method in METHODS:
async with httpx.AsyncClient(timeout=httpx.Timeout(timeout=300)) as client:
async with anyio.create_task_group() as tg:
for _ in range(ATTEMPTS):
tg.start_soon(query, client, method, TARGET)
anyio.run(main)
# Report
print()
for method in METHODS:
failures = RESULTS[method]
seen = set()
errors = []
for exc in failures:
if type(exc) in seen:
continue
seen.add(type(exc))
errors.append(exc)
print(f"{len(failures)}/{ATTEMPTS} {method} requests failed with {len(seen)} unique exception types")
for exc in errors:
print(f" - {type(exc).__name__} {exc}")
255/2000 HEAD requests failed with 2 unique exception types
- ReadError
- RemoteProtocolError Server disconnected without sending a response.
0/2000 GET requests failed with 0 unique exception types
0/2000 POST requests failed with 0 unique exception types
I've merged one of the two tickets related to this. The other one needs some smaller minor changes, working on those.
Thank you!
Using
waitress==2.1.2
andpyramid==2.0.2
I've discovered that ~1% of requests fail due to broken connections when there are many concurrent requests.Requests fail with a
BrokenResourceError
orReadError
orRemoteProtocolError: Server disconnected without sending a response.
orConnection reset by peer
etc.The server looks like:
If I increase the threads, nothing changes. If I use another WSGI server like
gunicorn
, there are no failed requests.I'm testing concurrent request success rates with following script:
I'm testing on macOS with Python 3.11.4.
I discovered this while working with
devpi
, see https://github.com/devpi/devpi/issues/1022.