Skip to content

Use an ephemeral port in bm_asyncio_websockets - #517

Open
sendaoYan wants to merge 1 commit into
python:mainfrom
sendaoYan:fix-asyncio-websockets-port
Open

sendaoYan wants to merge 1 commit into
python:mainfrom
sendaoYan:fix-asyncio-websockets-port

Conversation

@sendaoYan

Copy link
Copy Markdown
Contributor

Use an ephemeral port in bm_asyncio_websockets instead of the fixed port 8001

Fixes #516

Summary

bm_asyncio_websockets/run_benchmark.py hard-codes port 8001 for both the
server and the client:

async with websockets.server.serve(handler, "", 8001):
    async with websockets.client.connect("ws://localhost:8001") as ws:

The default suite runs this benchmark only once, so the problem is not visible
in a normal sequential run. But it prevents two instances from running at the
same time (CI sharding, a custom parallel runner, or simply running the
benchmark twice), and it also fails if an unrelated process already owns port
8001:

OSError: [Errno 98] error while attempting to bind on address ('::', 8001, 0, 0):
[errno 98] address already in use

Change

Bind port 0 (let the kernel pick a free ephemeral port), read the assigned
port back from the listening socket, and use it for the client connection:

-    async with websockets.server.serve(handler, "", 8001):
-        async with websockets.client.connect("ws://localhost:8001") as ws:
+    async with websockets.server.serve(handler, "127.0.0.1", 0) as server:
+        port = server.sockets[0].getsockname()[1]
+        async with websockets.client.connect(f"ws://127.0.0.1:{port}") as ws:
             await asyncio.gather(*[send(ws) for _ in range(100)])

Why bind 127.0.0.1 instead of keeping the empty host?

Simply replacing 8001 with 0 while keeping host="" is not enough with
websockets==11.0.3. For an empty host it creates two sockets (AF_INET and
AF_INET6), and with port 0 they receive different ephemeral ports — for
example 39103 and 45903 in my testing. sockets[0] is then order-dependent and
the client can connect to the wrong address family/port. Binding 127.0.0.1
produces a single AF_INET socket and keeps the benchmark server on loopback,
which is all this benchmark needs.

Testing

Tested on Linux x86_64 with CPython 3.16.0a2+dev, pyperformance 1.14.0, and
websockets==11.0.3 (the version pinned by the benchmark). The change is not
version-specific.

  1. Two instances concurrently — the case that used to fail:
python -m pyperformance run -b asyncio_websockets -f -o /tmp/ws1.json &
python -m pyperformance run -b asyncio_websockets -f -o /tmp/ws2.json &
wait

Result: both complete successfully, no EADDRINUSE, and no listening socket is
left behind on port 8001:

asyncio_websockets: Mean +- std dev: 1.03 sec +- 0.01 sec
asyncio_websockets: Mean +- std dev: 1.03 sec +- 0.02 sec
  1. Standalone runs still work and produce the same kind of result as before.

The workload, transferred data size, and benchmark measurement are unchanged;
only the port selection changes.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

asyncio_websockets uses a fixed port (8001) and fails with EADDRINUSE when two instances run concurrently

1 participant