From c0ce7473bd1bdac7cdcf38369ec82ac32e75efc1 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Mon, 24 Aug 2026 06:01:51 -0700 Subject: [PATCH] fix(serve): Windows conflict probe uses SO_EXCLUSIVEADDRUSE (SO_REUSEADDR binds over live listeners on WinSock) --- hermes_cli/web_server.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.py index 5f486493c3..201d37f197 100644 --- a/hermes_cli/web_server.py +++ b/hermes_cli/web_server.py @@ -19301,10 +19301,22 @@ def _port_bind_conflict(host: str, port: int) -> bool: except OSError: return False try: - # Match uvicorn's bind flags (uvicorn/config.py bind_socket) so the - # probe conflicts exactly when uvicorn's own bind would: SO_REUSEADDR - # lets TIME_WAIT remnants pass while a live LISTEN socket still fails. - probe.setsockopt(_socket.SOL_SOCKET, _socket.SO_REUSEADDR, 1) + import sys as _sys_mod + + _exclusive = getattr(_socket, "SO_EXCLUSIVEADDRUSE", None) + if _sys_mod.platform == "win32" and _exclusive is not None: + # Windows: SO_REUSEADDR means "bind over anyone" — a probe (or + # uvicorn bind) with it SUCCEEDS on top of a live LISTEN socket, + # so it can never detect a conflict. SO_EXCLUSIVEADDRUSE makes + # the probe fail with WSAEADDRINUSE exactly when another socket + # holds the port (the reporter's 10048 shape in #93608). + probe.setsockopt(_socket.SOL_SOCKET, _exclusive, 1) + else: + # POSIX: match uvicorn's bind flags (uvicorn/config.py + # bind_socket) so the probe conflicts exactly when uvicorn's own + # bind would: SO_REUSEADDR lets TIME_WAIT remnants pass while a + # live LISTEN socket still fails. + probe.setsockopt(_socket.SOL_SOCKET, _socket.SO_REUSEADDR, 1) probe.bind((host, port)) except OSError as exc: return _is_addr_in_use_error(exc)