Skip to content

Commit

Permalink
fix: race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
olli-holmala committed Jun 12, 2024
1 parent 40e8128 commit 3fc280a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ __debug_bin

vendor/
go.work
go.work.sum
go.work.sum

# Apple's random filesystem file.
.DS_Store
37 changes: 19 additions & 18 deletions internal/collector/otel_collector_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,43 +63,44 @@ func (oc *Collector) Init(ctx context.Context, mp bus.MessagePipeInterface) erro
}

go func() {
runErr := oc.run(runCtx)
if runErr != nil {
bootErr := oc.bootup(runCtx)
if bootErr != nil {
slog.ErrorContext(runCtx, "error", err)
}
}()

return nil
}

func (oc *Collector) run(ctx context.Context) error {
var err error
func (oc *Collector) bootup(ctx context.Context) error {
oc.appDone = make(chan struct{})
errChan := make(chan error)

go func() {
defer close(oc.appDone)
appErr := oc.service.Run(ctx)
if appErr != nil {
err = appErr
errChan <- appErr
}
}()

for {
state := oc.service.GetState()
// While waiting for collector start, an error was found. Most likely
// an invalid custom collector configuration file.
if err != nil {
select {
case err := <-errChan:
return err
}

switch state {
case otelcol.StateStarting:
// NoOp
case otelcol.StateRunning:
return nil
case otelcol.StateClosing, otelcol.StateClosed:
default:
err = fmt.Errorf("unable to start, otelcol state is %d", state)
state := oc.service.GetState()

switch state {
case otelcol.StateStarting:
// NoOp
continue
case otelcol.StateRunning:
return nil
case otelcol.StateClosing, otelcol.StateClosed:
default:
return fmt.Errorf("unable to start, otelcol state is %s", state)
}
}
}
}
Expand Down

0 comments on commit 3fc280a

Please sign in to comment.