Skip to content

The agent that grows with you

fix(whatsapp): keep main's TIME-WAIT-aware port_is_free for the respawn wait

Teknium7b90783f7629
diff --git a/plugins/platforms/whatsapp/adapter.py b/plugins/platforms/whatsapp/adapter.py
index 293e47a0a45..9afe3716614 100644
--- a/plugins/platforms/whatsapp/adapter.py
+++ b/plugins/platforms/whatsapp/adapter.py
@@ -783,13 +783,13 @@ class WhatsAppAdapter(WhatsAppBehaviorMixin, BasePlatformAdapter):
self._bridge_log_fh = None
async def _wait_bridge_port_free(self) -> bool:
- """Bounded wait for the just-stopped bridge to release its port (a real bind probe, like the
- 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."""
+ """Bounded wait for the just-stopped bridge to release its port (``port_is_free`` probes with the
+ bridge's own SO_REUSEADDR bind semantics, so TIME-WAIT alone never blocks); False with a named fatal
+ when a live listener still holds it — spawning onto a busy port only bought an EADDRINUSE crash-loop."""
import time
- from .bridge_ownership import port_accepts_listen
+ from .bridge_ownership import port_is_free
deadline = time.monotonic() + _BRIDGE_PORT_FREE_TIMEOUT_S
- while not port_accepts_listen(self._bridge_port):
+ while not port_is_free(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 08fb51d79bf..4f816295a31 100644
--- a/plugins/platforms/whatsapp/bridge_ownership.py
+++ b/plugins/platforms/whatsapp/bridge_ownership.py
@@ -16,8 +16,9 @@ def port_is_free(port: int) -> bool:
Contract: this answers for the bridge's own bind semantics, not for an arbitrary binder. On
POSIX the bridge (Express/libuv) sets SO_REUSEADDR, so a port holding only TIME-WAIT entries
reads as free here even though a binder without SO_REUSEADDR would get EADDRINUSE; a live
- listener still reads as busy. Its only callers are this module's bridge-port allocation and
- ownership checks. Do not reuse it to gate a process that binds without SO_REUSEADDR.
+ listener still reads as busy. Its callers are this module's bridge-port allocation and
+ ownership checks and the adapter's pre-spawn wait for the old bridge to release its port.
+ Do not reuse it to gate a process that binds without SO_REUSEADDR.
"""
with socket.socket() as sock:
# A just-stopped bridge can leave its accepted TCP connections in TIME-WAIT. The Node
@@ -33,20 +34,6 @@ 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())