diff --git a/internal/integrations/v4/runner/runner.go b/internal/integrations/v4/runner/runner.go index c66e7c7e7..9b3f708b3 100644 --- a/internal/integrations/v4/runner/runner.go +++ b/internal/integrations/v4/runner/runner.go @@ -226,6 +226,7 @@ func (r *runner) execute(ctx context.Context, matches *databind.Values, discover var act contexts.Actuator ctx, act = contexts.WithHeartBeat(ctx, def.Timeout, r.log) r.setHeartBeat(act.HeartBeat) + defer act.HeartBeatStop() } // add hostID in the context to fetch and set in executor diff --git a/pkg/helpers/contexts/heartbeat.go b/pkg/helpers/contexts/heartbeat.go index df7d83acc..d16fa51d9 100644 --- a/pkg/helpers/contexts/heartbeat.go +++ b/pkg/helpers/contexts/heartbeat.go @@ -24,14 +24,18 @@ type Actuator struct { // Cancel cancels the context Cancel context.CancelFunc // HeartBeat extends the context life time by the value the context was created with - HeartBeat func() + HeartBeat func() + HeartBeatStop func() } // WithHeartBeat with return a context that is automatically cancelled if the HeartBeat function // from the returned Actuator is not invoked periodically before the passed timeout expires. func WithHeartBeat(parent context.Context, timeout time.Duration, lg log.Entry) (context.Context, Actuator) { ctx := heartBeatCtx{lifeTime: timeout} - actuator := Actuator{HeartBeat: ctx.heartBeat} + actuator := Actuator{ + HeartBeat: ctx.heartBeat, + HeartBeatStop: ctx.heartBeatStop, + } ctx.Context, actuator.Cancel = context.WithCancel(parent) ctx.timer = time.AfterFunc(timeout, func() { lg.Warnf("HeartBeat timeout exceeded after %f seconds", timeout.Seconds()) @@ -48,3 +52,9 @@ func (ctx *heartBeatCtx) heartBeat() { } ctx.timer.Reset(ctx.lifeTime) } + +func (ctx *heartBeatCtx) heartBeatStop() { + ctx.mutex.Lock() + defer ctx.mutex.Unlock() + ctx.timer.Stop() +}