Repository navigation
Commit da36855
authored
fix(auth): emails a new release adds no longer fail when the old release claims them mid-deploy (#508)
During a deploy the previous release runs beside the new one. If the new
release added an email kind, the old release's workers could claim one
of its deliveries, fail to decode it, and mark it failed. It then stayed
failed until an operator retried it, even though the new release would
have sent it.
Now a worker that can't decode a delivery leaves it for a release that
can. `DeliveryRepository::claim` releases the row without spending an
attempt, sets its last error to `stored recipient or payload does not
decode`, makes it due again in 30 seconds, and moves on to the next due
delivery. So the old release keeps sending what it can decode and
doesn't loop on what it can't. While both releases run, a worker of
either may claim it each time it comes due, so the new release sends it
at the latest 30 seconds after the old one stops. A delivery that no
release decodes fails at its channel's `MAX_AGE`. To check that, `claim`
now takes the sender's `MAX_AGE` and returns the delivery's
`expires_at`, which replaces the worker's own expiry calculation.
While a row waits, the CLI's `deliveries list` and the admin dashboard
show it as `pending`, with its attempts unchanged and the decode error
as its last error. Both already show these fields, so neither needed a
code change. The delivery queue, email operations and notification
how-to docs now describe this behaviour, including that a release from
before this change still fails such a delivery at once (so this PR's own
deploy, and the account recovery note, keep the retry advice).
Proof: a database test with two versions of one channel: the old one
skips an undecodable row and sends a decodable one, then the new one
sends the undecodable row. A delivery claimed past its maximum age
fails.
Closes #450
Reviewed with review-loop: 1 round with 2× Claude Opus 5.5 and 2× Claude
Fable 5.2, on high. Verdict: the code was approved; all four asked for
doc fixes (the 30-second bound and the recovery rollout note), which are
in, and doc-only fixes needed no further round.
> [!NOTE]
> Written by Claude Opus 5.5 in Claude Code and reviewed by Claude Opus
5.5 and Claude Fable 5.2 (via T3 Code)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->
## Summary
This change stops a worker from failing a delivery that it cannot
decode. The delivery stays pending for a release that can decode it. The
change also moves delivery expiry into the claim query. The change
touches 10 files: 1 application module, 1 test, 3 SQLx query metadata
files, 1 repository module, its tests, and 3 documentation files.
### Delivery repository
(`crates/database/src/repositories/deliveries/mod.rs`)
- `DeliveryRepository::claim` now takes a second parameter: `claim<C:
Channel>(&mut self, lease: Duration, max_age: Duration)`.
- When a claimed row cannot be decoded, `claim`:
- releases the claim,
- does not count an attempt,
- records the error `stored recipient or payload does not decode`,
- makes the delivery due again after 30 seconds,
- continues to the next due delivery.
- Expiry is measured from the delivery's creation time or from the
operator's latest retry.
- If a row cannot be decoded after its expiry, `claim` marks the
delivery failed and returns `InvalidStoredData`.
- The public `Delivery<C>` struct replaces `created_at` and `retried_at`
with `expires_at: DateTime<Utc>`.
- `ClaimedRow` also carries `expires_at`.
### Database queries (`crates/database/.sqlx/`)
- A new query metadata file describes the claim query. The query selects
the earliest due delivery whose lease is absent or expired. It skips
rows locked by other transactions. It sets the claim token and lease
expiry, increments attempts, and returns `expires_at`. The expiry is
based on `retried_at` or `created_at` plus the supplied interval.
- A new query metadata file describes a release query. The query
decrements attempts, sets the next attempt time and last error, and
clears the claim token and lease. It requires a matching delivery ID and
claim token.
- The previous claim query metadata file is deleted.
### Application worker (`apps/auth-service/src/deliveries/mod.rs`)
- `Worker::send` passes the lease and the channel's `MAX_AGE` to
`claim`.
- `Worker::send` reads `expires_at` from the stored delivery. The local
expiry calculation is removed.
- The `Sender::MAX_AGE` documentation now describes the retry lifetime.
It includes the time since the operator's last retry. It also states
that a delivery that no release can decode by the deadline fails.
- No public declarations change.
### Application tests (`apps/auth-service/src/oidc/tests/logout.rs`)
- The backchannel logout delivery test claims deliveries with a
one-minute claim duration. It uses `BackchannelLogoutSender::MAX_AGE` as
the maximum age.
### Repository tests
(`crates/database/src/repositories/deliveries/tests.rs`)
- The test-channel macro accepts a payload type for each channel.
Previously every channel used `u32`.
- Old-release and new-release channel declarations use the same channel
name. The new release adds a message variant.
- The `claim` test helper passes a one-hour maximum age.
- The operator retry test checks that a retry extends `expires_at`. It
no longer checks that `retried_at` is set.
- A new compatibility test covers two releases:
- The old release claims the shared `Known` message.
- The old release leaves the unknown `Added` message pending, with no
attempt counted.
- The new release claims the `Added` message once it is due.
- A new test uses a zero maximum age. An undecodable message returns
`InvalidStoredData` and appears as failed with `UNDECODABLE`.
### Documentation
- `docs/dev/architecture/delivery-queue.md`: Describes how workers
handle undecodable recipients or payloads. It also covers deployment and
rollback behavior and when operators can retry or cancel these
deliveries.
- `docs/dev/extending/notification.md`: The "Limits" section covers
deploys and rollbacks.
- During a deploy, an unknown notification email waits up to 30 seconds
after the previous release stops.
- After a rollback to a release that does not know the kind, the email
waits and then fails at the email channel's 24-hour `MAX_AGE`.
- A release that predates this behavior marks the email failed
immediately.
- Operators retry failed deliveries after a release that knows the kind
serves again.
- `docs/dev/operations/email.md`: States that an undecodable email waits
for a release that can decode it, up to the channel's `MAX_AGE`. While
it waits, its last error is `stored recipient or payload does not
decode`.
### Behavior change
- A delivery that an older release cannot decode is no longer failed by
that release. A newer release can send it.
- A delivery that no release can decode ends at its channel's `MAX_AGE`.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->1 parent f4cdb9c commit da36855
10 files changed
Lines changed: 352 additions & 169 deletions
File tree
- apps/auth-service/src
- deliveries
- oidc/tests
- crates/database
- .sqlx
- src/repositories/deliveries
- docs/dev
- architecture
- extending
- operations
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
84 | | - | |
85 | | - | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
86 | 87 | | |
87 | 88 | | |
88 | 89 | | |
| |||
254 | 255 | | |
255 | 256 | | |
256 | 257 | | |
257 | | - | |
| 258 | + | |
258 | 259 | | |
259 | 260 | | |
260 | 261 | | |
| |||
302 | 303 | | |
303 | 304 | | |
304 | 305 | | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | | - | |
312 | | - | |
313 | | - | |
314 | | - | |
315 | | - | |
316 | | - | |
317 | | - | |
318 | | - | |
319 | 306 | | |
320 | 307 | | |
321 | 308 | | |
322 | | - | |
| 309 | + | |
323 | 310 | | |
324 | 311 | | |
325 | 312 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
94 | | - | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
95 | 98 | | |
96 | 99 | | |
97 | 100 | | |
| |||
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 commit comments