fix(outbox): discard clears incoming envelope after rolled-back outbox#3327
Merged
jeremydmiller merged 2 commits intoJul 9, 2026
Merged
Conversation
…x commit The Marten and Polecat FlushOutgoingMessagesOnCommit listeners flipped Envelope.Status to Handled in BeforeSaveChangesAsync, before the commit. On rollback (e.g. a duplicate document insert violating the unique PK) the DB row stayed 'Incoming' but the stale in-memory flag made DurableReceiver._markAsHandled skip the real UPDATE, so a .Discard() policy left the envelope stuck as 'Incoming' and it was reprocessed on every InboxStaleTime reclaim / restart. Defer the in-memory status flip to AfterCommitAsync, which only runs once the commit is durable. Polecat's ITransactionParticipant path has no after-commit hook, so it simply no longer sets the premature flag (the idempotent _markAsHandled UPDATE covers the success path). Adds red-green regression tests for both Marten and Polecat. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…led-outbox-commit
This was referenced Jul 9, 2026
Merged
This was referenced Jul 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a durable-local-queue handler enlists the Marten (or Polecat) outbox and
SaveChangesAsyncrolls back — e.g. a document insert hits a unique constraint — an.OnException<T>().Discard()policy fails to remove the incoming envelope. It staysstatus='Incoming'inwolverine_incoming_envelopesand is reprocessed on everyInboxStaleTimereclaim / restart.Root cause
FlushOutgoingMessagesOnCommit.BeforeSaveChangesAsyncflipsEnvelope.Status = Handledin memory before the commit, alongside queuing the mark-handledUPDATEinto the same batch. On rollback the DB row staysIncoming, but the in-memory flag is not reverted. The discard path then hits the_markAsHandledoptimization inDurableReceiver(if (e.Status == Handled) return;), which trusts the stale flag and skips the realUPDATE— so the row is never marked handled.Fix
Defer the in-memory
Status = Handledflip toAfterCommitAsync, which only runs once the commit is durable. Polecat'sITransactionParticipantpath has no after-commit hook, so it simply stops setting the premature flag — the idempotent_markAsHandledUPDATEcovers the success path.Testing
Incoming == 1, pass after; verified against the reverted code on both stores).