fix: race conditions in replicasync (#5289)

This commit is contained in:
Marcin Tojek
2022-12-05 17:18:15 +01:00
committed by GitHub
parent 9cfdbec2ef
commit e04877a638
2 changed files with 7 additions and 2 deletions
+2 -2
View File
@@ -276,6 +276,8 @@ func (m *Manager) syncReplicas(ctx context.Context) error {
return xerrors.Errorf("ping database: %w", err)
}
m.mutex.Lock()
defer m.mutex.Unlock()
replica, err := m.db.UpdateReplica(ctx, database.UpdateReplicaParams{
ID: m.self.ID,
UpdatedAt: database.Now(),
@@ -291,8 +293,6 @@ func (m *Manager) syncReplicas(ctx context.Context) error {
if err != nil {
return xerrors.Errorf("update replica: %w", err)
}
m.mutex.Lock()
defer m.mutex.Unlock()
if m.self.Error != replica.Error {
// Publish an update occurred!
err = m.pubsub.Publish(PubsubEvent, []byte(m.self.ID.String()))
@@ -206,7 +206,12 @@ func TestReplica(t *testing.T) {
_ = server.Close()
})
done := false
var m sync.Mutex
server.SetCallback(func() {
m.Lock()
defer m.Unlock()
if len(server.All()) != count {
return
}