Skip to content

Commit c89d9f1

Browse files
authored
Fix race conditions (#1094)
1 parent 7579f83 commit c89d9f1

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

internal/collector/otel_collector_plugin.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,16 @@ func (oc *Collector) processReceivers(ctx context.Context, receivers []config.Ot
161161
}
162162
}
163163

164+
// nolint: revive, cyclop
164165
func (oc *Collector) bootup(ctx context.Context) error {
165166
errChan := make(chan error)
166167

167168
go func() {
169+
if oc.service == nil {
170+
errChan <- fmt.Errorf("unable to start OTel collector: service is nil")
171+
return
172+
}
173+
168174
appErr := oc.service.Run(ctx)
169175
if appErr != nil {
170176
errChan <- appErr
@@ -177,8 +183,11 @@ func (oc *Collector) bootup(ctx context.Context) error {
177183
case err := <-errChan:
178184
return err
179185
default:
180-
state := oc.service.GetState()
186+
if oc.service == nil {
187+
return fmt.Errorf("unable to start otel collector: service is nil")
188+
}
181189

190+
state := oc.service.GetState()
182191
switch state {
183192
case otelcol.StateStarting:
184193
// NoOp
@@ -212,9 +221,9 @@ func (oc *Collector) Close(ctx context.Context) error {
212221
oc.service.Shutdown()
213222
oc.cancel()
214223

215-
settings := oc.config.Client.Backoff
224+
settings := *oc.config.Client.Backoff
216225
settings.MaxElapsedTime = maxTimeToWaitForShutdown
217-
err := backoff.WaitUntil(ctx, settings, func() error {
226+
err := backoff.WaitUntil(ctx, &settings, func() error {
218227
if oc.service.GetState() == otelcol.StateClosed {
219228
return nil
220229
}

internal/file/file_manager_service.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,10 @@ func (fms *FileManagerService) UpdateOverview(
178178
return response, nil
179179
}
180180

181+
backoffSettings := fms.agentConfig.Client.Backoff
181182
response, err := backoff.RetryWithData(
182183
sendUpdateOverview,
183-
backoffHelpers.Context(backOffCtx, fms.agentConfig.Client.Backoff),
184+
backoffHelpers.Context(backOffCtx, backoffSettings),
184185
)
185186
if err != nil {
186187
return err

internal/watcher/instance/instance_watcher_service.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,9 @@ func (iw *InstanceWatcherService) checkForUpdates(
223223
iw.sendNginxConfigContextUpdate(newCtx, nginxConfigContext)
224224
iw.nginxConfigCache[nginxConfigContext.InstanceID] = nginxConfigContext
225225
proto.UpdateNginxInstanceRuntime(newInstance, nginxConfigContext)
226+
iw.cacheMutex.Lock()
226227
iw.instanceCache[newInstance.GetInstanceMeta().GetInstanceId()] = newInstance
228+
iw.cacheMutex.Unlock()
227229
}
228230
}
229231
}

0 commit comments

Comments
 (0)