Skip to content

Commit

Permalink
ui/page: fix concurrent map write error
Browse files Browse the repository at this point in the history
Signed-off-by: Philemon Ukane <[email protected]>
  • Loading branch information
ukane-philemon committed Jul 9, 2024
1 parent 2499445 commit a6c7153
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions ui/page/components/wallet_sync_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@ type WalletSyncInfo struct {
syncSwitch *cryptomaterial.Switch
toBackup cryptomaterial.Button

isStatusConnected bool
reload Reload
backup func(sharedW.Asset)
ForwardButton cryptomaterial.IconButton
syncProgressInfoMutex sync.Mutex
isStatusConnected bool
reload Reload
backup func(sharedW.Asset)
ForwardButton cryptomaterial.IconButton

IsSlider bool
}

// SyncProgressInfo is made independent of the walletInfo struct so that once
// set with a value, it always persists till unset. This will help address the
// progress bar issue where, changing UI pages alters the progress on the sync
// status progress percentage. Stores sharedW.Asset : ProgressInfo.
var syncProgressInfo = sync.Map{}

type ProgressInfo struct {
remainingSyncTime string
HeadersToFetchOrScan int32
Expand All @@ -45,12 +50,6 @@ type ProgressInfo struct {

type Reload func()

// SyncProgressInfo is made independent of the walletInfo struct so that once
// set with a value, it always persists till unset. This will help address the
// progress bar issue where, changing UI pages alters the progress on the sync
// status progress percentage.
var syncProgressInfo = map[sharedW.Asset]ProgressInfo{}

func NewWalletSyncInfo(l *load.Load, wallet sharedW.Asset, reload Reload, backup func(sharedW.Asset)) *WalletSyncInfo {
wsi := &WalletSyncInfo{
Load: l,
Expand Down Expand Up @@ -495,20 +494,21 @@ func (wsi *WalletSyncInfo) layoutAutoSyncSection(gtx C) D {
}

func (wsi *WalletSyncInfo) FetchSyncProgress() ProgressInfo {
pgrss, ok := syncProgressInfo[wsi.wallet]
pgrss, ok := syncProgressInfo.Load(wsi.wallet)
if !ok {
pgrss = ProgressInfo{}
return ProgressInfo{}
}

// remove the unnecessary sync progress data if already synced.
wsi.deleteSyncProgress()
return pgrss
return pgrss.(ProgressInfo)
}

// deleteSyncProgress removes the map entry after the data persisted is no longer necessary.
func (wsi *WalletSyncInfo) deleteSyncProgress() {
wal := wsi.wallet
if wal.IsSynced() {
delete(syncProgressInfo, wal)
syncProgressInfo.Delete(wal)
}
}

Expand Down Expand Up @@ -543,12 +543,8 @@ func (wsi *WalletSyncInfo) ListenForNotifications() {
// headers to fetch cannot be less than the previously fetched.
// Page refresh only needed if there is new data to update the UI.
if progress.HeadersToFetchOrScan >= previousProgress.HeadersToFetchOrScan {
// Lock the map before updating it
wsi.syncProgressInfoMutex.Lock()
// Ensure the mutex is unlocked even if there is a panic
defer wsi.syncProgressInfoMutex.Unlock()
// set the new progress against the associated asset.
syncProgressInfo[wsi.wallet] = progress
syncProgressInfo.Store(wsi.wallet, progress)

// We only care about sync state changes here, to
// refresh the window display.
Expand Down

0 comments on commit a6c7153

Please sign in to comment.