Skip to content

AI-3513: count a forward's attempt at its claim, leave an unopenable secret to another router - #2269

Merged
Loschcode merged 4 commits into
mainfrom
laurentschaffner/ai-3513-webhook-forwards-are-retried-for
Nov 24, 2026
Merged

Loschcode merged 4 commits into
mainfrom
laurentschaffner/ai-3513-webhook-forwards-are-retried-for

Conversation

@Loschcode

@Loschcode Loschcode commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Why

https://linear.app/stream/issue/AI-3513/webhook-forwards-are-retried-for-ever-when-the-sender-dies-and-deleted

Two problems with webhook forwards:

  • Only Retry counted an attempt, and it runs after a send. If the forwarder died between the claim and the send, the delivery came back with the same count every time and was never given up.
  • A router that did not hold the key version a destination's secret was sealed with treated the forward as refused and deleted it, even though another router could have sent it.

What changed

  • Claim counts the attempt (store/event_delivery.go). The claim's UPDATE now sets attempts = cedl.attempts + 1, as claim in queue.go does. Retry only writes the next due time and no longer takes attempts. The forwarder reads the count the claim returned. A delivery that was claimed more times than it may be tried (the first try plus one per retry wait) is now given up without being sent. It is counted as forward_failed, because the claims before it never recorded an outcome.
  • Secret this router cannot open (eventforward/forwarder.go).
    • When opening the secret fails with auth.ErrKeyVersionUnavailable, the existing sentinel the sealer returns for a key version it does not hold, the forwarder calls the new EventDeliveries.Release. Release takes back the attempt the claim counted and makes the row due again one lease later, on Postgres's clock, as if the claim had simply run out. It does not make the row due at once: this router is woken by its own finished send, so it would take the destination's backlog straight back on every poll.
    • Release is fenced on the claim it gives back (attempts = ? AND next_attempt_at = ?, the values the claim wrote), so a release whose lease ran out cannot undo another router's newer claim.
    • The error is logged once per lease per destination, not once per row.
    • A forward that no router has signed for longer than its whole retry schedule plus one lease is given up as forward_failed. Before this change it was deleted on the first try; without the bound it would cycle forever.
    • A secret that opens under no key (a corrupt blob) is still given up as forward_failed, as TestAForwardThatCouldNotBeSignedIsCountedAsNotSent expects.
    • Release takes the attempt back, while AI-3517: judge leases on the database's clock #2267's unclaim for the other queues leaves it counted. The difference is on purpose. Those queues fence every later write on attempts (heldBy), so lowering it there would let the claim before it write again. Event delivery Retry/Finish take no attempts fence, so nothing here reads the number as a token.
  • eventforward/AGENTS.md describes both behaviours.

This change is independent of #2267 (AI-3517). That PR moves queue.go's claim onto the database clock and leaves the event delivery claim alone. Three things are filed as AI-3716 rather than fixed here:

  • The event delivery claim still compares and leases on the calling router's clock.
  • A claim that Close cuts off between its commit and the read of its rows uses up an attempt.
  • A previous secret that does not open blocks a delivery whose current secret opens (openSecrets).

No other open PR touches event_delivery.go or internal/eventforward.

Tests

  • TestAForwardWhoseSenderDiesEveryTimeIsGivenUpAfterItsAttempts: three claims whose lease ends at once and that are never sent, then a started forwarder. The delivery is dropped and the destination is never hit.
  • TestAForwardWhoseSecretThisRouterCannotOpenIsLeftToOneThatCan: a router on key version 1 alone neither sends nor drops a forward sealed under version 2. Once a router that holds version 2 starts, it sends the forward exactly once.
  • TestAForwardNoRouterCanSignIsGivenUpAndNotClaimedEveryPoll: a router on key version 1, with a 500 ms lease, releases a forward sealed under version 2 at least once and at most twice, then gives it up. It sends nothing, and does not claim the forward again on each 10 ms poll.
  • TestAClaimCountsItsAttemptAndAReleaseTakesItBack (store): two claims count 1 and 2. After a release the row is due again, even though the last lease runs an hour, and the next claim counts 2. That claim runs a minute on, so the release's DB-clock now() is behind it.
  • TestAReleaseOfAClaimTakenOverChangesNothing (store): releasing a stale claim leaves the newer claim's attempts and lease as they were.
  • TestEventDeliveryQueryBudgets covers Release (1 query).
  • TestARetriedDeliveryIsDueAtItsNextAttempt still sees 1 attempt: the count now comes from the claim.
  • go test -tags integration ./internal/eventforward (3 runs), the whole ./internal/store StoreSuite, and ./internal/channelbridge: all pass. go vet ./... and gofmt are clean.

🤖 Generated with Claude Code

…an unopenable secret to another router

The claim of event deliveries now counts the attempt, as queue.go's claim
does, and Retry no longer writes it. A delivery whose forwarder dies between
claim and send every time is given up once it was claimed more often than it
may be tried, instead of coming back with the same count for ever.

A router that does not hold the key version a destination's secret was
sealed with sends nothing: it gives the counted attempt back and leaves the
delivery due a poll later, rather than deleting it as refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the docs label Nov 23, 2026
@Loschcode

Copy link
Copy Markdown
Contributor Author

/jeffery review

@getstream-metis

