The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(whatsapp): POSIX-only reuse in the probe, real TIME-WAIT test
Teknium1848982075f4
diff --git a/plugins/platforms/whatsapp/bridge_ownership.py b/plugins/platforms/whatsapp/bridge_ownership.pyindex 484ce962316..f8f694fb1e6 100644--- a/plugins/platforms/whatsapp/bridge_ownership.py+++ b/plugins/platforms/whatsapp/bridge_ownership.py@@ -1,4 +1,5 @@"""Non-destructive bridge allocation for multiplexed secondary profiles."""+import osimport socketfrom contextlib import suppressfrom pathlib import Path@@ -13,7 +14,10 @@ def port_is_free(port: int) -> bool:with socket.socket() as sock:# A just-stopped bridge can leave its accepted TCP connections in TIME-WAIT. The Node# bridge enables this option too, so match its bind semantics while still rejecting a listener.- sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)+ # POSIX only, like libuv: on Windows SO_REUSEADDR lets a bind share a live listener's port,+ # which would read a foreign bridge as free (Windows binds past TIME-WAIT without it).+ if os.name != "nt":+ sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)try:sock.bind(("127.0.0.1", port))except OSError:diff --git a/tests/gateway/test_whatsapp_multiplex.py b/tests/gateway/test_whatsapp_multiplex.pyindex 701fa9a5037..777f31468d5 100644--- a/tests/gateway/test_whatsapp_multiplex.py+++ b/tests/gateway/test_whatsapp_multiplex.py@@ -91,6 +91,7 @@ def _hold_port():return sock, sock.getsockname()[1][email protected](os.name == "nt", reason="SO_REUSEADDR shares live listeners on Windows; probe skips it there")def test_port_probe_reuses_time_wait_addresses(monkeypatch):"""A secondary restart must not mistake the prior bridge's TCP TIME-WAIT for a listener (#139425)."""import plugins.platforms.whatsapp.bridge_ownership as ownership@@ -119,6 +120,37 @@ def test_port_probe_reuses_time_wait_addresses(monkeypatch):][email protected](os.name == "nt", reason="POSIX TIME-WAIT bind semantics")+def test_time_wait_leftovers_are_not_a_foreign_listener(tmp_path):+ """Real kernel TIME-WAIT, no mocks: the stopped bridge closed its accepted connection first, so+ the port carries a TIME-WAIT entry and no listener. The ownership check must call it free (#139425),+ while a real listener on the same port still fails closed."""+ from plugins.platforms.whatsapp.bridge_ownership import check_secondary_ownership, port_is_free+ listener = socket.socket()+ listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)+ listener.bind(("127.0.0.1", 0))+ listener.listen()+ port = listener.getsockname()[1]+ client = socket.create_connection(("127.0.0.1", port))+ accepted, _ = listener.accept()+ accepted.close() # server side closes first -> TIME-WAIT lands on the bridge port+ client.recv(1)+ client.close()+ listener.close()+ session = tmp_path / "session"+ session.mkdir()+ assert port_is_free(port)+ assert check_secondary_ownership(session, port) == "free"+ sock = socket.socket()+ sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)+ sock.bind(("127.0.0.1", port))+ sock.listen()+ with sock:+ assert not port_is_free(port)+ with pytest.raises(ValueError, match="does not own"):+ check_secondary_ownership(session, port)++def test_allocation_skips_ports_recorded_by_siblings(tmp_path):"""A sibling whose bridge is down leaves its recorded port unbound; a new profile must not take it,or the sibling hits a non-retryable port conflict on its next start. A corrupt record names itself."""