Skip to content

The agent that grows with you

fix(gateway): hold the Webapp's port exclusively on Windows

emozilla4e3c17ff83d4
diff --git a/gateway/run_api.py b/gateway/run_api.py
index 0498d1bd366..ea96dacb586 100644
--- a/gateway/run_api.py
+++ b/gateway/run_api.py
@@ -30,6 +30,9 @@ _ACTIVE_RUNNER: Any = None
# TIME_WAIT. POSIX allows that with SO_REUSEADDR; on Windows that option lets another process bind
# the same port, and a rebind there does not wait on TIME_WAIT anyway.
_REUSE_FIXED_PORT = os.name != "nt"
+# On Windows a fixed port is held for exclusive use instead: otherwise another process may bind it with
+# SO_REUSEADDR, and a more specific address (127.0.0.1 under a 0.0.0.0 bind) then takes its connections.
+_EXCLUSIVE_FIXED_PORT = os.name == "nt"
@dataclass
@@ -168,10 +171,13 @@ class GatewayDashboardMount:
def _listener_socket(family, kind, proto, port: int) -> socket.socket:
- """The listener's unbound socket; a fixed *port* may bind again at once (``_REUSE_FIXED_PORT``)."""
+ """The listener's unbound socket; a fixed *port* may bind again at once (``_REUSE_FIXED_PORT``)
+ or is held exclusively (``_EXCLUSIVE_FIXED_PORT``)."""
listener = socket.socket(family, kind, proto)
if port and _REUSE_FIXED_PORT:
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ elif port and _EXCLUSIVE_FIXED_PORT:
+ listener.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
return listener
diff --git a/tests/gateway/test_webapp_listener_fallback.py b/tests/gateway/test_webapp_listener_fallback.py
index 98b679c66c8..e1dfe4507e3 100644
--- a/tests/gateway/test_webapp_listener_fallback.py
+++ b/tests/gateway/test_webapp_listener_fallback.py
@@ -78,3 +78,33 @@ def test_the_webapp_port_accepts_connections_before_the_dashboard_mounts(tmp_pat
assert report["start_error"] is None, report.get("start_traceback")
assert report["connect_during_mount"] == "connected"
assert report["status"]["state"] == "serving"
+
+
[email protected](sys.platform != "win32", reason="SO_EXCLUSIVEADDRUSE is a Windows socket option")
+def test_on_windows_the_webapps_fixed_port_is_held_exclusively():
+ # Without exclusive use another process may bind the Webapp's port with SO_REUSEADDR (a more
+ # specific address then takes those connections). Main's private ephemeral listener is unchanged.
+ import socket
+
+ from gateway import run_api
+ with run_api._listener_socket(socket.AF_INET, socket.SOCK_STREAM, 0, _free_port()) as fixed, \
+ run_api._listener_socket(socket.AF_INET, socket.SOCK_STREAM, 0, 0) as ephemeral:
+ assert fixed.getsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE) == 1
+ assert ephemeral.getsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE) == 0
+ fixed.bind(("127.0.0.1", 0))
+ fixed.listen()
+ with socket.socket() as rival:
+ rival.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ with pytest.raises(OSError):
+ rival.bind(fixed.getsockname())
+
+
+def test_a_webapp_that_needs_sign_in_it_cannot_have_falls_back_to_the_private_listener(tmp_path):
+ # The Webapp's own URL needs a sign-in provider and none is registered: the Webapp is unavailable,
+ # and native clients keep main's private listener.
+ spec = {"webapp": {"enabled": True, "port": _free_port(), "public_url": "https://webapp.example.net"}}
+ report = _run(tmp_path, spec)
+ assert report["start_error"] is None, report.get("start_traceback")
+ assert report["native_session_list"] is True
+ assert report["status"]["state"] == "unavailable"
+ assert report["state"]["auth_required"] is False