Skip to content

Commit

Permalink
encoding/gob: avoid race on idToType
Browse files Browse the repository at this point in the history
Fixes #23328

Change-Id: Ie4864d7f388d363860318fe41431d8a9719e9a75
Reviewed-on: https://go-review.googlesource.com/86075
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
  • Loading branch information
ianlancetaylor committed Jan 4, 2018
1 parent 43bf63f commit 1a9f27d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cmd/dist/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ func (t *tester) runFlag(rx string) string {
func (t *tester) raceTest(dt *distTest) error {
t.addCmd(dt, "src", t.goTest(), "-race", "-i", "runtime/race", "flag", "os", "os/exec")
t.addCmd(dt, "src", t.goTest(), "-race", t.runFlag("Output"), "runtime/race")
t.addCmd(dt, "src", t.goTest(), "-race", t.runFlag("TestParse|TestEcho|TestStdinCloseRace|TestClosedPipeRace"), "flag", "os", "os/exec")
t.addCmd(dt, "src", t.goTest(), "-race", t.runFlag("TestParse|TestEcho|TestStdinCloseRace|TestClosedPipeRace|TestTypeRace"), "flag", "os", "os/exec", "encoding/gob")
// We don't want the following line, because it
// slows down all.bash (by 10 seconds on my laptop).
// The race builder should catch any error here, but doesn't.
Expand Down
2 changes: 2 additions & 0 deletions src/encoding/gob/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,8 @@ func (dec *Decoder) compatibleType(fr reflect.Type, fw typeId, inProgress map[re

// typeString returns a human-readable description of the type identified by remoteId.
func (dec *Decoder) typeString(remoteId typeId) string {
typeLock.Lock()
defer typeLock.Unlock()
if t := idToType[remoteId]; t != nil {
// globally known type.
return t.string()
Expand Down
42 changes: 42 additions & 0 deletions src/encoding/gob/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package gob
import (
"bytes"
"reflect"
"sync"
"testing"
)

Expand Down Expand Up @@ -218,3 +219,44 @@ func TestStressParallel(t *testing.T) {
<-c
}
}

// Issue 23328. Note that this test name is known to cmd/dist/test.go.
func TestTypeRace(t *testing.T) {
c := make(chan bool)
var wg sync.WaitGroup
for i := 0; i < 2; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
var buf bytes.Buffer
enc := NewEncoder(&buf)
dec := NewDecoder(&buf)
var x interface{}
switch i {
case 0:
x = &N1{}
case 1:
x = &N2{}
default:
t.Errorf("bad i %d", i)
return
}
m := make(map[string]string)
<-c
if err := enc.Encode(x); err != nil {
t.Error(err)
return
}
if err := enc.Encode(x); err != nil {
t.Error(err)
return
}
if err := dec.Decode(&m); err == nil {
t.Error("decode unexpectedly succeeded")
return
}
}(i)
}
close(c)
wg.Wait()
}

0 comments on commit 1a9f27d

Please sign in to comment.