Skip to content

The agent that grows with you

fix(gateway): let a restarted gateway take the Webapp's port back at once

emozilla3686e9081a6a
diff --git a/gateway/run_api.py b/gateway/run_api.py
index a8f9e4156cd..fe2d242a6a4 100644
--- a/gateway/run_api.py
+++ b/gateway/run_api.py
@@ -15,6 +15,7 @@ process that already imported it mount it before the listener starts, exactly as
import asyncio
from dataclasses import dataclass
import logging
+import os
import socket
import sys
import types
@@ -24,6 +25,10 @@ logger = logging.getLogger(__name__)
# One listener per process: the dashboard routers share one process-local app.
_ACTIVE_RUNNER: Any = None
+# A fixed port (the Webapp's) has to bind again while the last gateway's closed connections sit in
+# 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"
@dataclass
@@ -151,6 +156,14 @@ class GatewayDashboardMount:
await web.app(scope, receive, send)
+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``)."""
+ listener = socket.socket(family, kind, proto)
+ if port and _REUSE_FIXED_PORT:
+ listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ return listener
+
+
async def start_gateway_api(runner, *, host: str = "127.0.0.1", port: int = 0, webapp=None) -> GatewayAPIHandle:
global _ACTIVE_RUNNER
from gateway.webapp_listener import dial_origin
@@ -175,7 +188,7 @@ async def start_gateway_api(runner, *, host: str = "127.0.0.1", port: int = 0, w
family, kind, proto, _, address = socket.getaddrinfo(
host, port, type=socket.SOCK_STREAM,
)[0]
- listener = socket.socket(family, kind, proto)
+ listener = _listener_socket(family, kind, proto, port)
_ACTIVE_RUNNER = runner
try:
listener.bind(address)
diff --git a/tests/gateway/test_webapp_listener.py b/tests/gateway/test_webapp_listener.py
index f33b94be559..22f0c2f5d58 100644
--- a/tests/gateway/test_webapp_listener.py
+++ b/tests/gateway/test_webapp_listener.py
@@ -245,3 +245,43 @@ def test_a_webapp_listener_that_cannot_start_still_leaves_native_clients_a_liste
def test_cancelling_the_gateway_while_the_webapp_listener_starts_cancels_it(monkeypatch, tmp_path):
with pytest.raises(asyncio.CancelledError):
_start_runtime_api(monkeypatch, tmp_path, "webapp:\n enabled: true\n", refuse=asyncio.CancelledError())
+
+
[email protected]("posix", [True, False])
+def test_only_posix_lets_the_webapps_fixed_port_bind_again_at_once(monkeypatch, posix):
+ # A restarted gateway must take the Webapp's port back while the old connections sit in
+ # TIME_WAIT. On Windows SO_REUSEADDR would let another process bind the port, so it stays off.
+ import socket
+
+ from gateway import run_api
+ monkeypatch.setattr(run_api, "_REUSE_FIXED_PORT", posix)
+ with run_api._listener_socket(socket.AF_INET, socket.SOCK_STREAM, 0, webapp.DEFAULT_PORT) as fixed, \
+ run_api._listener_socket(socket.AF_INET, socket.SOCK_STREAM, 0, 0) as ephemeral:
+ assert bool(fixed.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR)) is posix
+ assert not ephemeral.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) # main's private listener
+
+
+def test_the_fixed_port_switch_follows_the_platform():
+ from gateway import run_api
+ assert run_api._REUSE_FIXED_PORT is (os.name != "nt")
+
+
+def test_a_fixed_port_binds_again_while_the_last_gateways_connections_linger():
+ # The server closing first leaves its side of the connection in TIME_WAIT on the fixed port.
+ import socket
+
+ from gateway import run_api
+ with socket.socket() as probe:
+ probe.bind(("127.0.0.1", 0))
+ port = probe.getsockname()[1]
+ first = run_api._listener_socket(socket.AF_INET, socket.SOCK_STREAM, 0, port)
+ first.bind(("127.0.0.1", port))
+ first.listen()
+ client = socket.create_connection(("127.0.0.1", port))
+ served, _ = first.accept()
+ served.close()
+ client.recv(1)
+ client.close()
+ first.close()
+ with run_api._listener_socket(socket.AF_INET, socket.SOCK_STREAM, 0, port) as second:
+ second.bind(("127.0.0.1", port))