Skip to content

Commit 4ec3654

Browse files
ethantkoenigappleboy
authored andcommitted
Kill zombie subprocesses in GetCommitsInfo (go-gitea#101)
1 parent 1aa1c8c commit 4ec3654

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

commit_info.go

+12
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ func getCommitsInfo(state *getCommitsInfoState) error {
207207
if err := cmd.Start(); err != nil {
208208
return err
209209
}
210+
// it's okay to ignore the error returned by cmd.Wait(); we expect the
211+
// subprocess to sometimes have a non-zero exit status, since we may
212+
// prematurely close stdout, resulting in a broken pipe.
213+
defer cmd.Wait()
210214

211215
numThreads := runtime.NumCPU()
212216
done := make(chan error, numThreads)
@@ -216,6 +220,14 @@ func getCommitsInfo(state *getCommitsInfoState) error {
216220

217221
scanner := bufio.NewScanner(readCloser)
218222
err = state.processGitLogOutput(scanner)
223+
224+
// it is important that we close stdout here; if we do not close
225+
// stdout, the subprocess will keep running, and the deffered call
226+
// cmd.Wait() may block for a long time.
227+
if closeErr := readCloser.Close(); closeErr != nil && err == nil {
228+
err = closeErr
229+
}
230+
219231
for i := 0; i < numThreads; i++ {
220232
doneErr := <-done
221233
if doneErr != nil && err == nil {

0 commit comments

Comments
 (0)