Skip to content

The agent that grows with you

fix(gateway): claim ledger rows and clear resume_pending inline before the abandonable boot-send task

Kshitij Kapoor684e95a0012b
diff --git a/gateway/run.py b/gateway/run.py
index 50b2925d712..5070e47b56c 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -11920,12 +11920,27 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
startup-restore gate. Logs a late failure that would otherwise be
swallowed once the task is discarded from ``_background_tasks``.
Cancellation is expected (shutdown) and is not an error."""
+ GatewayRunner._log_late_background_failure(
+ task,
+ "background startup auto-resume task failed after gate release",
+ level=logging.DEBUG,
+ )
+
+ @staticmethod
+ def _log_late_background_failure(
+ task: "asyncio.Task", message: str, *, level: int = logging.WARNING
+ ) -> None:
+ """Shared done-callback body for boot-path tasks that outlive the
+ startup-restore gate: surface a late failure that would otherwise be
+ swallowed once the task is discarded from ``_background_tasks``.
+ Cancellation is expected (shutdown) and is not an error."""
if task.cancelled():
return
exc = task.exception()
if exc is not None:
- logger.debug(
- "background startup auto-resume task failed after gate release",
+ logger.log(
+ level,
+ message,
exc_info=(type(exc), exc, exc.__traceback__),
)
@@ -11945,7 +11960,18 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
This uses the same bounded ``asyncio.wait`` the resume gate already
uses: on timeout we return and let the sends finish in the
background. Tasks are not cancelled.
+
+ The ledger claim + ``resume_pending`` clear happen INLINE here,
+ before the send task exists: they are pure DB work (no network,
+ bounded by claimed-row count), and deferring them into the send
+ task left a window where a hung restart notification ahead of the
+ redelivery step let the gate expire with zero rows claimed — the
+ resume scheduler then replayed turns whose answers were already in
+ the ledger, and the background task later redelivered them too
+ (duplicate delivery + re-paid turn).
"""
+ claimed = await self._claim_pending_obligations()
+
async def _boot_sends() -> None:
await self._send_restart_notification()
if planned_restart_notification_pending:
@@ -11955,7 +11981,7 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
)
finally:
_clear_planned_restart_notification()
- await self._redeliver_pending_obligations()
+ await self._redeliver_claimed_obligations(claimed)
boot_task = asyncio.create_task(_boot_sends())
timeout = _startup_restore_drain_timeout_secs()
@@ -11982,43 +12008,35 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
@staticmethod
def _log_background_boot_send_result(task: "asyncio.Task") -> None:
"""Done-callback for boot-path sends that outlived the restore gate."""
- if task.cancelled():
- return
- exc = task.exception()
- if exc is not None:
- logger.warning(
- "background boot-path send failed after gate release: %s",
- exc,
- exc_info=(type(exc), exc, exc.__traceback__),
- )
+ GatewayRunner._log_late_background_failure(
+ task, "background boot-path send failed after gate release: see traceback"
+ )
- async def _redeliver_pending_obligations(self) -> int:
- """Redeliver final responses recorded in the delivery ledger by a
- previous (now dead) gateway process.
+ async def _claim_pending_obligations(self) -> list:
+ """Claim recoverable delivery-ledger rows and clear their
+ ``resume_pending`` flags. Pure DB work — no network sends.
- Runs at startup BEFORE ``_schedule_resume_pending_sessions``. A
+ Runs INLINE at startup BEFORE ``_schedule_resume_pending_sessions``
+ and before the (bounded, abandonable) boot-send task exists. A
session with a recoverable obligation already produced its answer —
- the turn completed and only delivery is owed — so this method sends
- the stored text and clears ``resume_pending`` for that session,
- preventing the resume path from re-running (and re-paying for) a
- turn whose output we hold.
+ the turn completed and only delivery is owed — so clearing
+ ``resume_pending`` here prevents the resume path from re-running
+ (and re-paying for) a turn whose output we hold, regardless of how
+ long the sends ahead of redelivery take (#91969).
Crash-ambiguity contract (see gateway/delivery_ledger.py):
rows that were mid-send or previously rejected carry a visible
recovered-reply marker so a possible duplicate is labeled, never
- silent. Returns the number of redeliveries attempted.
+ silent. Returns the claimed rows for redelivery.
"""
try:
from gateway.delivery_ledger import (
- RECOVERED_MARKER,
ledger_enabled,
- mark_delivered,
- mark_failed,
sweep_recoverable,
)
if not await asyncio.to_thread(ledger_enabled):
- return 0
+ return []
# Only claim rows we can actually send this boot: self.adapters
# holds a platform only after its connect() succeeded, and each
# claim spends one of the row's three redelivery attempts.
@@ -12030,17 +12048,14 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
)
except Exception:
logger.debug("delivery ledger sweep failed", exc_info=True)
- return 0
+ return []
if not claimed:
- return 0
+ return []
# Clear resume_pending for EVERY claimed row up front, before any
# send. Claiming already spent one of the row's redelivery attempts —
# the answer is in the ledger, so the resume path must never re-run
- # these turns. Doing this per-row as the loop reaches each send left
- # a window: when a slow/flood-limited send held the loop past the
- # inbound-gate timeout, _schedule_resume_pending_sessions could
- # replay turns for rows the loop had not reached yet (#91969).
+ # these turns (#91969).
for row in claimed:
session_key = row.get("session_key") or ""
if not session_key:
@@ -12052,6 +12067,27 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
"clear_resume_pending failed for %s", session_key,
exc_info=True,
)
+ return claimed
+
+ async def _redeliver_claimed_obligations(self, claimed: list) -> int:
+ """Redeliver final responses for rows already claimed (and
+ resume-cleared) by :meth:`_claim_pending_obligations`.
+
+ Network half of the split — runs inside the bounded boot-send task,
+ so a flood-limited send can be abandoned by the restore gate without
+ reopening the turn-replay window. Returns redeliveries attempted.
+ """
+ if not claimed:
+ return 0
+ try:
+ from gateway.delivery_ledger import (
+ RECOVERED_MARKER,
+ mark_delivered,
+ mark_failed,
+ )
+ except Exception:
+ logger.debug("delivery ledger import failed", exc_info=True)
+ return 0
redelivered = 0
for row in claimed:
@@ -12107,6 +12143,19 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
logger.debug("delivery ledger update failed", exc_info=True)
return redelivered
+ async def _redeliver_pending_obligations(self) -> int:
+ """Claim + redeliver in one call — composition of
+ :meth:`_claim_pending_obligations` and
+ :meth:`_redeliver_claimed_obligations`.
+
+ Kept as the stable public shape (tests and any external callers
+ drive this name); the startup path calls the two halves separately
+ so the DB half can run inline before the abandonable send task.
+ """
+ return await self._redeliver_claimed_obligations(
+ await self._claim_pending_obligations()
+ )
+
def _schedule_resume_pending_sessions(self, platform=None) -> int:
"""Auto-continue fresh restart-interrupted sessions after startup.
diff --git a/tests/gateway/test_restart_resume_pending.py b/tests/gateway/test_restart_resume_pending.py
index 5c26e37c627..2e99c2605b1 100644
--- a/tests/gateway/test_restart_resume_pending.py
+++ b/tests/gateway/test_restart_resume_pending.py
@@ -1102,7 +1102,8 @@ async def test_startup_restore_gate_releases_when_boot_path_send_hangs(
return None
runner._send_restart_notification = never_returns
- runner._redeliver_pending_obligations = AsyncMock(return_value=0)
+ runner._claim_pending_obligations = AsyncMock(return_value=[])
+ runner._redeliver_claimed_obligations = AsyncMock(return_value=0)
seen: list[str] = []
@@ -1133,7 +1134,11 @@ async def test_startup_restore_gate_releases_when_boot_path_send_hangs(
)
assert runner._startup_restore_queue == []
assert runner._startup_restore_in_progress is False
- runner._redeliver_pending_obligations.assert_not_awaited()
+ # The DB half (claim + resume clear) runs inline BEFORE the abandonable
+ # send task, so it must have completed even though the boot send hung;
+ # the network half never ran because the hung notification precedes it.
+ runner._claim_pending_obligations.assert_awaited_once()
+ runner._redeliver_claimed_obligations.assert_not_awaited()
hung.set()
leftover = [t for t in list(runner._background_tasks) if not t.done()]
@@ -1149,13 +1154,15 @@ async def test_startup_boot_sends_still_run_when_they_finish_quickly(monkeypatch
runner, _adapter = make_restart_runner()
runner._background_tasks = set()
runner._send_restart_notification = AsyncMock(return_value=None)
- runner._redeliver_pending_obligations = AsyncMock(return_value=0)
+ runner._claim_pending_obligations = AsyncMock(return_value=[])
+ runner._redeliver_claimed_obligations = AsyncMock(return_value=0)
await runner._await_startup_boot_sends(
planned_restart_notification_pending=False,
)
runner._send_restart_notification.assert_awaited_once()
- runner._redeliver_pending_obligations.assert_awaited_once()
+ runner._claim_pending_obligations.assert_awaited_once()
+ runner._redeliver_claimed_obligations.assert_awaited_once()