Repository navigation
fix(database): transaction-scoped locks refuse to run outside a transaction - #685
Draft
PiquelChips wants to merge 2 commits into
Draft
PiquelChips wants to merge 2 commits into
PiquelChips wants to merge 2 commits into
Conversation
…action A repository bound to the pool or to an ExclusiveConnection took pg_advisory_xact_lock and FOR UPDATE locks that ended with their statement. The executor now tells a transaction from an exclusive connection, and one guarded helper takes every transaction-scoped advisory lock, refusing with DatabaseError::OutsideTransaction anywhere else.
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
Review follow-up: SigningKeyRepository::lock checks for a transaction before it sets the lock wait, from_transaction takes a sqlx Transaction so only one binds as a transaction, and the guard's test goes through the public accessors, so it also proves an ExclusiveConnection's handles refuse.
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.
Part of #544 (NEW-16).
A transaction-scoped advisory lock (
pg_advisory_xact_lock) taken outside a transaction ends with its own statement, so it protects nothing. Two locks ran on whatever the repository was bound to:AlertRepository::currentandOidcRepository::lock_related_origins. The signing keys'require_transactionrefused only the pool, not anExclusiveConnection, whose statements also each commit alone. Refresh redemption'sFOR UPDATEguard had the same gap.Now the executor tells a transaction (
RepositoryExecutor::Transaction) from an exclusive connection (RepositoryExecutor::Exclusive). Inside the crate,from_transactiontakes asqlx::Transaction, so only a transaction can bind as one. One guarded helper,RepositoryExecutor::lock/try_lock, takes every transaction-scoped advisory lock, and refuses with a newDatabaseError::OutsideTransactionbefore it runs any query.run!(@on executor, …)lets the helper run its query with the same span as every other query.What changes
Only misuse changes. Every caller in
auth-servicealready runs these in a transaction, so nothing visible changes for them.ExclusiveConnectionalerts().current(..)Err(DatabaseError::OutsideTransaction)oidc().lock_related_origins()Err(DatabaseError::OutsideTransaction)signing_keys().lock()/try_lock()on the poolDatabaseError::Operation("the signing key lock requires a transaction")DatabaseError::OutsideTransactionsigning_keys().lock()/try_lock()on anExclusiveConnectionDatabaseError::OutsideTransactiontokens().redeem_refresh(..)on the poolOperation("refresh redemption requires a transaction")OutsideTransactiontokens().redeem_refresh(..)on anExclusiveConnectionOutsideTransactionThe new variant's message, if it is ever logged:
the operation's locks need a transaction, and none is open. ThroughApiErrorandOAuthErrorit is an internal 500, the same asOperationwas.Files outside the database crate
Adding the variant means updating the exhaustive
DatabaseErrormatches. Each one gets the new variant in its internal-error arm, and nothing else changes:apps/auth-service/src/oidc/error.rsapps/auth-service/src/passkey/mod.rs(refactor(auth): the dashboard's name and handle fields take their limits from the rules #674 also edits this file, in other lines)apps/auth-service/src/security/mod.rscrates/database/src/repositories/tokens/mod.rsis also edited by #667, in other lines.Tests
repositories::tests::transaction_scoped_locks_need_a_transaction. Through the public accessors,database.oidc().lock_related_origins()andexclusive.oidc().lock_related_origins()refuse withOutsideTransaction, and a transaction takes the lock. So it also proves that anExclusiveConnection's handles bind asExclusive. The exclusive connection holds a new test-only key,LockKey::GuardTest = -5.from_connection→from_transaction(tokens/tests.rs,users/tests.rs,emails/tests.rs).the_lock_excludes_and_one_key_per_type_signsnow checks that the pool is refused withOutsideTransactionexactly, not just any error.Unchanged
.sqlxcache has no diff.lock_timeouthandling.SigningKeyRepository::lockchecks for a transaction before its first statement, so a refusal runs no query.run!body.Checks
cargo clippy --locked --all-targets -- -D warnings, with and without--all-features, forsourceorigin-repositoryandauth-serviceRUSTDOCFLAGS='-D warnings' cargo doc --locked --no-deps --all-features -p sourceorigin-repositoryjust db test --test-threads 2: 510 passed, with the query cache checkjust fmtNo published crate is touched, so there is no semver check.
Note
Written by Claude Opus 5.5 in Claude Code and reviewed by Claude Opus 5.5 (via T3 Code)