Skip to content

GH-6151: The outbox stale sweep releases by key on MySQL and SQL Server - #6164

Merged
jeremydmiller merged 1 commit into
mainfrom
gh-6151-outbox-stale-sweep
Dec 4, 2026
Merged

jeremydmiller merged 1 commit into
mainfrom
gh-6151-outbox-stale-sweep

Conversation

@jeremydmiller

Copy link
Copy Markdown
Member

Closes #6151

Follow-up to #6142 (#6148 on main, #6150 on 6.0), which fixed the same lock-order problem for the inbox. This PR fixes the outbox.

The problem

BumpStaleOutgoingEnvelopesOperation (OutboxStaleTime) ran in the shared recovery batch and reached the outbox rows through the owner index, so it locked each index entry before its row. A sender deletes a row it has just sent by primary key, so it locks the row first and the index entries after. On MySQL the two can deadlock. When the sweep is the victim, the whole shared recovery batch it rides in rolls back.

On 7.0 this sweep runs by default: OutboxStaleTime defaults to 1 hour (DurabilitySettings.DefaultOutboxStaleTime) unless V6DefaultsRestored is set.

The fix

On the providers that release ownership by key (MySQL and SQL Server, from #6148), the sweep is now a ReleaseStaleOutgoingEnvelopesCommand posted after the recovery batch, the way ReleaseStaleIncomingEnvelopesCommand works for the inbox. It reads the stale keys without locking, then updates each row by primary key in key order, 100 rows per transaction, with the predicate owner_id != 0 and timestamp <= @stale checked again in each update. StaleOutboxSweep.Build() is the single entry point that the agent and the tests share. PostgreSQL, SQLite and Oracle keep BumpStaleOutgoingEnvelopesOperation in the batch.

SQL Server gets the change because the two providers share one opt-in (releasesOwnershipByKey), not because it reproduced: see below.

The repro is weak

Bug_6151_outbox_stale_sweep_deadlocks_with_outgoing_deletes in MySqlTests and SqlServerTests: 8 connections insert an outgoing row owned by node 1 and delete it by id, 2,000 times each, while a ninth runs the outbox stale sweep every 10 ms. The test asserts no deadlock on either side.

  • Before, MySQL: deadlocked once in 11 runs. The sweep was the victim.
  • Before, SQL Server: never deadlocked in 5 runs.
  • After: MySQL 8 of 8 green; SQL Server 3 of 3 green.

So this test is a weak guard: a regression would most likely still pass it. A second MySQL test, the_sweep_releases_only_the_stale_rows, checks the behaviour (stale rows released, fresh and unowned rows untouched). SQL Server's existing bumping_stale_outbox_messages now runs the sweep through StaleOutboxSweep.Build, so it covers the new path.

What I ran

All on this branch after rebasing onto main with #6148 merged, locally, Docker MySQL 8.0 and SQL Server:

  • dotnet build wolverine.slnx -c Release -f net10.0: 0 errors, 0 warnings.
  • MySqlTests, full suite: 574/574 passed.
  • SqlServerTests, full suite: 1,023 passed, 0 failed, 2 skipped.
  • Before rebasing (on top of the GH-6142: Owner releases and the stale-inbox sweep no longer deadlock with mark-as-handled (MySQL, SQL Server) #6148 branch), the same two suites ran once more: MySqlTests 574/574; SqlServerTests had 3 failures in SqlServerMessageStoreTests (one counted 302 rows where 101 were expected, so another writer was in the shared SQL Server), and the class passed 102/102 twice when re-run on its own.
  • Bug_6151 before/after runs as listed above.

Not run

  • PostgreSQL, SQLite, Oracle, Marten, Polecat and EF Core suites. Those providers keep BumpStaleOutgoingEnvelopesOperation in the batch; the only change they see is the RunsOnItsOwn check in DurabilityAgent.
  • The full ./build.sh LocalGate / CI suites.

🤖 Generated with Claude Code

BumpStaleOutgoingEnvelopesOperation (OutboxStaleTime, on by default since 7.0) reached the
outbox rows through the owner index, locking each entry before its row, while a sender deletes
a row it has just sent by primary key, locking the row first. On MySQL the two deadlocked, and
a sweep chosen as the victim rolled back the whole shared recovery batch it rode in.

On the providers that release ownership by key (GH-6142), the sweep is now a
ReleaseStaleOutgoingEnvelopesCommand posted after the recovery batch: read the stale keys
without locking, then update by primary key in key order. StaleOutboxSweep.Build is the
single entry point the agent and the tests share.

The deadlock is rare: Bug_6151 (8 connections inserting and deleting outgoing rows, the sweep
every 10 ms) deadlocked once in 11 pre-fix runs on MySQL and never in 5 on SQL Server, so the
test is a weak guard. It passed 8 of 8 after the fix. A second MySQL test checks that the sweep
releases only the stale rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jeremydmiller jeremydmiller added this to the 7.0 milestone Dec 3, 2026
Copilot AI balanced review requested due to automatic review settings December 3, 2026 23:44

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@jeremydmiller
jeremydmiller merged commit 6d81a03 into main Dec 4, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Outbox stale sweep (OutboxStaleTime) may deadlock with outgoing deletes on MySQL and SQL Server, as #6142 did for the inbox

2 participants