Skip to content

Commit da36855

Browse files
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/mod.rs‎

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ static BACKLOGS: Backlogs = Backlogs(Mutex::new(BTreeMap::new()));
8181
pub(crate) trait Sender: Send + Sync + 'static {
8282
/// The channel sent, which defines what its deliveries hold.
8383
type Channel: Channel;
84-
/// How long after its creation a delivery keeps being retried. A retryable
85-
/// failure that would retry later than this fails it instead.
84+
/// How long after its creation, or an operator's last retry, a delivery keeps
85+
/// being retried. A retryable failure that would retry later than this fails it
86+
/// instead, and so does a delivery no release could decode by then.
8687
const MAX_AGE: Duration;
8788
/// How long a worker holds a claimed delivery: well beyond [`SEND_TIMEOUT`].
8889
/// Tests shorten it to see another replica take over a stalled attempt.
@@ -254,7 +255,7 @@ impl<S: Sender> Worker<S> {
254255
let claimed = tokio::select! {
255256
biased;
256257
() = shutdown.requested() => return,
257-
claimed = deliveries.claim::<S::Channel>(S::LEASE) => claimed,
258+
claimed = deliveries.claim::<S::Channel>(S::LEASE, S::MAX_AGE) => claimed,
258259
};
259260
match claimed {
260261
Ok(Some(delivery)) => {
@@ -302,24 +303,10 @@ impl<S: Sender> Worker<S> {
302303
}
303304
}
304305

305-
/// When `delivery` stops being attempted: [`Sender::MAX_AGE`] after it was
306-
/// created, or after an operator last retried it.
307-
fn expires(delivery: &Delivery<S::Channel>) -> DateTime<Utc> {
308-
TimeDelta::from_std(S::MAX_AGE)
309-
.ok()
310-
.and_then(|age| {
311-
delivery
312-
.retried_at
313-
.unwrap_or(delivery.created_at)
314-
.checked_add_signed(age)
315-
})
316-
.unwrap_or(DateTime::<Utc>::MAX_UTC)
317-
}
318-
319306
/// Sends `delivery` within [`SEND_TIMEOUT`], unless it expired while it waited,
320307
/// and records the outcome, which is discarded if the lease was lost meanwhile.
321308
async fn send(sender: Arc<S>, database: Database, delivery: Delivery<S::Channel>) {
322-
let expires = Self::expires(&delivery);
309+
let expires = delivery.expires_at;
323310
let result = if Utc::now() > expires {
324311
Err(DeliveryError::Permanent(Failure::Expired))
325312
} else {

‎apps/auth-service/src/oidc/tests/logout.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ async fn logout_posts_a_logout_token_to_the_clients_of_the_session() {
9191
.app
9292
.database
9393
.deliveries()
94-
.claim::<BackchannelLogout>(std::time::Duration::from_mins(1))
94+
.claim::<BackchannelLogout>(
95+
std::time::Duration::from_mins(1),
96+
BackchannelLogoutSender::MAX_AGE,
97+
)
9598
.await
9699
.expect("claim")
97100
{

‎crates/database/.sqlx/query-0b3fbd2d055b1a275086714282268d0ad24e99e7f69f8ca896771bf711705e0b.json‎

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/database/.sqlx/query-5536142913ad35c1ed0ebb922fc3a1ae6eb8fa4861830d38e240f053ed360d75.json‎

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/database/.sqlx/query-9ffe95371c4b38abbcbac1f39d500efcb76050d9d920919ad9191aa006f0efb2.json‎

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)