Skip to content

bm_fastapi fails with "503 Service Unavailable" when a proxy is configured #504

Open

Description

@sendaoYan

Summary

The fastapi benchmark (bm_fastapi) fails when the environment has an HTTP
proxy configured — e.g. HTTP_PROXY/http_proxy set, which is common in CI or
corporate environments — and 127.0.0.1 is not listed in NO_PROXY.

Environment

  • pyperformance 1.14.0
  • Python 3.10
  • fastapi==0.121.0, httpx==0.28.1, uvicorn==0.38.0
  • A proxy is configured via HTTP_PROXY/http_proxy, and NO_PROXY does not
    contain 127.0.0.1/localhost.

How to reproduce

Run the benchmark while inheriting the proxy environment variables:

$ python -m pyperformance run bm_fastapi --inherit-environ http_proxy,https_proxy,no_proxy

Actual result

The benchmark fails ("Benchmark died"):

[27/80] fastapi...
# .../bm_fastapi/run_benchmark.py --inherit-environ https_proxy,http_proxy,PYPERFORMANCE_RUNID,no_proxy ...
Traceback (most recent call last):
  ...
  File ".../bm_fastapi/run_benchmark.py", line 76, in run_benchmark
    response.raise_for_status()
  ...
httpx.HTTPStatusError: Server error '503 Service Unavailable' for url 'http://127.0.0.1:40027/items/0'
...
RuntimeError: Benchmark died
ERROR: Benchmark fastapi failed: Benchmark died

Root cause

bm_fastapi/run_benchmark.py starts a uvicorn server on 127.0.0.1 and then
talks to it with httpx.AsyncClient(). httpx reads HTTP_PROXY/HTTPS_PROXY/
NO_PROXY from the environment by default (trust_env=True), so the request to
http://127.0.0.1:<port>/items/0 is sent through the proxy instead of directly
to the loopback server. The proxy cannot reach the benchmark machine's loopback
address, so it returns 503 Service Unavailable.

Expected behavior

The benchmark should connect directly to its own 127.0.0.1 server and complete
normally, regardless of the proxy configuration in the environment.

Suggested fix

Create the client with trust_env=False, since the benchmark only ever talks to
its own loopback server:

async with httpx.AsyncClient(trust_env=False) as client:

Scope

Only bm_fastapi is affected. The other networking benchmarks
(bm_tornado_http, bm_asyncio_tcp, bm_asyncio_websockets, bm_async_tree)
use clients that do not read proxy environment variables.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions