Repository navigation
fix(pairing): take the write lock before reading expired pairings - #55
Conversation
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.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
POST /v1/pairingsometimes 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_expiredstarted its transaction with aSELECTof the expired IDs and only then deleted them, andcreatecalls it first. In a deferred SQLite transaction that has already read, the upgrade to a write lock returnsSQLITE_BUSYimmediately when another connection is writing. SQLite doesn't call the busy handler there.Fix:
delete_expirednow deletes withRETURNING idfirst 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 ofimmediate_transactionbecause diesel-async'sAsyncFuncbound 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_sinceonly reads.Test:
create_waits_for_a_concurrent_writerholdsBEGIN IMMEDIATEon a second connection to a WAL file database and checks thatcreatewaits 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.