Skip to content

The agent that grows with you

fix(whatsapp): don't treat the old bridge's TIME_WAIT as a busy port

unsupportedpastelsfb45657d551d
diff --git a/plugins/platforms/whatsapp/adapter.py b/plugins/platforms/whatsapp/adapter.py
index a7c940ea85b..293e47a0a45 100644
--- a/plugins/platforms/whatsapp/adapter.py
+++ b/plugins/platforms/whatsapp/adapter.py
@@ -787,9 +787,9 @@ class WhatsAppAdapter(WhatsAppBehaviorMixin, BasePlatformAdapter):
bridge's own ``listen``); False with a named fatal when something still holds it — spawning
onto a busy port only bought an EADDRINUSE crash-loop."""
import time
- from .bridge_ownership import port_is_free
+ from .bridge_ownership import port_accepts_listen
deadline = time.monotonic() + _BRIDGE_PORT_FREE_TIMEOUT_S
- while not port_is_free(self._bridge_port):
+ while not port_accepts_listen(self._bridge_port):
if time.monotonic() >= deadline:
holders: list = []
with suppress(Exception):
diff --git a/plugins/platforms/whatsapp/bridge_ownership.py b/plugins/platforms/whatsapp/bridge_ownership.py
index c3363582906..08fb51d79bf 100644
--- a/plugins/platforms/whatsapp/bridge_ownership.py
+++ b/plugins/platforms/whatsapp/bridge_ownership.py
@@ -33,6 +33,20 @@ def port_is_free(port: int) -> bool:
return True
+def port_accepts_listen(port: int) -> bool:
+ """Whether the bridge's own ``listen`` would succeed now. libuv sets SO_REUSEADDR on POSIX, so
+ a TIME_WAIT left by the old bridge's closed connections does not block it — only a live
+ listener does. A plain bind (``port_is_free``) refuses for up to the 60s TIME_WAIT window."""
+ with socket.socket() as sock:
+ if os.name != "nt": # on Windows SO_REUSEADDR allows binding over a live listener
+ sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ try:
+ sock.bind(("127.0.0.1", port))
+ except OSError:
+ return False
+ return True
+
+
def _read_port_record(record: Path) -> int:
try:
return int(record.read_text(encoding="utf-8-sig").strip())
diff --git a/tests/gateway/test_whatsapp_bridge_port_release.py b/tests/gateway/test_whatsapp_bridge_port_release.py
index 361ac309ca0..f24a72981f2 100644
--- a/tests/gateway/test_whatsapp_bridge_port_release.py
+++ b/tests/gateway/test_whatsapp_bridge_port_release.py
@@ -264,6 +264,34 @@ class TestPortReleasedBeforeSpawn:
class TestListenerDiscovery:
+ @linux
+ @pytest.mark.asyncio
+ async def test_time_wait_from_old_bridge_does_not_block_respawn(self):
+ """The old bridge closed its keep-alive connections first, so its port sits in TIME_WAIT
+ (up to 60s) with no listener. node's listen (SO_REUSEADDR) succeeds; the wait must too."""
+ from plugins.platforms.whatsapp.adapter import WhatsAppAdapter
+
+ srv = socket.socket()
+ srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # as libuv does for the bridge
+ srv.bind(("127.0.0.1", 0))
+ srv.listen(1)
+ port = srv.getsockname()[1]
+ cli = socket.create_connection(("127.0.0.1", port))
+ conn, _ = srv.accept()
+ conn.close() # server side closes first -> TIME_WAIT on 127.0.0.1:<port>
+ srv.close()
+ time.sleep(0.1)
+ cli.close()
+
+ adapter = WhatsAppAdapter.__new__(WhatsAppAdapter)
+ adapter.platform = Platform.WHATSAPP
+ adapter._bridge_port = port
+ adapter._fatal_error_code = adapter._fatal_error_message = None
+ adapter._fatal_error_retryable = True
+ adapter._fatal_error_handler = None
+ assert await adapter._wait_bridge_port_free() is True
+ assert adapter._fatal_error_code is None
+
@linux
def test_psutil_fallback_when_lsof_and_ss_missing(self, monkeypatch):
"""Neither lsof nor ss on PATH (the repo image ships neither): the psutil TCP scan still