From 67f2fe919da5de136059f2988064981ce5544bcd Mon Sep 17 00:00:00 2001 From: Jon Johnson Date: Thu, 29 Feb 2024 10:57:01 -0800 Subject: [PATCH] Fix resource usage in melange Apparently `for { select { break } }` doesn't do what I thought. This cleans up orphaned goroutines after the reader is done. Signed-off-by: Jon Johnson --- internal/contextreader/contextreader.go | 2 +- pkg/container/docker/docker_runner.go | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/contextreader/contextreader.go b/internal/contextreader/contextreader.go index e73a4c7a4..2da52d544 100644 --- a/internal/contextreader/contextreader.go +++ b/internal/contextreader/contextreader.go @@ -49,7 +49,7 @@ func (c *contextReader) init() { c.n, c.err = c.r.Read(p) c.done <- struct{}{} case <-c.ctx.Done(): - break + return } } }() diff --git a/pkg/container/docker/docker_runner.go b/pkg/container/docker/docker_runner.go index 2ff32ef32..ee0ba7e2d 100644 --- a/pkg/container/docker/docker_runner.go +++ b/pkg/container/docker/docker_runner.go @@ -199,6 +199,8 @@ func (dk *docker) waitForCommand(ctx context.Context, r io.Reader) error { defer stderr.Close() // Wrap this in a contextReader so we respond to cancel. + ctx, cancel := context.WithCancel(ctx) + defer cancel() ctxr := contextreader.New(ctx, r) _, err := stdcopy.StdCopy(stdout, stderr, ctxr)