sg: Fix deadlock when command fails to install/run (#24654)

This commit is contained in:
Thorsten Ball 2021-09-06 18:14:34 +02:00 committed by GitHub
parent dd601b549e
commit 67882dc058
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 4 deletions

View File

@ -13,7 +13,7 @@ require (
github.com/jackc/pgx/v4 v4.11.0
github.com/peterbourgon/ff/v3 v3.0.0
github.com/rjeczalik/notify v0.9.2
github.com/sourcegraph/sourcegraph/lib v0.0.0-00010101000000-000000000000
github.com/sourcegraph/sourcegraph/lib v0.0.0-20210906140940-dd601b549e29
golang.org/x/mod v0.4.2
golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
@ -22,4 +22,4 @@ require (
gopkg.in/yaml.v2 v2.4.0
)
replace github.com/sourcegraph/sourcegraph/lib v0.0.0-00010101000000-000000000000 => ./../../lib
replace github.com/sourcegraph/sourcegraph/lib => ./../../lib

View File

@ -655,6 +655,8 @@ github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJ
github.com/sourcegraph/batch-change-utils v0.0.0-20210708162152-c9f35b905d94/go.mod h1:kKNRPN6dxuXwC4UUhPVcAJsvz4EVEfI0jyN5HUJsazA=
github.com/sourcegraph/go-diff v0.6.1/go.mod h1:iBszgVvyxdc8SFZ7gm69go2KDdt3ag071iBaWPF6cjs=
github.com/sourcegraph/jsonx v0.0.0-20200629203448-1a936bd500cf/go.mod h1:ppFaPm6kpcHnZGqQTFhUIAQRIEhdQDWP1PCv4/ON354=
github.com/sourcegraph/sourcegraph/lib v0.0.0-20210906140940-dd601b549e29 h1:sfaIvdvMeSFu2+KHKUIx7MSiVXLr14+N+xpA2dyJ5wc=
github.com/sourcegraph/sourcegraph/lib v0.0.0-20210906140940-dd601b549e29/go.mod h1:ZK8mHKWdapYI8iIQwsO7QUxf44RjiNGKkOZa9aYIjTc=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=

View File

@ -135,7 +135,7 @@ func startCmd(ctx context.Context, dir string, cmd Command, globalEnv map[string
stderrWriter = io.MultiWriter(logger, sc.stderrBuf)
}
wg, err := process.PipeOutput(sc.Cmd, stdoutWriter, stderrWriter)
wg, err := process.PipeOutput(ctx, sc.Cmd, stdoutWriter, stderrWriter)
if err != nil {
return nil, err
}

View File

@ -2,6 +2,7 @@ package process
import (
"bufio"
"context"
"fmt"
"io"
"os/exec"
@ -15,7 +16,7 @@ import (
// WaitGroup after waiting for the *exec.Cmd to finish.
//
// See this issue for more details: https://github.com/golang/go/issues/21922
func PipeOutput(c *exec.Cmd, stdoutWriter, stderrWriter io.Writer) (*sync.WaitGroup, error) {
func PipeOutput(ctx context.Context, c *exec.Cmd, stdoutWriter, stderrWriter io.Writer) (*sync.WaitGroup, error) {
stdoutPipe, err := c.StdoutPipe()
if err != nil {
return nil, err
@ -26,6 +27,16 @@ func PipeOutput(c *exec.Cmd, stdoutWriter, stderrWriter io.Writer) (*sync.WaitGr
return nil, err
}
go func() {
// We start a goroutine here to make sure that our pipes are closed
// when the context is canceled.
//
// See enterprise/cmd/executor/internal/command/run.go for more details.
<-ctx.Done()
stdoutPipe.Close()
stderrPipe.Close()
}()
wg := &sync.WaitGroup{}
readIntoBuf := func(w io.Writer, r io.Reader) {