fix(db): prevent deadlock in concurrent melt quote locking #1474

Merged
thesimplekid merged 1 commit from fix_psgl_quote_locking into main 2025-12-30 11:41:10 +00:00
thesimplekid commented 2025-12-29 23:30:22 +00:00 (Migrated from github.com)

When multiple wallets attempt to melt quotes sharing the same request_lookup_id concurrently, the previous two-step locking approach (lock target quote, then lock all related quotes) could cause deadlocks.

Each transaction would hold its own quote's lock while waiting for locks held by other transactions, creating a circular wait condition that PostgreSQL would resolve by aborting transactions.

Fix by introducing lock_melt_quote_and_related() which acquires all related quote locks atomically in a single query, ordered by ID. This ensures consistent lock ordering across transactions, eliminating the circular wait condition.

Description


Notes to the reviewers


Suggested CHANGELOG Updates

CHANGED

ADDED

REMOVED

FIXED


Checklist

When multiple wallets attempt to melt quotes sharing the same request_lookup_id concurrently, the previous two-step locking approach (lock target quote, then lock all related quotes) could cause deadlocks. Each transaction would hold its own quote's lock while waiting for locks held by other transactions, creating a circular wait condition that PostgreSQL would resolve by aborting transactions. Fix by introducing lock_melt_quote_and_related() which acquires all related quote locks atomically in a single query, ordered by ID. This ensures consistent lock ordering across transactions, eliminating the circular wait condition. ### Description <!-- Describe the purpose of this PR, what's being adding and/or fixed --> ----- ### Notes to the reviewers <!-- In this section you can include notes directed to the reviewers, like explaining why some parts of the PR were done in a specific way --> ----- ### Suggested [CHANGELOG](https://github.com/cashubtc/cdk/blob/main/CHANGELOG.md) Updates <!-- Please do not edit the actual changelog but note what you changed here. --> #### CHANGED #### ADDED #### REMOVED #### FIXED ---- ### Checklist * [ ] I followed the [code style guidelines](https://github.com/cashubtc/cdk/blob/main/CODE_STYLE.md) * [ ] I ran `just final-check` before committing
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cashubtc/cdk!1474
No description provided.