diff --git a/dev/sg/go.mod b/dev/sg/go.mod index 2c9a000d99b..5533a096d16 100644 --- a/dev/sg/go.mod +++ b/dev/sg/go.mod @@ -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 diff --git a/dev/sg/go.sum b/dev/sg/go.sum index 6bfa27dc957..0144a0d9647 100644 --- a/dev/sg/go.sum +++ b/dev/sg/go.sum @@ -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= diff --git a/dev/sg/internal/run/command.go b/dev/sg/internal/run/command.go index 776ce80899a..84eb27a9f66 100644 --- a/dev/sg/internal/run/command.go +++ b/dev/sg/internal/run/command.go @@ -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 } diff --git a/lib/process/pipe.go b/lib/process/pipe.go index 0bbf96abf39..8347ed521ec 100644 --- a/lib/process/pipe.go +++ b/lib/process/pipe.go @@ -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) {