From fa11bf9e57a68e3b5252386fdf44042dd672949a Mon Sep 17 00:00:00 2001 From: Erik Sipsma Date: Sun, 30 Jul 2023 11:42:48 -0700 Subject: [PATCH] executor: fix resource sampler goroutine leak Before this, the runc executor did not close the cgroupRecord when the container exited non-zero, which resulted in goroutines leaking. Signed-off-by: Erik Sipsma --- executor/resources/monitor.go | 7 +++++++ executor/resources/types/types.go | 1 + executor/runcexecutor/executor.go | 3 +++ 3 files changed, 11 insertions(+) diff --git a/executor/resources/monitor.go b/executor/resources/monitor.go index 78438b26df76..95b954bcbe6f 100644 --- a/executor/resources/monitor.go +++ b/executor/resources/monitor.go @@ -56,6 +56,10 @@ func (r *cgroupRecord) Start() { r.closeSampler = s.Close } +func (r *cgroupRecord) Close() { + r.close() +} + func (r *cgroupRecord) CloseAsync(next func(context.Context) error) error { go func() { r.close() @@ -160,6 +164,9 @@ func (r *nopRecord) Samples() (*types.Samples, error) { return nil, nil } +func (r *nopRecord) Close() { +} + func (r *nopRecord) CloseAsync(next func(context.Context) error) error { return next(context.TODO()) } diff --git a/executor/resources/types/types.go b/executor/resources/types/types.go index 8ef316a167e3..9bac557e2160 100644 --- a/executor/resources/types/types.go +++ b/executor/resources/types/types.go @@ -9,6 +9,7 @@ import ( type Recorder interface { Start() + Close() CloseAsync(func(context.Context) error) error Wait() error Samples() (*Samples, error) diff --git a/executor/runcexecutor/executor.go b/executor/runcexecutor/executor.go index 9c12996e53da..e804ee850b28 100644 --- a/executor/runcexecutor/executor.go +++ b/executor/runcexecutor/executor.go @@ -335,6 +335,9 @@ func (w *runcExecutor) Run(ctx context.Context, id string, root executor.Mount, err = exitError(ctx, err) if err != nil { + if rec != nil { + rec.Close() + } releaseContainer(context.TODO()) return nil, err }