Commit 1a9f27d5 authored by Ian Lance Taylor's avatar Ian Lance Taylor

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: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: 's avatarRob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 43bf63fc
......@@ -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.
......
......@@ -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()
......
......@@ -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()
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment