Skip to content

fix(pairing): take the write lock before reading expired pairings - #55

Merged
D3SOX merged 1 commit into
mainfrom
fix/immediate-transactions
Dec 3, 2026
Merged

D3SOX merged 1 commit into
mainfrom
fix/immediate-transactions

Conversation

@D3SOX

@D3SOX D3SOX commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

POST /v1/pairing sometimes fails with a 503 ("database is locked") after about 0.2 s, even though the busy timeout is 30 s. The expired-pairing cleanup fails the same way.

Cause: pairing::delete_expired started its transaction with a SELECT of the expired IDs and only then deleted them, and create calls it first. In a deferred SQLite transaction that has already read, the upgrade to a write lock returns SQLITE_BUSY immediately when another connection is writing. SQLite doesn't call the busy handler there.

Fix: delete_expired now deletes with RETURNING id first and then removes the pending account sessions, so every pairing transaction writes before it reads. This works on SQLite and PostgreSQL. I used this instead of immediate_transaction because diesel-async's AsyncFunc bound is private, so a generic per-backend wrapper isn't possible.

Audit: every other write transaction already starts with a write (the account row lock, lock_account_quota, or a direct insert, update or delete). encrypted_sync::read_since only reads.

Test: create_waits_for_a_concurrent_writer holds BEGIN IMMEDIATE on a second connection to a WAL file database and checks that create waits instead of failing, then succeeds after the commit. Before the fix it failed right away. cargo test --workspace --locked (108 passed), clippy and fmt are clean, and the PostgreSQL build compiles.


Made by Claude Opus 5.5 in Claude Code.

View guided diff Turn on auto-fix

SQLite returns SQLITE_BUSY immediately, without the busy timeout, when a
deferred transaction that already read tries to write while another
connection holds the write lock. Pairing creation and the expired-pairing
cleanup read the expired IDs first, so they failed with 'database is
locked' under concurrent sync writes. Delete with RETURNING first instead.
@sourcery-ai

sourcery-ai Bot commented Dec 3, 2026

Copy link
Copy Markdown

Sorry @D3SOX, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 8 hours and 22 minutes by commenting @sourcery-ai review. Upgrade to get a review now.

@coderabbitai

coderabbitai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change updates expired pairing cleanup to return the deleted pairing count and adds a SQLite test for pairing creation while another transaction holds a write lock.

Changes: Pairing database

Layer / File(s) Summary
Expired pairing cleanup
src/database/pairing.rs
delete_expired deletes expired pairings with RETURNING, removes pending account sessions for those pairings, and returns the number of deleted pairing IDs.
SQLite writer-wait test
src/database/pairing.rs
A new test checks that create remains pending while an immediate write transaction is held and returns CreateResult::Created after that transaction commits.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: Minimal · up to b864f

This change makes pairing creation and expired-pairing cleanup wait for a concurrent writer instead of failing with a database-locked error. No merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: delete_expired now takes the write lock before reading expired pairings, which matches the raw summary (DELETE ... RETURNING before the session cleanup) and the PR obj…
Description check Passed The description states the 503 'database is locked' symptom, the cause (a deferred SQLite transaction that reads before it writes, and the busy handler not being invoked on upgrade), the fix (DELETE .…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: delete_expired now takes the write lock before reading expired pairings, which matches the raw summary (DELETE ... RETURNING before the session cleanup) and the PR objectives. It is specific and under the length norm, and it uses the conventional commit prefix.

Full details: Description check

Explanation

The description states the 503 'database is locked' symptom, the cause (a deferred SQLite transaction that reads before it writes, and the busy handler not being invoked on upgrade), the fix (DELETE ... RETURNING first), the rejected alternative (immediate_transaction), an audit of other write transactions, and the new test. These all relate directly to the changeset described in the raw summary.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • 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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 08ee9ca0-cef1-4e3f-aaeb-87b95a47f517
Commits

Reviewing files that changed from the base of the PR and between aef58fb and b864f48.

Files selected for processing: 1

  • src/database/pairing.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@D3SOX
D3SOX merged commit bb285a7 into main Dec 3, 2026
19 checks passed
@D3SOX
D3SOX deleted the fix/immediate-transactions branch December 3, 2026 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant