diff --git a/client/internal/clientstate.go b/client/internal/clientstate.go index 95a04f81..7b67bdb2 100644 --- a/client/internal/clientstate.go +++ b/client/internal/clientstate.go @@ -32,6 +32,7 @@ var ( // It is safe to call methods of this struct concurrently. type ClientSyncedState struct { mutex sync.Mutex + packageSyncMutex sync.Mutex agentDescription *protobufs.AgentDescription health *protobufs.AgentHealth diff --git a/client/internal/packagessyncer.go b/client/internal/packagessyncer.go index 8f76a2cc..7a03a05b 100644 --- a/client/internal/packagessyncer.go +++ b/client/internal/packagessyncer.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "net/http" + "sync" "github.com/open-telemetry/opamp-go/client/types" "github.com/open-telemetry/opamp-go/protobufs" @@ -58,7 +59,7 @@ func (s *packagesSyncer) Sync(ctx context.Context) error { } // Now do the actual syncing in the background. - go s.doSync(ctx) + go s.doSync(ctx, &s.clientSyncedState.packageSyncMutex) return nil } @@ -98,7 +99,7 @@ func (s *packagesSyncer) initStatuses() error { } // doSync performs the actual syncing process. -func (s *packagesSyncer) doSync(ctx context.Context) { +func (s *packagesSyncer) doSync(ctx context.Context, mutex *sync.Mutex) { hash, err := s.localState.AllPackagesHash() if err != nil { s.logger.Errorf("Package syncing failed: %V", err) @@ -109,6 +110,9 @@ func (s *packagesSyncer) doSync(ctx context.Context) { return } + (*mutex).Lock() + defer (*mutex).Unlock() + failed := false if err := s.deleteUnneededLocalPackages(); err != nil { s.logger.Errorf("Cannot delete unneeded packages: %v", err)