Copy link
Copy Markdown

Thanks for asking Jeffery for review. I'll stay on this PR until I can approve it.

How it works

  • Findings come as inline comments, each starting with [Must Fix], [Should Fix], nit: or consider:.
  • Push fixes whenever you're ready. Once the branch has been quiet for 10 minutes I review the new head and resolve the threads it fixes. To skip the wait, comment /jeffery review.
  • If you disagree or have a question, reply in the thread. I'll answer there, and resolve it if you're right.
  • I approve once every must-fix and should-fix is closed and nobody has requested changes. CI isn't part of that; branch protection still holds the merge until it's green. From round 3 on, open should-fix findings stop blocking.
  • I don't approve release/* branches or changes to protected paths, and I'll say so when that applies.
  • If a must-fix is still open after round 8, I stop and hand the PR to a human. Draft PRs wait until they're marked ready.

Commands (each as a whole comment)

Command What it does
/jeffery review Review the current head now
/jeffery rereview Start over at round 1 on the current head and withdraw my approval. This redoes the whole review and costs as much as the first one, so use it only when you don't trust the earlier rounds. If nothing changed since my last round I say so instead; add force to redo it anyway
/jeffery pause / /jeffery resume Stop or restart automatic reviews on this PR
/jeffery status What I last reviewed, and why I have or haven't approved
/jeffery help This list

/review-agent or a mention of me works in place of /jeffery.

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Round 1 on 98735f3f73a4 · 🆕 2 new

getstream-metis[bot]
getstream-metis Bot previously approved these changes Nov 23, 2026

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved 98735f3f73a4 after 1 round

Left open, your call:

…im counted

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Loschcode

Copy link
Copy Markdown
Contributor Author

Round 1, point by point:

  • Shutdown during a send now spends an attempt: fixed in d6d8aeb, by changing the comments rather than adding a Release. The attempt stays counted on purpose. A send cut short by Close may already have reached the destination, and from the row's side it looks the same as a router that died mid-send, which is exactly the case this PR counts. The comments on Close and at the early return now say so.
  • Unopenable key version is re-claimed every poll forever: left as is, and flagged under "Needs a call" in the description. The ticket asks for the claim to be released without counting an attempt, so another router can send it. Capping releases, or giving up past the retry schedule, is the other option. Laurent will decide between them.

@Loschcode

Copy link
Copy Markdown
Contributor Author

/jeffery review

@getstream-metis
getstream-metis Bot dismissed their stale review November 23, 2026 19:36

Withdrawing approval until d6d8aeb is reviewed.

getstream-metis[bot]
getstream-metis Bot previously approved these changes Nov 23, 2026

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved d6d8aeb4732f after 2 rounds · 1 fixed

Left open, your call:

Laurent Schaffner and others added 2 commits November 24, 2026 09:54
…release

A release is due a lease later rather than a poll, so a router without the
key no longer cycles the destination's backlog every second, and logs once
a lease per destination. A forward no router has signed for longer than its
retries and a lease is given up as forward_failed. Release names the claim
it gives back by its attempts and lease, so it cannot undo a newer claim.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@getstream-metis
getstream-metis Bot dismissed their stale review November 24, 2026 08:55

Withdrawing approval until d7ab375 is reviewed.

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Round 3 on d7ab37572667 · 🆕 1 new · ✅ 1 fixed
Still open: 1 should-fix. Full list on the checks tab.

// The claim counted this attempt.
attempts := delivery.Attempts
switch {
case errors.Is(err, auth.ErrKeyVersionUnavailable) && time.Since(delivery.CreatedAt) > f.unsignedWait():

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Should Fix] Give-up by row age drops a forward still on its retries
Issue: A forward still within its normal retries, or one that waited behind a slow destination's backlog, is dropped as not sent the first time a router without its key version claims it. A router that holds the key never gets to send it.
Cause: forwarder.go:532 compares time.Since(delivery.CreatedAt) with unsignedWait(), which is the lease plus the sum of the retries. That measures the row's age, not how long no router could sign it. Retries are scheduled from the end of each send (forwarder.go:549), so real retries pass the margin.
Fix: Time the give-up from the first release for ErrKeyVersionUnavailable. For example, store a marker that Release sets once and that a successful send or a retry leaves alone.
Side effect: This needs a new column or marker on the delivery row, so check the migration order.

Evidence
  • created_at comes from DEFAULT now() in Queue (store/event_delivery.go:87), so it is the queue time
  • unsignedWait = lease + sum(retries) (forwarder.go:562-568); defaultRetries sum is about 2h35m (line 90)
  • Retry at forwarder.go:549 schedules from time.Now() after each send, and attemptTimeout is 15s (line 50). Four timed-out sends add 60s, which is the whole lease margin.
  • Key rotation rollout: a v1-only router claims an old row, errors.Is(err, ErrKeyVersionUnavailable) holds and the row age is over the limit. Finish at line 536 deletes it without any v2 router trying.

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved d7ab37572667 after 3 rounds · 2 fixed

Left open, your call:

@Loschcode
Loschcode merged commit 2db842c into main Nov 24, 2026
9 of 13 checks passed
@Loschcode
Loschcode deleted the laurentschaffner/ai-3513-webhook-forwards-are-retried-for branch November 24, 2026 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant