fix: race condition on updating account.Name during handle BackupData

This commit is contained in:
frank 2024-03-05 21:03:10 +08:00
parent 6e44165b51
commit deb56c2c81
2 changed files with 26 additions and 5 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/status-im/status-go/images" "github.com/status-im/status-go/images"
"github.com/status-im/status-go/multiaccounts/errors" "github.com/status-im/status-go/multiaccounts/errors"
"github.com/status-im/status-go/multiaccounts/settings"
"github.com/status-im/status-go/protocol/identity" "github.com/status-im/status-go/protocol/identity"
"github.com/status-im/status-go/protocol/protobuf" "github.com/status-im/status-go/protocol/protobuf"
v1protocol "github.com/status-im/status-go/protocol/v1" v1protocol "github.com/status-im/status-go/protocol/v1"
@ -220,6 +221,14 @@ func (m *Messenger) handleBackedUpSettings(message *protobuf.SyncSetting) error
if settingField != nil { if settingField != nil {
if message.GetType() == protobuf.SyncSetting_PREFERRED_NAME && message.GetValueString() != "" { if message.GetType() == protobuf.SyncSetting_PREFERRED_NAME && message.GetValueString() != "" {
displayNameClock, err := m.settings.GetSettingLastSynced(settings.DisplayName)
if err != nil {
m.logger.Warn("failed to get last synced clock for display name", zap.Error(err))
return nil
}
// there is a race condition between display name and preferred name on updating m.account.Name, so we need to check the clock
// there is also a similar check within SaveSyncDisplayName
if displayNameClock < message.GetClock() {
m.account.Name = message.GetValueString() m.account.Name = message.GetValueString()
err = m.multiAccounts.SaveAccount(*m.account) err = m.multiAccounts.SaveAccount(*m.account)
if err != nil { if err != nil {
@ -227,6 +236,7 @@ func (m *Messenger) handleBackedUpSettings(message *protobuf.SyncSetting) error
return nil return nil
} }
} }
}
if m.config.messengerSignalsHandler != nil { if m.config.messengerSignalsHandler != nil {
response := wakusync.WakuBackedUpDataResponse{ response := wakusync.WakuBackedUpDataResponse{

View File

@ -106,9 +106,20 @@ func (m *Messenger) SaveSyncDisplayName(displayName string, clock uint64) error
return err return err
} }
preferredNameClock, err := m.settings.GetSettingLastSynced(settings.PreferredName)
if err != nil {
return err
}
// When either the display name or preferred name changes, m.account.Name should be updated.
// However, a race condition can occur during BackupData, where m.account.Name could be incorrectly updated.
// The final value of m.account.Name depending on which backup message(BackedUpProfile/BackedUpSettings) arrives later.
// So we should check the clock of the preferred name and only update m.account.Name if it's older than the display name.
if preferredNameClock < clock {
m.account.Name = displayName m.account.Name = displayName
return m.multiAccounts.SaveAccount(*m.account) return m.multiAccounts.SaveAccount(*m.account)
} }
return nil
}
func ValidateBio(bio *string) error { func ValidateBio(bio *string) error {
if len(*bio) > maxBioLength { if len(*bio) > maxBioLength {