diff --git a/protocol/messenger_backup_handler.go b/protocol/messenger_backup_handler.go index f7b3db1b8..e7464b8d2 100644 --- a/protocol/messenger_backup_handler.go +++ b/protocol/messenger_backup_handler.go @@ -7,6 +7,7 @@ import ( "github.com/status-im/status-go/images" "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/protobuf" v1protocol "github.com/status-im/status-go/protocol/v1" @@ -220,12 +221,21 @@ func (m *Messenger) handleBackedUpSettings(message *protobuf.SyncSetting) error if settingField != nil { if message.GetType() == protobuf.SyncSetting_PREFERRED_NAME && message.GetValueString() != "" { - m.account.Name = message.GetValueString() - err = m.multiAccounts.SaveAccount(*m.account) + displayNameClock, err := m.settings.GetSettingLastSynced(settings.DisplayName) if err != nil { - m.logger.Warn("[handleBackedUpSettings] failed to save account", zap.Error(err)) + 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() + err = m.multiAccounts.SaveAccount(*m.account) + if err != nil { + m.logger.Warn("[handleBackedUpSettings] failed to save account", zap.Error(err)) + return nil + } + } } if m.config.messengerSignalsHandler != nil { diff --git a/protocol/messenger_identity.go b/protocol/messenger_identity.go index 93c46058e..52c2b1f0a 100644 --- a/protocol/messenger_identity.go +++ b/protocol/messenger_identity.go @@ -106,8 +106,19 @@ func (m *Messenger) SaveSyncDisplayName(displayName string, clock uint64) error return err } - m.account.Name = displayName - return m.multiAccounts.SaveAccount(*m.account) + 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 + return m.multiAccounts.SaveAccount(*m.account) + } + return nil } func ValidateBio(bio *string) error {