From c5b289603650ae820917b322463e3d20ff7c410f Mon Sep 17 00:00:00 2001 From: beerosagos Date: Wed, 7 May 2025 16:03:47 +0200 Subject: [PATCH] backend/account: fix bugs in account discovery Recent commit 726289c7eedbd5157cf5f514cca4e4ce2cc4e9b3 made the account Initialization asynchronous, moving the `account.ensureAddresses()` call on a separate thread. This caused a regression inside `backend.checkAccountUsed()` where the method wasn't waiting for the complete account synchronization before marking it as used. This commit fixes the issue waiting for account sync before checking if it used. This also fixes the `backend.Accounts()` method, that was returning the original `backend.accounts` object instead of a copy. This caused the caller to read the account list without a proper lock, causing weird behaviors during account discovery. --- backend/accounts.go | 10 +++++----- backend/backend.go | 3 ++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/backend/accounts.go b/backend/accounts.go index a06caa724f..e8d2e3900c 100644 --- a/backend/accounts.go +++ b/backend/accounts.go @@ -750,12 +750,16 @@ func (backend *Backend) addAccount(account accounts.Interface) { }) if event.Subject == string(accountsTypes.EventSyncDone) { backend.notifyNewTxs(account) + go backend.checkAccountUsed(account) } }) + if err := account.Initialize(); err != nil { + backend.log.WithError(err).Error("error initializing account") + return + } if backend.onAccountInit != nil { backend.onAccountInit(account) } - go backend.checkAccountUsed(account) } // The accountsAndKeystoreLock must be held when calling this function. @@ -1529,10 +1533,6 @@ func (backend *Backend) checkAccountUsed(account accounts.Interface) { } } log := backend.log.WithField("accountCode", account.Config().Config.Code) - if err := account.Initialize(); err != nil { - log.WithError(err).Error("error initializing account") - return - } txs, err := account.Transactions() if err != nil { log.WithError(err).Error("discoverAccount") diff --git a/backend/backend.go b/backend/backend.go index 03c23c8aec..bd928dcf53 100644 --- a/backend/backend.go +++ b/backend/backend.go @@ -22,6 +22,7 @@ import ( "net/url" "os" "path/filepath" + "slices" "strings" "time" @@ -590,7 +591,7 @@ func (backend *Backend) Testing() bool { // Accounts returns the current accounts of the backend. func (backend *Backend) Accounts() AccountsList { defer backend.accountsAndKeystoreLock.RLock()() - return backend.accounts + return slices.Clone(backend.accounts) } // KeystoreTotalAmount represents the total balance amount of the accounts belonging to a keystore.