From 1f0914e2df98226a78f655943bf28d06f46c9106 Mon Sep 17 00:00:00 2001 From: Tim Beatham Date: Thu, 4 Jan 2024 00:23:20 +0000 Subject: [PATCH] bugfix-node-not-leaving - Add lock when perform synchronisation on concurrent access --- pkg/conn/cluster.go | 5 ++--- pkg/mesh/manager.go | 3 ++- pkg/sync/syncer.go | 3 +++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/pkg/conn/cluster.go b/pkg/conn/cluster.go index d3d241b..5a08f70 100644 --- a/pkg/conn/cluster.go +++ b/pkg/conn/cluster.go @@ -23,9 +23,10 @@ func binarySearch(global []string, selfId string, groupSize int) (int, int) { lower := 0 higher := len(global) - 1 - mid := (lower + higher) / 2 for (higher+1)-lower > groupSize { + mid := (lower + higher) / 2 + if global[mid] < selfId { lower = mid + 1 } else if global[mid] > selfId { @@ -33,8 +34,6 @@ func binarySearch(global []string, selfId string, groupSize int) (int, int) { } else { break } - - mid = (lower + higher) / 2 } return lower, int(math.Min(float64(lower+groupSize), float64(len(global)))) diff --git a/pkg/mesh/manager.go b/pkg/mesh/manager.go index 226706f..59c9132 100644 --- a/pkg/mesh/manager.go +++ b/pkg/mesh/manager.go @@ -10,6 +10,7 @@ import ( "github.com/tim-beatham/smegmesh/pkg/conf" "github.com/tim-beatham/smegmesh/pkg/ip" "github.com/tim-beatham/smegmesh/pkg/lib" + logging "github.com/tim-beatham/smegmesh/pkg/log" "github.com/tim-beatham/smegmesh/pkg/wg" "golang.zx2c4.com/wireguard/wgctrl" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" @@ -355,7 +356,7 @@ func (s *MeshManagerImpl) LeaveMesh(meshId string) error { err := mesh.RemoveNode(s.HostParameters.GetPublicKey()) if err != nil { - return err + logging.Log.WriteErrorf(err.Error()) } if s.OnDelete != nil { diff --git a/pkg/sync/syncer.go b/pkg/sync/syncer.go index 359abc7..c14e10a 100644 --- a/pkg/sync/syncer.go +++ b/pkg/sync/syncer.go @@ -28,6 +28,7 @@ type SyncerImpl struct { cluster conn.ConnCluster conf *conf.DaemonConfiguration lastSync map[string]int64 + lock sync.RWMutex } // Sync: Sync with random nodes @@ -134,7 +135,9 @@ func (s *SyncerImpl) Sync(correspondingMesh mesh.MeshProvider) error { correspondingMesh.SaveChanges() + s.lock.Lock() s.lastSync[correspondingMesh.GetMeshId()] = time.Now().Unix() + s.lock.Unlock() return nil }