You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ApplyAllConfiguredChangesToDatabaseAsync never releases the global lock when the migration fails, locking every other node out of migrating #659
DatabaseBase.ApplyAllConfiguredChangesToDatabaseAsync releases the global migration lock only on its success paths. If the migration throws, the lock is left held for the life of that connection, and every other store or replica sharing the database is 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.
In the current source
src/Weasel.Core/Migrations/DatabaseBase.cs:
line 490 — ReleaseLock on the already-stamped fingerprint path
line 512 — ReleaseLock after a successful apply
line 529 — throw new InvalidOperationException("Unable to attain the global lock in time…")
line 534 — finally, which only disposes the connection
So an exception anywhere between attaining the lock and line 512 — initializeSchema, SchemaMigration.DetermineAsync, or Migrator.ApplyAllAsync — skips the release.
Why disposing the connection is not enough
It usually would be: the lock is taken with session-scoped pg_advisory_lock / pg_try_advisory_lock (Weasel.Postgresql/AdvisoryLockExtensions.cs:51,67), and Npgsql resets a pooled connection on return with DISCARD ALL, which does release session advisory locks. I measured that directly rather than assuming it:
before=1
DISCARD ALL
after=0
But the connection is only reset when it is actually returned to the pool. Observed in practice is a connection that keeps the lock while sitting in state=idle, so the release never happens on any timescale that matters to the nodes waiting.
Observed
Found while investigating JasperFx/marten#5529. Sampling pg_locks once a second through a Marten CoreTests run, filtered to Marten's apply-changes lock id (4004):
objid=4004 pid=2341 state=idle app=<none>
last query: ALTER TABLE bug_4185_….mt_events
held across 17 consecutive samples — the rest of the run. The chain was: 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 Marten's suite that looks like flaky tests; in a deployment it looks like one node's failed migration bricking the rest.
Suggested fix
Either move ReleaseLock into the existing finally (guarded on having actually attained it), or take the lock with pg_advisory_xact_lock so the server releases it on rollback without Weasel having to be careful. The transaction-scoped variant is the more robust of the two, but it changes the lock's lifetime to the transaction rather than the whole apply, so it needs checking against how the apply brackets its work.
Whichever way, a test that makes the apply throw and then asserts a second ApplyAllConfiguredChangesToDatabaseAsync on the same database can still attain the lock would pin it. A stranded lock currently produces no error on the node that stranded it, which is why this went unnoticed.
Activity
gh-659: release the global migration lock when the apply fails (#663)
9.37.0
The Weasel lock comments say Weasel releases a lock after a failed ap…
The Weasel lock comments say Weasel releases a lock after a failed ap…
The Weasel lock comments say Weasel releases a lock after a failed ap…