Skip to content

Use an ephemeral port in bm_asyncio_tcp - #515

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

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

Conversation

@sendaoYan

Copy link
Copy Markdown
Contributor

Use an ephemeral port in bm_asyncio_tcp instead of the fixed port 8882

Fixes #514

Summary

asyncio_tcp and asyncio_tcp_ssl both run
pyperformance/data-files/benchmarks/bm_asyncio_tcp/run_benchmark.py, which
hard-codes 127.0.0.1:8882. Because bm_asyncio_tcp_ssl.toml selects the SSL
variant with extra_opts = ["--ssl"], the two benchmarks use the same server
script and the same port, so one of them dies with EADDRINUSE when they run
concurrently:

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

This also makes the benchmark fail whenever an unrelated process is listening on
port 8882.

Change

Bind port 0 (let the kernel pick a free ephemeral port) and read the assigned
port back from the listening socket. The client then connects to that port.

-    server = await asyncio.start_server(handle_echo, '127.0.0.1', 8882, ssl=server_context)
+    # Bind to an ephemeral port (port 0) and read back the port the kernel
+    # assigned.  This keeps concurrent runs (e.g. asyncio_tcp and
+    # asyncio_tcp_ssl running in parallel) from colliding on a fixed port.
+    server = await asyncio.start_server(handle_echo, '127.0.0.1', 0, ssl=server_context)
+    host, port = server.sockets[0].getsockname()[:2]
 
     async with server:
         asyncio.create_task(server.start_serving())
-        reader, writer = await asyncio.open_connection('127.0.0.1', 8882, ssl=client_context)
+        reader, writer = await asyncio.open_connection(host, port, ssl=client_context)

The change covers both the plain and the SSL code path because both are handled
by the same main() function.

Why not probe for a free port first?

A common alternative is to create a temporary socket, bind it to port 0, read
the port, close it, and then bind the real server to that port. That has a
TOCTOU race: another process can take the port between the close and the second
bind. Binding port 0 on the real server socket avoids the race entirely — the
listening socket owns the port, and getsockname() reports what the kernel
assigned.

bm_tornado_http already uses the same idea (bind_sockets(0, ...) plus
getsockname()).

Testing

Tested on Linux x86_64 with CPython 3.16.0a2+dev and pyperformance 1.14.0.
The change is not version-specific.

  1. Concurrent run of the two variants (the case that used to fail):
python -m pyperformance run -b asyncio_tcp     -f -o /tmp/asyncio_tcp.json &
python -m pyperformance run -b asyncio_tcp_ssl -f -o /tmp/asyncio_tcp_ssl.json &
wait

Result: both complete successfully, no EADDRINUSE, and no listening socket is
left behind on 8882 after the run:

asyncio_tcp:     Mean +- std dev: 700 ms +- 39 ms
asyncio_tcp_ssl: Mean +- std dev: 2.15 sec +- 0.06 sec
  1. Standalone runs still work:
python -m pyperformance run -b asyncio_tcp_ssl -f -o /tmp/asyncio_tcp_ssl.json
# asyncio_tcp_ssl: Mean +- std dev: 2.21 sec +- 0.07 sec
  1. Whole suite in parallel: I ran all 100 default sub-items concurrently, one
    pyperformance run process per benchmark, each pinned to one CPU core. 99
    passed; the only failure was dask, which is unrelated to this change
    (cloudpickle 3.1.2 looks up the removed DELETE_GLOBAL opcode on Python
    3.16). No port-conflict errors appeared in any log, and ports 8001/8882 were
    free after the run.

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_tcp and asyncio_tcp_ssl share a fixed port (8882) and fail with EADDRINUSE when run concurrently

1 participant