Skip to content

fix(database): transaction-scoped locks refuse to run outside a transaction - #685

Draft
PiquelChips wants to merge 2 commits into
mainfrom
fix/guarded-advisory-locks
Draft

PiquelChips wants to merge 2 commits into
mainfrom
fix/guarded-advisory-locks

Conversation

@PiquelChips

@PiquelChips PiquelChips commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

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::current and OidcRepository::lock_related_origins. The signing keys' require_transaction refused only the pool, not an ExclusiveConnection, whose statements also each commit alone. Refresh redemption's FOR UPDATE guard had the same gap.

Now the executor tells a transaction (RepositoryExecutor::Transaction) from an exclusive connection (RepositoryExecutor::Exclusive). Inside the crate, from_transaction takes a sqlx::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 new DatabaseError::OutsideTransaction before 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-service already runs these in a transaction, so nothing visible changes for them.

Called on the pool or an ExclusiveConnection Before After
alerts().current(..) took a lock that ended at once Err(DatabaseError::OutsideTransaction)
oidc().lock_related_origins() took a lock that ended at once Err(DatabaseError::OutsideTransaction)
signing_keys().lock() / try_lock() on the pool DatabaseError::Operation("the signing key lock requires a transaction") DatabaseError::OutsideTransaction
signing_keys().lock() / try_lock() on an ExclusiveConnection took a lock that ended at once DatabaseError::OutsideTransaction
tokens().redeem_refresh(..) on the pool Operation("refresh redemption requires a transaction") OutsideTransaction
tokens().redeem_refresh(..) on an ExclusiveConnection ran with row locks that ended per statement OutsideTransaction

The new variant's message, if it is ever logged: the operation's locks need a transaction, and none is open. Through ApiError and OAuthError it is an internal 500, the same as Operation was.

Files outside the database crate

Adding the variant means updating the exhaustive DatabaseError matches. Each one gets the new variant in its internal-error arm, and nothing else changes:

crates/database/src/repositories/tokens/mod.rs is also edited by #667, in other lines.

Tests

  • New: repositories::tests::transaction_scoped_locks_need_a_transaction. Through the public accessors, database.oidc().lock_related_origins() and exclusive.oidc().lock_related_origins() refuse with OutsideTransaction, and a transaction takes the lock. So it also proves that an ExclusiveConnection's handles bind as Exclusive. The exclusive connection holds a new test-only key, LockKey::GuardTest = -5.
  • Renamed in tests, with no change to what they test: from_connection → from_transaction (tokens/tests.rs, users/tests.rs, emails/tests.rs).
  • No test was removed. the_lock_excludes_and_one_key_per_type_signs now checks that the pool is refused with OutsideTransaction exactly, not just any error.

Unchanged

  • The SQL text of both lock queries, so the .sqlx cache has no diff.
  • Lock order, and the signing keys' lock_timeout handling. SigningKeyRepository::lock checks for a transaction before its first statement, so a refusal runs no query.
  • The spans: the helper's queries go through the same run! body.

Checks

  • cargo clippy --locked --all-targets -- -D warnings, with and without --all-features, for sourceorigin-repository and auth-service
  • RUSTDOCFLAGS='-D warnings' cargo doc --locked --no-deps --all-features -p sourceorigin-repository
  • just db test --test-threads 2: 510 passed, with the query cache check
  • just fmt

No 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)

…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.
@PiquelChips PiquelChips added bug Something isn't working schema-change Any changes to the database schema labels Nov 22, 2026
@github-project-automation github-project-automation Bot moved this to Needs Triage in sourceorigin.dev Nov 22, 2026
@coderabbitai

coderabbitai Bot commented Nov 22, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

@PiquelChips PiquelChips added apps/auth-service Origin Auth at auth.sourceorigin.dev, its settings and developer console, and the operator CLI and removed schema-change Any changes to the database schema labels Nov 22, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

apps/auth-service Origin Auth at auth.sourceorigin.dev, its settings and developer console, and the operator CLI bug Something isn't working

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

1 participant