Skip to content

fix: prevent deadlocks #770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions internal/storage/ledger/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ledger
import (
"context"
"math/big"
"slices"
"strings"

"github.com/formancehq/go-libs/v2/platform/postgres"
Expand Down Expand Up @@ -49,6 +50,21 @@ func (store *Store) GetBalances(ctx context.Context, query ledgercontroller.Bala
}
}

// prevent deadlocks by sorting the accountsVolumes slice
slices.SortStableFunc(accountsVolumes, func(i, j AccountsVolumesWithLedger) int {
if i.Account < j.Account {
return -1
} else if i.Account > j.Account {
return 1
} else if i.Asset < j.Asset {
return -1
} else if i.Asset > j.Asset {
return 1
} else {
return 0
}
})

// Try to insert volumes using last move (to keep compat with previous version) or 0 values.
// This way, if the account has a 0 balance at this point, it will be locked as any other accounts.
// If the complete sql transaction fails, the account volumes will not be inserted.
Expand Down
Loading