fix: Copy replicas to prevent race (#4596)

This was seen in https://github.com/coder/coder/actions/runs/3267638198/jobs/5373066836
This commit is contained in:
Kyle Carberry
2022-10-17 14:22:54 -05:00
committed by GitHub
parent 618c6dcaa4
commit fda71dadcb

View File

@ -315,7 +315,14 @@ func (m *Manager) Self() database.Replica {
func (m *Manager) All() []database.Replica {
m.mutex.Lock()
defer m.mutex.Unlock()
return append(m.peers[:], m.self)
replicas := make([]database.Replica, 0, len(m.peers))
for _, replica := range append(m.peers, m.self) {
// When we assign the non-pointer to a
// variable it loses the reference.
replica := replica
replicas = append(replicas, replica)
}
return replicas
}
// Regional returns all replicas in the same region excluding itself.