Fix/accounts transaction integrity#248
Open
Kenshiin13 wants to merge 8 commits into
Open
Conversation
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.
Description
The account money functions had several ways to lose or duplicate money when a query failed partway through, all rooted in how transactions were opened, committed, and cleaned up. This fixes them across
server/accounts/db.ts,server/groups/db.ts, andserver/db/index.ts.What was wrong
Balance changes and their ledger rows weren't atomic.
UpdateBalanceupdated the balance and inserted the ledger row as two separate autocommitted statements. If the ledger insert failed, the balance had already changed and committed while the function returned failure.Invoice payments moved money in separate steps.
UpdateInvoicedebited one account and credited the other independently, then marked the invoice paid in a third statement. A failure between any of them lost money or left a paid invoice unflagged.Transactions committed and rolled back without awaiting.
PerformTransaction,DepositMoney, andWithdrawMoneyleaned on connection disposal to commit, and disposal fired the commit without awaiting it before releasing the connection back to the pool. The rollbacks had the same race.Disposal committed whatever was still open. If a function threw mid-transaction, the connection wrapper committed the half-finished transaction on disposal instead of rolling it back.
What changed
UpdateBalancewraps its balance update and ledger insert in one transaction.DepositMoney,WithdrawMoney,PerformTransaction, andInsertGroupcommit explicitly instead of relying on disposal.await usingso this runs as async cleanup (server/db/index.ts).UpdateInvoiceandPerformTransactionshare oneApplyTransferhelper, so the invoice's money move and its paid flag commit together in a single transaction:The invoice ledger is now a single transfer row and one
ox:transferredMoneyevent, same as a normal transfer.ox:invoicePaidstill fires.