Repository navigation
gh-659: release the global migration lock when the apply fails - #663
Merged
Merged
Conversation
ApplyAllConfiguredChangesToDatabaseAsync released the lock only on its success paths. An exception anywhere between attaining it and finishing -- initializeSchema, SchemaMigration.DetermineAsync, Migrator.ApplyAllAsync -- skipped the release, and the finally block only disposed the connection. Disposing is not enough. The lock is session-scoped, and while DISCARD ALL does release session advisory locks, Npgsql resets a pooled connection when it is next used rather than when it is returned, so the lock outlives the apply for as long as that connection sits idle. Every other store or replica sharing the database is then locked out of migrating, told "Unable to attain the global lock in time to apply database changes" -- advice whose own remedy, ContinueOnFailures, would start them against an unmigrated schema. Release it on the way out instead, guarded on having actually attained it, and swallow a release that itself fails so the migration failure stays the exception the caller sees. The release passes CancellationToken.None: a cancelled apply is one of the ways to reach this, and the caller's token would mean never releasing in exactly that case. Found while investigating JasperFx/marten#5529, where one test's failed host migration stranded the lock and every later test that migrated against that database failed. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Merged
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 #659.
The bug
DatabaseBase.ApplyAllConfiguredChangesToDatabaseAsyncreleased the global migration lock only on its success paths. An exception anywhere between attaining the lock and finishing —initializeSchema,SchemaMigration.DetermineAsync,Migrator.ApplyAllAsync— skipped the release, and thefinallyonly disposed the connection.Every other store or replica sharing that database was then locked out of migrating, reporting
Unable to attain the global lock in time to apply database changes— whose own advice,ResourceMigrationFailureMode.ContinueOnFailures, would start them against an unmigrated schema.Disposing the connection is not enough, and the test proves it rather than arguing it:
another_node_can_attain_the_lock_after_a_failed_applyfails onmaster.DISCARD ALLdoes release session advisory locks, but Npgsql resets a pooled connection when it is next used, not when it is returned, so the lock outlives the apply for as long as that connection sits idle in the pool. The repo already knew this in another corner —AdvisoryLockTesting.IsFreeAsynccarries the comment "Pooled connections keep session locks after Close, so release explicitly".The fix
The first of the issue's two suggestions: release on the way out, guarded on having actually attained the lock. Not
pg_advisory_xact_lock— the apply is not bracketed in one transaction, so moving the lock's lifetime to a transaction would change where it is held, and this is a bug fix.Two details worth calling out:
CancellationToken.None. A cancelled apply is one of the ways to reach this path, and the caller's token would mean never releasing in exactly that case.The success paths keep their existing explicit releases and their exact current semantics, so a release failure there still surfaces as it does today.
Tests
releasing_the_global_lockinWeasel.Postgresql.Tests, over a real session-scoped advisory lock on an id of its own:masterthe_lock_is_released_after_a_successful_applythe_lock_is_released_after_a_failed_applyanother_node_can_attain_the_lock_after_a_failed_applya_release_that_fails_does_not_replace_the_migration_failureFull local run against PostgreSQL 17:
Weasel.Postgresql.Tests1059 passed,Weasel.Core.Tests574 passed.Provenance
Found while investigating JasperFx/marten#5529: one test's host migration failed with a DDL error, the lock was never released, and every later test that started a host against that database failed to migrate. In a suite that reads as flakiness; in a deployment it is one node's failed migration bricking the rest. A stranded lock currently produces no error at all on the node that stranded it, which is why it went unnoticed.
🤖 Generated with Claude Code