1
0
mirror of https://github.com/golang/go synced 2024-11-19 15:44:44 -07:00

encoding/gob: avoid race on idToType

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>
This commit is contained in:
Ian Lance Taylor 2018-01-03 17:50:47 -08:00
parent 43bf63fce1
commit 1a9f27d503
3 changed files with 45 additions and 1 deletions

View File

@ -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.

View File

@ -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()

View File

@ -7,6 +7,7 @@ package gob
import (
"bytes"
"reflect"
"sync"
"testing"
)
@ -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()
}