From 736443c13a718f0a9c30327ebbf09f58ccbe6d49 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Mon, 17 Oct 2016 17:20:48 -0400 Subject: os/exec: allow simultaneous cmd.Wait and Write of cmd.StdinPipe cmd.StdinPipe returns an io.WriteCloser. It's reasonable to expect the caller not to call Write and Close simultaneously, but there is an implicit Close in cmd.Wait that's not obvious. We already synchronize the implicit Close in cmd.Wait against any explicit Close from the caller. Also synchronize that implicit Close against any explicit Write from the caller. Fixes #9307. Change-Id: I8561e9369d6e5ac88dfbca1175549f6dfa04b8ac Reviewed-on: https://go-review.googlesource.com/31148 Reviewed-by: Brad Fitzpatrick Reviewed-by: Ian Lance Taylor --- src/os/exec/exec_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/os/exec/exec_test.go') diff --git a/src/os/exec/exec_test.go b/src/os/exec/exec_test.go index 8d44401d0e..b14343752a 100644 --- a/src/os/exec/exec_test.go +++ b/src/os/exec/exec_test.go @@ -101,6 +101,26 @@ func TestCatStdin(t *testing.T) { } } +func TestEchoFileRace(t *testing.T) { + cmd := helperCommand(t, "echo") + stdin, err := cmd.StdinPipe() + if err != nil { + t.Fatalf("StdinPipe: %v", err) + } + if err := cmd.Start(); err != nil { + t.Fatalf("Start: %v", err) + } + wrote := make(chan bool) + go func() { + defer close(wrote) + fmt.Fprint(stdin, "echo\n") + }() + if err := cmd.Wait(); err != nil { + t.Fatalf("Wait: %v", err) + } + <-wrote +} + func TestCatGoodAndBadFile(t *testing.T) { // Testing combined output and error values. bs, err := helperCommand(t, "cat", "/bogus/file.foo", "exec_test.go").CombinedOutput() -- cgit v1.3