Skip to content

Commit e04877a

Browse files
authored
fix: race conditions in replicasync (#5289)
1 parent 9cfdbec commit e04877a

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

enterprise/replicasync/replicasync.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ func (m *Manager) syncReplicas(ctx context.Context) error {
276276
return xerrors.Errorf("ping database: %w", err)
277277
}
278278

279+
m.mutex.Lock()
280+
defer m.mutex.Unlock()
279281
replica, err := m.db.UpdateReplica(ctx, database.UpdateReplicaParams{
280282
ID: m.self.ID,
281283
UpdatedAt: database.Now(),
@@ -291,8 +293,6 @@ func (m *Manager) syncReplicas(ctx context.Context) error {
291293
if err != nil {
292294
return xerrors.Errorf("update replica: %w", err)
293295
}
294-
m.mutex.Lock()
295-
defer m.mutex.Unlock()
296296
if m.self.Error != replica.Error {
297297
// Publish an update occurred!
298298
err = m.pubsub.Publish(PubsubEvent, []byte(m.self.ID.String()))

enterprise/replicasync/replicasync_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,12 @@ func TestReplica(t *testing.T) {
206206
_ = server.Close()
207207
})
208208
done := false
209+
210+
var m sync.Mutex
209211
server.SetCallback(func() {
212+
m.Lock()
213+
defer m.Unlock()
214+
210215
if len(server.All()) != count {
211216
return
212217
}

0 commit comments

Comments
 (0)