Skip to content

Commit 071ff86

Browse files
authored
fix(backend): stop a dead recipient from pinning the daily-summary job cursor forever (#21012) (#21020)
* fix(backend): stop a dead recipient from pinning the daily-summary job cursor forever * docs(failure-class): add FC-failed-resume-renews-its-own-staleness-ttl
1 parent 199f84c commit 071ff86

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"schema_version": 1,
3+
"id": "FC-failed-resume-renews-its-own-staleness-ttl",
4+
"violated_contract": "A resumable checkpoint's TTL-based staleness bound only protects against abandonment if the checkpoint goes untouched while stale; it does nothing against a checkpoint that is touched every tick but never advances. The daily-summary job resumes its saved cohort before serving the current one, and a single recipient whose push keeps reporting zero devices delivered re-fails on every tick; the failure handler checkpoints that exact recipient again with a fresh Redis TTL (documented as a 2-hour staleness bound), so the \"stale cursor expires and the next run starts clean\" safety net never fires -- the cohort is resumed, re-fails, and re-checkpointed forever. Because the job only serves the current cohort after the resumed one reports complete, every other recipient's recap silently stopped generating for as long as the one dead recipient kept failing (observed: six days and 17k+ recipients, #21012).",
5+
"canonical_prevention": "Measure a resumed cohort's age from the cohort's own origin instant compared against the current time, not from whether the checkpoint key still exists in Redis -- a failed resume rewrites the key and renews its TTL, so TTL survival cannot distinguish a cohort that is actively failing from one that is healthily in progress. Once the age exceeds the documented staleness bound, abandon the resume unconditionally (clear the checkpoint, log the abandoned recipient, proceed to the current cohort) rather than returning early on an incomplete resume.",
6+
"canonical_prevention_artifact": [
7+
"backend/utils/other/notifications.py",
8+
"backend/tests/unit/test_daily_summary_job_resilience.py"
9+
],
10+
"evidence_prs": [],
11+
"scope_hints": [
12+
"backend/utils/other/notifications.py",
13+
"backend/utils/other/daily_summary_budget.py"
14+
],
15+
"status": "open"
16+
}

‎backend/tests/unit/test_daily_summary_job_resilience.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,43 @@ def unavailable(zones, hour):
691691
assert notifications.summary_budget.cursor_cohort_utc(saved) == original
692692

693693

694+
def test_a_cursor_stuck_past_its_staleness_bound_is_abandoned_so_current_recipients_are_not_starved() -> None:
695+
"""A recipient who can never succeed (an invalid token, say) keeps getting
696+
retried and rewriting the checkpoint every tick, which renews its Redis TTL
697+
and defeats the staleness bound documented in daily_summary_budget.py. That
698+
pins every execution on the same dead cohort forever, and the *current*
699+
cohort below is never reached -- every other recipient's recap silently
700+
stops too, for as long as the one broken recipient keeps failing."""
701+
with _loaded_job() as (notifications, notification_db, redis, _fallbacks):
702+
now = datetime(2026, 10, 9, 6, 0, tzinfo=timezone.utc)
703+
stuck_at = now - timedelta(seconds=notifications.summary_budget.JOB_CURSOR_TTL_SECONDS, hours=1)
704+
705+
class FixedDateTime(datetime):
706+
@classmethod
707+
def now(cls, tz=None):
708+
return now.astimezone(tz) if tz else now.replace(tzinfo=None)
709+
710+
notifications.datetime = FixedDateTime
711+
notifications._get_timezones_grouped_by_hour = lambda at: {at.hour: ['UTC']}
712+
notifications.summary_budget.write_job_cursor(
713+
notifications.summary_budget.job_cursor_key(),
714+
notifications.summary_budget.make_cursor(stuck_at.hour, 'stuck-uid', stuck_at),
715+
)
716+
reads = []
717+
718+
def selector(zones, hour):
719+
reads.append(hour)
720+
return []
721+
722+
notification_db.get_users_for_daily_summary_indexed = selector
723+
outcome = asyncio.run(notifications.send_daily_summary_notification())
724+
725+
assert reads == [now.hour], 'the dead cohort must not be re-queried; only the current cohort runs'
726+
assert outcome.ok and outcome.complete
727+
saved = notifications.summary_budget.read_job_cursor(notifications.summary_budget.job_cursor_key())
728+
assert saved is None, 'the stale checkpoint must be cleared, not rewritten pointing at the same dead cohort'
729+
730+
694731
def test_due_local_hours_include_half_and_quarter_hour_zones() -> None:
695732
with _loaded_job() as (notifications, _db, _redis, _fallbacks):
696733
now = datetime(2026, 10, 3, 16, 15, tzinfo=timezone.utc)

‎backend/utils/other/notifications.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,25 @@ async def send_daily_summary_notification() -> DailySummaryCronOutcome:
417417
now = datetime.now(pytz.utc)
418418
cursor = await run_blocking(db_executor, summary_budget.read_job_cursor, summary_budget.job_cursor_key())
419419
resumed_at = summary_budget.cursor_cohort_utc(cursor)
420+
cursor_key = summary_budget.job_cursor_key()
421+
# The Redis TTL is supposed to be the staleness bound (daily_summary_budget.py),
422+
# but every tick that resumes this cohort rewrites the checkpoint and renews
423+
# that TTL, so a recipient who can never succeed (e.g. a permanently invalid
424+
# FCM token) keeps the checkpoint alive forever. That pins every execution on
425+
# the same stale cohort and the *current* cohort below is never reached, so
426+
# every other recipient's recap silently stops too. Age the cohort out by
427+
# the same bound independent of the TTL renewal.
428+
if resumed_at is not None and now - resumed_at > timedelta(seconds=summary_budget.JOB_CURSOR_TTL_SECONDS):
429+
logger.error(
430+
'daily_summary_cursor_abandoned_stale cohort_utc=%s age_seconds=%s hour=%s uid=%s',
431+
resumed_at.isoformat(),
432+
(now - resumed_at).total_seconds(),
433+
summary_budget.cursor_hour(cursor),
434+
summary_budget.cursor_uid(cursor),
435+
)
436+
await run_blocking(db_executor, summary_budget.clear_job_cursor, cursor_key)
437+
resumed_at = None
438+
cursor = None
420439
# A local hour can roll over at :15/:30/:45 within the same UTC hour.
421440
if resumed_at is not None and resumed_at.replace(
422441
minute=(resumed_at.minute // 15) * 15, second=0, microsecond=0

0 commit comments

Comments
 (0)