From 9d241b6a6c6860f491680f60bd3dc7a1b2ea2648 Mon Sep 17 00:00:00 2001 From: oppiliappan Date: Sun, 12 Oct 2025 10:36:19 +0100 Subject: [PATCH] knotserver/git: wait for git cmd to complete after streaming output Change-Id: nqqnxowuwkvzstkosmltmrpqvvrptytn could be a source of zombie processes. Signed-off-by: oppiliappan --- knotserver/git/last_commit.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/knotserver/git/last_commit.go b/knotserver/git/last_commit.go index 384239ed..1de21228 100644 --- a/knotserver/git/last_commit.go +++ b/knotserver/git/last_commit.go @@ -30,7 +30,21 @@ func init() { commitCache = cache } -func (g *GitRepo) streamingGitLog(ctx context.Context, extraArgs ...string) (io.Reader, error) { +// processReader wraps a reader and ensures the associated process is cleaned up +type processReader struct { + io.Reader + cmd *exec.Cmd + stdout io.ReadCloser +} + +func (pr *processReader) Close() error { + if err := pr.stdout.Close(); err != nil { + return err + } + return pr.cmd.Wait() +} + +func (g *GitRepo) streamingGitLog(ctx context.Context, extraArgs ...string) (io.ReadCloser, error) { args := []string{} args = append(args, "log") args = append(args, g.h.String()) @@ -48,7 +62,11 @@ func (g *GitRepo) streamingGitLog(ctx context.Context, extraArgs ...string) (io. return nil, err } - return stdout, nil + return &processReader{ + Reader: stdout, + cmd: cmd, + stdout: stdout, + }, nil } type commit struct { @@ -104,6 +122,7 @@ func (g *GitRepo) calculateCommitTime(ctx context.Context, subtree *object.Tree, if err != nil { return nil, err } + defer output.Close() // Ensure the git process is properly cleaned up reader := bufio.NewReader(output) var current commit -- 2.43.0