Repository navigation
GH-6151: The outbox stale sweep releases by key on MySQL and SQL Server - #6164
Merged
Merged
Conversation
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>
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.
Closes #6151
Follow-up to #6142 (#6148 on
main, #6150 on6.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:
OutboxStaleTimedefaults to 1 hour (DurabilitySettings.DefaultOutboxStaleTime) unlessV6DefaultsRestoredis set.The fix
On the providers that release ownership by key (MySQL and SQL Server, from #6148), the sweep is now a
ReleaseStaleOutgoingEnvelopesCommandposted after the recovery batch, the wayReleaseStaleIncomingEnvelopesCommandworks 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 predicateowner_id != 0 and timestamp <= @stalechecked again in each update.StaleOutboxSweep.Build()is the single entry point that the agent and the tests share. PostgreSQL, SQLite and Oracle keepBumpStaleOutgoingEnvelopesOperationin 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_deletesin 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.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 existingbumping_stale_outbox_messagesnow runs the sweep throughStaleOutboxSweep.Build, so it covers the new path.What I ran
All on this branch after rebasing onto
mainwith #6148 merged, locally, Docker MySQL 8.0 and SQL Server:dotnet build wolverine.slnx -c Release -f net10.0: 0 errors, 0 warnings.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_6151before/after runs as listed above.Not run
BumpStaleOutgoingEnvelopesOperationin the batch; the only change they see is theRunsOnItsOwncheck inDurabilityAgent../build.sh LocalGate/ CI suites.🤖 Generated with Claude Code