aboutsummaryrefslogtreecommitdiff
path: root/src/os/exec
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2016-11-15 17:55:28 -0800
committerIan Lance Taylor <iant@golang.org>2016-11-16 02:24:30 +0000
commitb906df653b58bc2ab9b93e18f62adccc8c1419b7 (patch)
treefa1a7a507fcaea633dc0c66348ea3fe0df942613 /src/os/exec
parent1b66b38e25af567aa30a7b22581c05285be3564a (diff)
downloadgo-b906df653b58bc2ab9b93e18f62adccc8c1419b7.tar.xz
os/exec: add closeOnce.WriteString method
Add an explicit WriteString method to closeOnce that acquires the writers lock. This overrides the one promoted from the embedded *os.File field. The promoted one naturally does not acquire the lock, and can therefore race with the Close method. Fixes #17647. Change-Id: I3460f2a0d503449481cfb2fd4628b4855ab0ecdf Reviewed-on: https://go-review.googlesource.com/33298 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/os/exec')
-rw-r--r--src/os/exec/exec.go7
-rw-r--r--src/os/exec/exec_test.go26
2 files changed, 33 insertions, 0 deletions
diff --git a/src/os/exec/exec.go b/src/os/exec/exec.go
index a3a0f20ebc..c4c5168b98 100644
--- a/src/os/exec/exec.go
+++ b/src/os/exec/exec.go
@@ -579,6 +579,13 @@ func (c *closeOnce) Write(b []byte) (int, error) {
return n, err
}
+func (c *closeOnce) WriteString(s string) (int, error) {
+ c.writers.RLock()
+ n, err := c.File.WriteString(s)
+ c.writers.RUnlock()
+ return n, err
+}
+
// StdoutPipe returns a pipe that will be connected to the command's
// standard output when the command starts.
//
diff --git a/src/os/exec/exec_test.go b/src/os/exec/exec_test.go
index 67fe14faae..d3ac7ab4b9 100644
--- a/src/os/exec/exec_test.go
+++ b/src/os/exec/exec_test.go
@@ -246,6 +246,32 @@ func TestStdinClose(t *testing.T) {
check("Wait", cmd.Wait())
}
+// Issue 17647.
+func TestStdinCloseRace(t *testing.T) {
+ cmd := helperCommand(t, "stdinClose")
+ stdin, err := cmd.StdinPipe()
+ if err != nil {
+ t.Fatalf("StdinPipe: %v", err)
+ }
+ if err := cmd.Start(); err != nil {
+ t.Fatalf("Start: %v", err)
+ }
+ go func() {
+ if err := cmd.Process.Kill(); err != nil {
+ t.Errorf("Kill: %v", err)
+ }
+ }()
+ go func() {
+ io.Copy(stdin, strings.NewReader(stdinCloseTestString))
+ if err := stdin.Close(); err != nil {
+ t.Errorf("stdin.Close: %v", err)
+ }
+ }()
+ if err := cmd.Wait(); err == nil {
+ t.Fatalf("Wait: succeeded unexpectedly")
+ }
+}
+
// Issue 5071
func TestPipeLookPathLeak(t *testing.T) {
fd0, lsof0 := numOpenFDS(t)