knotserver/git: wait for git cmd to complete after streaming output #659

merged
opened by oppi.li targeting master from push-nqqnxowuwkvz

could be a source of zombie processes.

Signed-off-by: oppiliappan me@oppi.li

Changed files
+21 -2
knotserver
+21 -2
knotserver/git/last_commit.go
···
commitCache = cache
}
-
func (g *GitRepo) streamingGitLog(ctx context.Context, extraArgs ...string) (io.Reader, error) {
args := []string{}
args = append(args, "log")
args = append(args, g.h.String())
···
return nil, err
}
-
return stdout, nil
}
type commit struct {
···
if err != nil {
return nil, err
}
reader := bufio.NewReader(output)
var current commit
···
commitCache = cache
}
+
// 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())
···
return nil, err
}
+
return &processReader{
+
Reader: stdout,
+
cmd: cmd,
+
stdout: stdout,
+
}, nil
}
type commit struct {
···
if err != nil {
return nil, err
}
+
defer output.Close() // Ensure the git process is properly cleaned up
reader := bufio.NewReader(output)
var current commit