Skip to content

Commit

Permalink
Fix Manager.Run stop deadlock
Browse files Browse the repository at this point in the history
Fix open-telemetry#1218

Close probes in a different goroutine than the one draining their event
channel.
  • Loading branch information
MrAlias committed Oct 25, 2024
1 parent 098f594 commit dc4f5f7
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions internal/pkg/instrumentation/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,25 +277,30 @@ func (m *Manager) Run(ctx context.Context, target *process.TargetDetails) error

go m.ConfigLoop(ctx)

for {
select {
case <-ctx.Done():
m.probeMu.Lock()
done := make(chan error, 1)
go func() {
defer close(done)
<-ctx.Done()

m.logger.Debug("Shutting down all probes")
err := m.cleanup(target)
m.probeMu.Lock()

// Wait for all probes to stop before closing the chan they send on.
m.runningProbesWG.Wait()
close(m.eventCh)
m.logger.Debug("Shutting down all probes")
err := m.cleanup(target)

m.state = managerStateStopped
m.probeMu.Unlock()
return errors.Join(err, ctx.Err())
case e := <-m.eventCh:
m.otelController.Trace(e)
}
// Wait for all probes to stop before closing the chan they send on.
m.runningProbesWG.Wait()
close(m.eventCh)

m.state = managerStateStopped
m.probeMu.Unlock()

done <- errors.Join(err, ctx.Err())
}()

for e := range m.eventCh {
m.otelController.Trace(e)
}
return <-done
}

func (m *Manager) load(target *process.TargetDetails) error {
Expand Down

0 comments on commit dc4f5f7

Please sign in to comment.