Skip to content

The agent that grows with you

fix(whatsapp): don't read a signal-raced bridge exit as a fatal crash

liuhao1024f5a3cd6bd8da
diff --git a/plugins/platforms/whatsapp/adapter.py b/plugins/platforms/whatsapp/adapter.py
index 424bb87c777..2b7d95924b8 100644
--- a/plugins/platforms/whatsapp/adapter.py
+++ b/plugins/platforms/whatsapp/adapter.py
@@ -565,7 +565,14 @@ class WhatsAppAdapter(WhatsAppBehaviorMixin, BasePlatformAdapter):
if returncode is None:
return None
# getattr-with-default: tests build the adapter via ``__new__`` without __init__.
- if getattr(self, "_shutting_down", False) and returncode in {0, -2, -15}:
+ # A supervisor's stop signal reaches the bridge child (same process group) before the
+ # gateway's stop flow reaches this adapter's disconnect(), so ``_shutting_down`` alone
+ # misses that window and a normal -15/-2 exit reads as a fatal crash (#127047). The
+ # runner flips ``_stop_requested_by_signal`` in its signal handler — the first thing
+ # that runs on any signal-driven stop — so consult it too. Un-guarded it stays fatal:
+ # a bridge killed while the gateway keeps running still needs the reconnect watcher.
+ runner_stop = getattr(getattr(self, "gateway_runner", None), "_stop_requested_by_signal", False)
+ if (getattr(self, "_shutting_down", False) or runner_stop) and returncode in {0, -2, -15}:
logger.info("[%s] Bridge exited during shutdown (code %d).", self.name, returncode)
return None
message = f"WhatsApp bridge process exited unexpectedly (code {returncode})."
diff --git a/tests/gateway/test_whatsapp_connect.py b/tests/gateway/test_whatsapp_connect.py
index 2c58c1aa2cd..86f9454fd94 100644
--- a/tests/gateway/test_whatsapp_connect.py
+++ b/tests/gateway/test_whatsapp_connect.py
@@ -208,6 +208,52 @@ class TestBridgeRuntimeFailure:
mock_fh.close.assert_called_once()
assert adapter._bridge_log_fh is None
+ @pytest.mark.asyncio
+ @pytest.mark.parametrize("returncode", [-15, -2], ids=["sigterm", "sigint"])
+ async def test_signal_exit_during_gateway_signal_shutdown_is_not_fatal(self, returncode):
+ """A -15/-2 bridge exit races the stop flow: the signal handler flags the runner
+ long before disconnect() flips ``_shutting_down`` (#127047)."""
+ from types import SimpleNamespace
+
+ adapter = _make_adapter()
+ fatal_handler = AsyncMock()
+ adapter.set_fatal_error_handler(fatal_handler)
+ adapter._running = True
+ adapter._shutting_down = False # disconnect() has NOT run yet — this is the race
+ adapter.gateway_runner = SimpleNamespace(_stop_requested_by_signal=True)
+ adapter._bridge_log_fh = MagicMock()
+
+ mock_proc = MagicMock()
+ mock_proc.poll.return_value = returncode
+ adapter._bridge_process = mock_proc
+
+ assert await adapter._check_managed_bridge_exit() is None
+ assert adapter.fatal_error_code is None
+ fatal_handler.assert_not_awaited()
+
+ @pytest.mark.asyncio
+ async def test_sigterm_exit_without_gateway_shutdown_stays_fatal(self):
+ """The runner flag must not mask a genuine crash: a bridge SIGTERMed while the
+ gateway keeps running still queues the retryable reconnect."""
+ from types import SimpleNamespace
+
+ adapter = _make_adapter()
+ fatal_handler = AsyncMock()
+ adapter.set_fatal_error_handler(fatal_handler)
+ adapter._running = True
+ adapter._shutting_down = False
+ adapter.gateway_runner = SimpleNamespace(_stop_requested_by_signal=False)
+ adapter._bridge_log_fh = MagicMock()
+
+ mock_proc = MagicMock()
+ mock_proc.poll.return_value = -15
+ adapter._bridge_process = mock_proc
+
+ assert await adapter._check_managed_bridge_exit() is not None
+ assert adapter.fatal_error_code == "whatsapp_bridge_exited"
+ assert adapter.fatal_error_retryable is True
+ fatal_handler.assert_awaited_once()
+
@pytest.mark.asyncio
async def test_send_normalizes_bare_phone_numbers_to_jid(self):
"""A bare phone target (with or without +) becomes a full JID.