fix: race condition with empty PreferredName (status-desktop/14053) (#4981)

This commit is contained in:
Igor Sirotin 2024-03-25 15:43:45 +00:00 committed by GitHub
parent 7f44d4d12a
commit 0d9b6138d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 2 deletions

View File

@ -106,15 +106,22 @@ func (m *Messenger) SaveSyncDisplayName(displayName string, clock uint64) error
return err return err
} }
preferredName, err := m.settings.GetPreferredUsername()
if err != nil {
return err
}
preferredNameClock, err := m.settings.GetSettingLastSynced(settings.PreferredName) preferredNameClock, err := m.settings.GetSettingLastSynced(settings.PreferredName)
if err != nil { if err != nil {
return err return err
} }
// When either the display name or preferred name changes, m.account.Name should be updated. // 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. // 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. // The final value of m.account.Name depends 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. // 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 { // Yet even if the preferred name clock is older, but the preferred name was empty, we should still update m.account.Name.
if preferredNameClock < clock || preferredName == "" {
m.account.Name = displayName m.account.Name = displayName
return m.multiAccounts.SaveAccount(*m.account) return m.multiAccounts.SaveAccount(*m.account)
} }