diff --git a/.chloggen/fixchannel.yaml b/.chloggen/fixchannel.yaml new file mode 100755 index 00000000000..e6868473943 --- /dev/null +++ b/.chloggen/fixchannel.yaml @@ -0,0 +1,11 @@ +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: service + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Stop notification for signals before shutdown, increase channel size. + +# One or more tracking issues or pull requests related to the change +issues: [6522] diff --git a/service/collector.go b/service/collector.go index 4250e9874a5..c1fca67170a 100644 --- a/service/collector.go +++ b/service/collector.go @@ -107,10 +107,12 @@ func New(set CollectorSettings) (*Collector, error) { } return &Collector{ - set: set, - state: atomic.NewInt32(int32(StateStarting)), - shutdownChan: make(chan struct{}), - signalsChannel: make(chan os.Signal, 1), + set: set, + state: atomic.NewInt32(int32(StateStarting)), + shutdownChan: make(chan struct{}), + // Per signal.Notify documentation, a size of the channel equaled with + // the number of signals getting notified on is recommended. + signalsChannel: make(chan os.Signal, 3), asyncErrorChannel: make(chan error), }, nil } @@ -194,6 +196,7 @@ func (col *Collector) Run(ctx context.Context) error { // Always notify with SIGHUP for configuration reloading. signal.Notify(col.signalsChannel, syscall.SIGHUP) + defer signal.Stop(col.signalsChannel) // Only notify with SIGTERM and SIGINT if graceful shutdown is enabled. if !col.set.DisableGracefulShutdown { @@ -208,7 +211,6 @@ LOOP: col.service.telemetrySettings.Logger.Error("Config watch failed", zap.Error(err)) break LOOP } - if err = col.reloadConfiguration(ctx); err != nil { return err } @@ -217,20 +219,17 @@ LOOP: break LOOP case s := <-col.signalsChannel: col.service.telemetrySettings.Logger.Info("Received signal from OS", zap.String("signal", s.String())) - switch s { - case syscall.SIGHUP: - if err := col.reloadConfiguration(ctx); err != nil { - return err - } - default: + if s != syscall.SIGHUP { break LOOP } + if err := col.reloadConfiguration(ctx); err != nil { + return err + } case <-col.shutdownChan: col.service.telemetrySettings.Logger.Info("Received shutdown request") break LOOP case <-ctx.Done(): col.service.telemetrySettings.Logger.Info("Context done, terminating process", zap.Error(ctx.Err())) - // Call shutdown with background context as the passed in context has been canceled return col.shutdown(context.Background()) }