Skip to content

The agent that grows with you

fix(whatsapp): run the bridge reaps off the gateway event loop

Tekniumf3acd8ff331c
diff --git a/plugins/platforms/whatsapp/adapter.py b/plugins/platforms/whatsapp/adapter.py
index 9afe3716614..86ec7b8e78e 100644
--- a/plugins/platforms/whatsapp/adapter.py
+++ b/plugins/platforms/whatsapp/adapter.py
@@ -723,8 +723,10 @@ class WhatsAppAdapter(WhatsAppBehaviorMixin, BasePlatformAdapter):
retryable=False,
)
return False
+ # The reaps below block for up to their SIGTERM grace each; off the loop so every other
+ # platform on this gateway keeps running while a slow bridge exits.
if not secondary or prior_bridge_is_ours:
- _kill_stale_bridge_by_pidfile(self._session_path)
+ await asyncio.to_thread(_kill_stale_bridge_by_pidfile, self._session_path)
if not secondary:
if self._bridge_probe_timed_out:
# No /health answer is no ownership evidence (another profile's busy bridge looks the same), and
@@ -744,7 +746,7 @@ class WhatsAppAdapter(WhatsAppBehaviorMixin, BasePlatformAdapter):
retryable=True,
)
return False
- _kill_port_process(self._bridge_port)
+ await asyncio.to_thread(_kill_port_process, self._bridge_port)
# The kills above return after SIGTERM (or SIGKILL past the grace); a slow-exiting bridge could still
# hold the port, and the blind sleep that used to sit here spawned onto it — EADDRINUSE, retried forever.
# A secondary that skipped the kills found the port free, so this returns at once on that path.
diff --git a/tests/gateway/test_whatsapp_bridge_port_release.py b/tests/gateway/test_whatsapp_bridge_port_release.py
index a9bf07958a7..36daa9167ec 100644
--- a/tests/gateway/test_whatsapp_bridge_port_release.py
+++ b/tests/gateway/test_whatsapp_bridge_port_release.py
@@ -13,6 +13,7 @@ spawned bridge (``Popen``/node) and the health probe are mocked.
import asyncio
import contextlib
+import itertools
import os
import signal
import socket
@@ -221,11 +222,22 @@ class TestPortReleasedBeforeSpawn:
proc.pid = 4190000 # a PID that is not alive
return proc
+ # The gateway loop serves every platform: the SIGTERM grace must not freeze it.
+ loop = asyncio.get_running_loop()
+ ticks, handle = [], {}
+
+ def _tick():
+ ticks.append(time.monotonic())
+ handle["next"] = loop.call_later(0.05, _tick)
+
+ _tick()
with patch.object(whatsapp_adapter, "_kill_stale_bridge_by_pidfile", wraps=whatsapp_adapter._kill_stale_bridge_by_pidfile):
patches = _connect_patches(adapter, _record_spawn)
with patches[0], patches[1], patches[2], patches[3], patches[4]:
await adapter.connect()
+ handle["next"].cancel()
+ assert max(b - a for a, b in itertools.pairwise(ticks)) < 0.5, "connect() blocked the event loop"
assert adapter._fatal_error_code != "whatsapp_bridge_port_busy"
assert spawn_state.get("spawns") == 1, "the new bridge was not spawned exactly once"
assert spawn_state["port_free_at_spawn"] is True, "spawned while the port was still held"
@@ -294,6 +306,9 @@ class TestListenerDiscovery:
assert adapter._fatal_error_code is None
@linux
+ # psutil's system-wide scan readlinks /proc/<pid>/exe of every process, which names files under
+ # the real Hermes home whenever a Hermes process runs on the box; it reads, never writes.
+ @pytest.mark.allow_real_home_io
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
finds the listener, so the port holder can be identified and reaped."""