Skip to content

The agent that grows with you

gateway: one raising startup-restore replay must not wedge the inbound gate

beardthelion3ea1124fa719
diff --git a/gateway/run_startup.py b/gateway/run_startup.py
index 28e8c6c5e9f..053d2c161ed 100644
--- a/gateway/run_startup.py
+++ b/gateway/run_startup.py
@@ -84,18 +84,25 @@ class GatewayStartupMixin:
queue = getattr(self, "_startup_restore_queue", None) or []
while queue:
event = queue.pop(0)
- source = getattr(event, "source", None)
- adapter = self._intake_adapter_for(source)
- if adapter is None:
- logger.debug(
- "Dropping startup-restore queued message: adapter unavailable for %s",
- getattr(getattr(source, "platform", None), "value", None),
- )
+ try:
+ source = getattr(event, "source", None)
+ adapter = self._intake_adapter_for(source)
+ if adapter is None:
+ logger.debug(
+ "Dropping startup-restore queued message: adapter unavailable for %s",
+ getattr(getattr(source, "platform", None), "value", None),
+ )
+ continue
+ # Mark the replay so _handle_message does not re-queue it while the restore gate is closed.
+ with suppress(Exception):
+ setattr(event, "_hermes_startup_restore_replay", True)
+ await adapter.handle_message(event)
+ except Exception:
+ # One bad replay must not abort the drain: the remaining queued
+ # events still deserve their turn, and a raise here used to skip
+ # the gate release in _finish_startup_restore entirely.
+ logger.warning("Startup-restore queued replay failed; continuing drain", exc_info=True)
continue
- # Mark the replay so _handle_message does not re-queue it while the restore gate is closed.
- with suppress(Exception):
- setattr(event, "_hermes_startup_restore_replay", True)
- await adapter.handle_message(event)
drained += 1
return drained
@@ -216,8 +223,14 @@ class GatewayStartupMixin:
self._startup_restore_tasks = []
# Warm the turn machinery BEFORE the queue drains: inbound turns must not build skeleton prompts.
await self._await_startup_warmup()
- drained = await self._drain_startup_restore_queue()
- self._startup_restore_in_progress = False
+ drained = 0
+ try:
+ drained = await self._drain_startup_restore_queue()
+ finally:
+ # The inbound gate must open even if the drain raises: a stuck
+ # _startup_restore_in_progress would queue every non-internal
+ # inbound forever (run_inbound.py reads this flag first).
+ self._startup_restore_in_progress = False
if drained:
logger.info("Drained %d inbound message(s) queued during startup restore", drained)
diff --git a/tests/gateway/test_restart_resume_pending.py b/tests/gateway/test_restart_resume_pending.py
index b3211780b62..777885563b4 100644
--- a/tests/gateway/test_restart_resume_pending.py
+++ b/tests/gateway/test_restart_resume_pending.py
@@ -766,6 +766,70 @@ async def test_startup_restore_waits_for_resume_before_draining_inbound():
assert runner._startup_restore_in_progress is False
+async def test_one_raising_replay_neither_wedges_gate_nor_eats_queue(monkeypatch):
+ """A replay whose adapter handle_message raises must not strand the inbound
+ gate closed, must not abort the drain, and must not lose the remaining queue."""
+ runner, adapter = make_restart_runner()
+ runner._startup_restore_in_progress = True
+ runner._startup_restore_queue = []
+ runner._startup_restore_tasks = []
+ monkeypatch.setenv("HERMES_STARTUP_WARMUP_TIMEOUT", "0")
+ runner._start_startup_warmup()
+
+ handled: list[str] = []
+
+ async def fake_handle_message(event: MessageEvent) -> None:
+ if event.source.chat_id == "bad-chat":
+ raise RuntimeError("adapter exploded mid-drain")
+ handled.append(event.text)
+
+ adapter.handle_message = fake_handle_message
+
+ bad_source = make_restart_source(chat_id="bad-chat")
+ good_source = make_restart_source(chat_id="good-chat")
+ runner._queue_startup_restore_event(
+ MessageEvent(text="first", message_type=MessageType.TEXT, source=bad_source))
+ runner._queue_startup_restore_event(
+ MessageEvent(text="second", message_type=MessageType.TEXT, source=good_source))
+
+ await asyncio.wait_for(runner._finish_startup_restore(), timeout=5)
+
+ assert runner._startup_restore_in_progress is False
+ assert handled == ["second"]
+ assert runner._startup_restore_queue == []
+
+
+async def test_post_drain_inbound_processes_instead_of_queueing(monkeypatch):
+ """After a contained replay failure the gate is open, so the next inbound
+ event dispatches instead of queueing into the (drained) restore queue."""
+ runner, adapter = make_restart_runner()
+ runner._startup_restore_in_progress = True
+ runner._startup_restore_queue = []
+ runner._startup_restore_tasks = []
+ monkeypatch.setenv("HERMES_STARTUP_WARMUP_TIMEOUT", "0")
+ runner._start_startup_warmup()
+
+ async def exploding_handle_message(event: MessageEvent) -> None:
+ raise RuntimeError("boom")
+
+ adapter.handle_message = exploding_handle_message
+ runner._queue_startup_restore_event(
+ MessageEvent(text="doomed", message_type=MessageType.TEXT,
+ source=make_restart_source(chat_id="bad-chat")))
+
+ await asyncio.wait_for(runner._finish_startup_restore(), timeout=5)
+ assert runner._startup_restore_in_progress is False
+
+ # The next inbound event reads the flag at run_inbound's gate; with the gate
+ # open it proceeds past the queueing branch rather than appending.
+ late = MessageEvent(text="late", message_type=MessageType.TEXT,
+ source=make_restart_source(chat_id="late-chat"))
+ await runner._handle_message(late)
+ assert runner._startup_restore_queue == []
+
+
# ---------------------------------------------------------------------------
# Fresh-boot turn-machinery warm-up gate (#99373)
# ---------------------------------------------------------------------------