From 4607e5017c8de033d34a9711f6862fdf306b6c3e Mon Sep 17 00:00:00 2001 From: kshitijk4poor <82637225+kshitijk4poor@users.noreply.github.com> Date: Tue, 22 Sep 2026 14:45:09 +0530 Subject: [PATCH] refactor(gateway): drop the test-only delivery-ledger `_prune()` wrapper After 39e914ea33 the prune runs inside `record_obligation`'s transaction via `_prune_unlocked`; `_prune()` had a single caller left, the test `TestPrune::test_old_delivered_rows_pruned`. It also opened its own transaction WITHOUT `_DB_LOCK` while every other writer in the module takes `_DB_LOCK, _transaction()`, so the `_unlocked` contract ("caller holds the lock") was violated by its only wrapper. Delete it and have the test do what a writer does: `with dl._DB_LOCK, dl._transaction() as conn: dl._prune_unlocked(conn, now)`. `grep -rn '_prune(' --include=*.py gateway tests/gateway`: no production caller (gateway/run.py's hit is `_housekeeping_checkpoint_prune`). PROOF: tests/gateway/test_delivery_ledger.py (TestPrune included) + test_delivery_ledger_single_connection.py: 40 passed. --- gateway/delivery_ledger.py | 9 --------- tests/gateway/test_delivery_ledger.py | 4 +++- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/gateway/delivery_ledger.py b/gateway/delivery_ledger.py index 675ce4e4fa..d83738bc74 100644 --- a/gateway/delivery_ledger.py +++ b/gateway/delivery_ledger.py @@ -523,15 +523,6 @@ def _prune_unlocked(conn, now: float) -> None: LIMIT ?)""", (total - _MAX_ROWS,)) -def _prune(now: Optional[float] = None) -> None: - now = now if now is not None else time.time() - try: - with _transaction() as conn: - _prune_unlocked(conn, now) - except Exception: - logger.debug("delivery ledger prune failed", exc_info=True) - - def ledger_enabled(config: Optional[Dict[str, Any]] = None) -> bool: """Read the ``gateway.delivery_ledger`` config gate (default on).""" try: diff --git a/tests/gateway/test_delivery_ledger.py b/tests/gateway/test_delivery_ledger.py index f07b9da7eb..316a649b26 100644 --- a/tests/gateway/test_delivery_ledger.py +++ b/tests/gateway/test_delivery_ledger.py @@ -376,7 +376,9 @@ class TestPrune: "UPDATE delivery_obligations SET updated_at=? WHERE obligation_id=?", (time.time() - dl._RETENTION_SECONDS - 60, "ob-1"), ) - dl._prune() + # Prune has no wrapper of its own: it runs inside a writer's transaction, lock held. + with dl._DB_LOCK, dl._transaction() as conn: + dl._prune_unlocked(conn, time.time()) assert _row("ob-1") is None