src/server/pkg/exec/exec.go
func (c *closeOnce) safeClose() error {
	c.writers.Lock()
	err := c.Close()
	c.writers.Unlock()
	return err
}
func (c *closeOnce) safeClose() error {
	c.writers.Lock()
	err := c.Close()
	c.writers.Unlock()
	return err
}
func ( c * closeOnce ) safeClose ( ) error { c . writers . Lock ( ) err := c . Close ( ) c . writers . Unlock ( ) return err }
// safeClose closes c being careful not to race with any calls to c.Write.
// See golang.org/issue/9307 and TestEchoFileRace in exec_test.go.
// In theory other calls could also be excluded (by writing appropriate
// wrappers like c.Write's implementation below), but since c is most
// commonly used as a WriteCloser, Write is the main one to worry about.
// See also #7970, for which this is a partial fix for this specific instance.
// The idea is that we return a WriteCloser, and so the caller can be
// relied upon not to call Write and Close simultaneously, but it's less
// obvious that cmd.Wait calls Close and that the caller must not call
// Write and cmd.Wait simultaneously. In fact that seems too onerous.
// So we change the use of Close in cmd.Wait to use safeClose, which will
// synchronize with any Write.
//
// It's important that we know this won't block forever waiting for the
// operations being excluded. At the point where this is called,
// the invoked command has exited and the parent copy of the read side
// of the pipe has also been closed, so there should really be no read side
// of the pipe left. Any active writes should return very shortly with an EPIPE,
// making it reasonable to wait for them.
// Technically it is possible that the child forked a sub-process or otherwise
// handed off the read side of the pipe before exiting and the current holder
// is not reading from the pipe, and the pipe is full, in which case the close here
// might block waiting for the write to complete. That's probably OK.
// It's a small enough problem to be outweighed by eliminating the race here.
safeClose closes c being careful not to race with any calls to c . Write . See golang . org / issue / 9307 and TestEchoFileRace in exec_test . go . In theory other calls could also be excluded ( by writing appropriate wrappers like c . Write s implementation below ) but since c is most commonly used as a WriteCloser Write is the main one to worry about . See also #7970 for which this is a partial fix for this specific instance . The idea is that we return a WriteCloser and so the caller can be relied upon not to call Write and Close simultaneously but it s less obvious that cmd . Wait calls Close and that the caller must not call Write and cmd . Wait simultaneously . In fact that seems too onerous . So we change the use of Close in cmd . Wait to use safeClose which will synchronize with any Write . It s important that we know this won t block forever waiting for the operations being excluded . At the point where this is called the invoked command has exited and the parent copy of the read side of the pipe has also been closed so there should really be no read side of the pipe left . Any active writes should return very shortly with an EPIPE making it reasonable to wait for them . Technically it is possible that the child forked a sub - process or otherwise handed off the read side of the pipe before exiting and the current holder is not reading from the pipe and the pipe is full in which case the close here might block waiting for the write to complete . That s probably OK . It s a small enough problem to be outweighed by eliminating the race here .