fix(outbox): persist a delivery outcome on its own token, not the shutdown token (NTF-1, WP4 task 4.5)

This commit is contained in:
Joseph Doherty
2026-12-03 09:54:37 -05:00
parent f02fe9d696
commit 5f7b850af2
3 changed files with 85 additions and 5 deletions
@@ -683,7 +683,7 @@ public class NotificationOutboxActor : ReceiveActor, IWithTimers
notification.Status = NotificationStatus.Parked;
notification.LastError = missingAdapterError;
notification.LastAttemptAt = parkedAt;
await WarnIfVanishedAsync(outboxRepository, notification, cancellationToken);
await WarnIfVanishedAsync(outboxRepository, notification);
await EmitAttemptAuditAsync(
notification,
parkedAt,
@@ -741,7 +741,12 @@ public class NotificationOutboxActor : ReceiveActor, IWithTimers
break;
}
await WarnIfVanishedAsync(outboxRepository, notification, cancellationToken);
// NTF-1: from here on the shutdown token is NOT used. The adapter has returned an
// outcome — for a Success the email/SMS has already been accepted by the relay — so
// the write that records it must land even when a singleton handover cancelled the
// token mid-delivery. With the shutdown token the write threw, the row stayed
// Pending/Retrying with no record of the send, and the new active node re-sent it.
await WarnIfVanishedAsync(outboxRepository, notification);
// Emit the per-attempt Attempted row exactly once regardless of the
// outcome (B2). The error message comes from the outcome, not from
@@ -767,6 +772,14 @@ public class NotificationOutboxActor : ReceiveActor, IWithTimers
}
}
/// <summary>
/// Upper bound on the write that records a delivery outcome (NTF-1). That write runs on
/// its own token rather than the actor's shutdown token, so a handover cannot cancel the
/// record of a send that already happened; this keeps a wedged database from pinning a
/// stopping actor's delivery task forever. Settable for tests.
/// </summary>
internal static TimeSpan OutcomePersistTimeout { get; set; } = TimeSpan.FromSeconds(30);
/// <summary>
/// Persists the dispatcher's delivery-state write and logs when the row has
/// VANISHED underneath it — <see cref="INotificationOutboxRepository.UpdateAsync"/>
@@ -783,10 +796,12 @@ public class NotificationOutboxActor : ReceiveActor, IWithTimers
/// </remarks>
private async Task WarnIfVanishedAsync(
INotificationOutboxRepository repository,
Notification notification,
CancellationToken cancellationToken)
Notification notification)
{
var persisted = await repository.UpdateAsync(notification, cancellationToken);
// Deliberately NOT the actor's shutdown token (NTF-1): this records an outcome the
// channel already produced, so it is bounded by its own timeout instead.
using var persistCts = new CancellationTokenSource(OutcomePersistTimeout);
var persisted = await repository.UpdateAsync(notification, persistCts.Token);
if (!persisted)
{
_logger.LogWarning(