mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-11 05:06:51 +00:00
Other: Fix race condition when changing address mode
When changing address mode, we would close all a user's update channels and create them from scratch. This involved setting user.updateCh to a new value. However, it was possible for other goroutines to read from user.updateCh during this time. I replaced it with a call to user.updateCh.Clear(), which is threadsafe.
This commit is contained in:
@ -238,24 +238,22 @@ func (user *User) SetAddressMode(ctx context.Context, mode vault.AddressMode) er
|
||||
}
|
||||
})
|
||||
|
||||
updateCh := make(map[string]*queue.QueuedChannel[imap.Update])
|
||||
user.updateCh.Clear()
|
||||
|
||||
switch mode {
|
||||
case vault.CombinedMode:
|
||||
primaryUpdateCh := queue.NewQueuedChannel[imap.Update](0, 0)
|
||||
|
||||
user.apiAddrs.IterKeys(func(addrID string) {
|
||||
updateCh[addrID] = primaryUpdateCh
|
||||
user.updateCh.Set(addrID, primaryUpdateCh)
|
||||
})
|
||||
|
||||
case vault.SplitMode:
|
||||
user.apiAddrs.IterKeys(func(addrID string) {
|
||||
updateCh[addrID] = queue.NewQueuedChannel[imap.Update](0, 0)
|
||||
user.updateCh.Set(addrID, queue.NewQueuedChannel[imap.Update](0, 0))
|
||||
})
|
||||
}
|
||||
|
||||
user.updateCh = safe.NewMapFrom(updateCh, nil)
|
||||
|
||||
if err := user.vault.SetAddressMode(mode); err != nil {
|
||||
return fmt.Errorf("failed to set address mode: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user