Skip to content

Commit ff6f6b5

Browse files
committed
prevent a data race caused by unprotected access to testing.*.Log
The following commit golang/go@5c83e65 which was released in go 1.7, fixes a possible data race that can happen during testing. The fix is in common.flushToParent which is now used to protect read access to `output`.
1 parent 636041e commit ff6f6b5

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ func newSession(t *testing.T) (io.ReadWriteCloser, *server) {
3333
rs, wc := io.Pipe()
3434
rc, ws := io.Pipe()
3535

36-
rws := &logIO{t, "server", &pipe{r: rs, w: ws}}
37-
rwc := &logIO{t, "client", &pipe{r: rc, w: wc}}
36+
rws := &logIO{t: t, prefix: "server", proxy: &pipe{r: rs, w: ws}}
37+
rwc := &logIO{t: t, prefix: "client", proxy: &pipe{r: rc, w: wc}}
3838

3939
server := server{
4040
T: t,

integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1708,7 +1708,7 @@ func integrationURLFromEnv() string {
17081708

17091709
func loggedConnection(t *testing.T, conn *Connection, name string) *Connection {
17101710
if name != "" {
1711-
conn.conn = &logIO{t, name, conn.conn}
1711+
conn.conn = &logIO{t: t, prefix: name, proxy: conn.conn}
17121712
}
17131713
return conn
17141714
}

shared_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,14 @@ func (p *pipe) Close() error {
4848
type logIO struct {
4949
t *testing.T
5050
prefix string
51-
proxy io.ReadWriteCloser
51+
52+
m sync.Mutex
53+
proxy io.ReadWriteCloser
5254
}
5355

5456
func (me *logIO) Read(p []byte) (n int, err error) {
57+
me.m.Lock()
58+
defer me.m.Unlock()
5559
me.t.Logf("%s reading %d\n", me.prefix, len(p))
5660
n, err = me.proxy.Read(p)
5761
if err != nil {
@@ -64,6 +68,8 @@ func (me *logIO) Read(p []byte) (n int, err error) {
6468
}
6569

6670
func (me *logIO) Write(p []byte) (n int, err error) {
71+
me.m.Lock()
72+
defer me.m.Unlock()
6773
me.t.Logf("%s writing %d\n", me.prefix, len(p))
6874
n, err = me.proxy.Write(p)
6975
if err != nil {
@@ -76,6 +82,8 @@ func (me *logIO) Write(p []byte) (n int, err error) {
7682
}
7783

7884
func (me *logIO) Close() (err error) {
85+
me.m.Lock()
86+
defer me.m.Unlock()
7987
err = me.proxy.Close()
8088
if err != nil {
8189
me.t.Logf("%s close : %v\n", me.prefix, err)

0 commit comments

Comments
 (0)