Skip to content

Roll back a take that lost the first insert of a lock name - #3210

Merged
pournasserian merged 1 commit into
mainfrom
3200-lock-insert-rollback
Nov 16, 2026
Merged

pournasserian merged 1 commit into
mainfrom
3200-lock-insert-rollback

Conversation

@pournasserian

Copy link
Copy Markdown
Member

Closes #3200

Since #3182 the take of the row lock runs in an explicit transaction. On the first use of a name, takes that start together race on the insert of its row. A loser's insert fails with a unique violation, and in catch (DbUpdateException) it called ExistsAsync, which reads cms_lock on another connection while the loser's own transaction is still open. On the in-memory SQLite database of FluentCmsTestHost (shared cache), that open transaction holds the write lock, so the read got SQLITE_LOCKED and retried for the 30-second command timeout. Meanwhile other takes overshot their waits and the holder's own write failed with database table is locked, which was the 500 of the plugin template's PostsSentTogetherAddNoMoreNotesThanTheSettingsAllow in the Templates job. The take now rolls its transaction back before it reads the row, as SaveChangesAsync did before #3182. The backend spec's "Multi-instance as built" paragraph gets one sentence on it. The preview release notes do not describe the lock change of #3182, so they are unchanged.

What I checked in DatabaseDistributedLock, by reading it:

  • No other path reads or writes on another connection or scope while a take's or a renewal's transaction is open. The ExecuteUpdateAsync path of the take and RenewAsync's update run on the transaction's own context. When either throws, await using disposes the transaction (a rollback) before the caller's catch, and IsBusy only inspects the exception. RenewAsync's fallback read runs after its transaction is disposed, on the same context. ReleaseAsync opens no explicit transaction, and the handle waits for its renewal loop to end before it releases. TryAcquireAsync only waits after a null take and touches no database.
  • PostgreSQL: a failed statement puts the transaction block in an aborted state. The docs give ROLLBACK TO a savepoint, "short of rolling it back completely", as the way out of it (tutorial: transactions), so the rollback is allowed and needed, and a read in the same transaction would fail.
  • SQL Server: with SET XACT_ABORT OFF, the default and not changed anywhere in the code, a failed statement can roll back only that statement and leave the transaction open (SET XACT_ABORT), so Rollback ends a pending transaction. SqlTransaction.Rollback throws InvalidOperationException only when the server already rolled the transaction back (SqlTransaction.Rollback), which a unique violation does not do with XACT_ABORT OFF.

Measured by the diagnosing lane (the generated Acme.ReviewNotes test, 20 runs per build under load):

Build Failed (500) Race 30 s stall Most runs took
7b6cd7e, before #3182 0 / 20 18 0 10–25 s
7a085f3 (#3182) 9 / 20 18 18 37–41 s
c0f6d5e (tip of main) 7 / 20 19 19 37–54 s
tip plus the rollback 0 / 20 20 0 8–14 s

Measured here:

Run Result
New test TakesThatRaceOnTheFirstUseOfANameEndWithinTheirTimeout, without the rollback 30 / 30 failed, all on the elapsed time (test took 36–66 s)
The same test with the rollback 0 / 30 failed (each process 8–12 s, host start included)
DistributedLockTests with the rollback, SQLite 15 / 15 passed
FluentCMS.Data.Tests Migrations namespace with the rollback, PostgreSQL 15 143 passed, 46 skipped, 0 failed
Acme.ReviewNotes PostsSentTogetherAddNoMoreNotesThanTheSettingsAllow from packages of this branch, as the Templates job makes it 0 / 20 failed, 6.7–15.5 s per run

The new test runs ten takes with a 500 ms wait on a name never used before, on the in-memory database. Exactly one holds the lock: the lease is 30 seconds on a wall clock that does not move, and nobody releases before the count.

Dependencies

None. #3195 follows this pull request and merges main after it.

Acceptance

  • The cause is named in this issue, with the host's exception
  • The test passes repeated on main, in CI's Templates job, and the fix has a test in the repository's own tests if the cause is in the product (the regression test is in DistributedLockTests, and the generated test passed 20 runs locally; CI's Templates job on main is not verified yet)
  • If the row lock changed what a busy SQLite database answers to a request, the specification says what it answers now

@pournasserian pournasserian added type:bug Defect area:storage EF Core model, providers and migrations labels Nov 16, 2026
@pournasserian
pournasserian merged commit 60edb85 into main Nov 16, 2026
33 of 51 checks passed
@pournasserian
pournasserian deleted the 3200-lock-insert-rollback branch November 16, 2026 11:29
pournasserian added a commit that referenced this pull request Nov 16, 2026
…3216)

Brings `main` at f8ff094 into `feature/a1`, as a merge commit, so that the feature branch is tested with what `main` gained since the last sync:

- #3210: a take of the row lock that loses the first insert of a name rolls back before it reads the row (the `Templates` failure of #3200).
- #3208: the delivery cache tests deliver the grant's eviction before the shared-store reads (#3201).
- #3199: a confirmed logout revokes the authorizations of the session it ends (#3198).

One conflict, in the "Multi-instance as built" paragraph of `docs/planning/backend-technical-spec.md`: `main`'s text is taken, and the feature branch's own change to the paragraph on the commands in the reference host is kept.

With #3199 in, `docs/decisions/0007-admin-sign-in-flow.md` says what a logout ends: an unconfirmed one leaves the server's session and its tokens; a confirmed one ends the session and revokes every authorization issued in it; a sign-in from before #3198 and a third-party client's consent are not ended.

## Dependencies

None.

Part of #3152.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:storage EF Core model, providers and migrations type:bug Defect

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The plugin template's concurrent posts test fails on main with a 500 after 31 seconds

1 participant