reflect: fix race condition on funcTypes

CL 425314 made creating funcTypes using StructOf, and using a mutex to
protect read+write to funcTypes. However, after initializing funcTypes,
it is accessed in FuncOf without holding lock, causing a race.

Fixing it by returning the n-th Type directly from initFuncTypes, so the
accessing funcTypes will always be guarded by a mutex.

Fixes #56011

Change-Id: I1b50d1ae342943f16f368b8606f2614076dc90fb
Reviewed-on: https://go-review.googlesource.com/c/go/+/437997
Reviewed-by: Keith Randall <khr@google.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
Cuong Manh Le 2022-10-03 23:46:13 +07:00 committed by Gopher Robot
parent 12daabb915
commit 6d8ec89303
2 changed files with 29 additions and 17 deletions

View file

@ -15,6 +15,7 @@ import (
"io"
"math"
"math/rand"
"net"
"os"
. "reflect"
"reflect/internal/example1"
@ -8240,3 +8241,20 @@ func TestValue_Equal(t *testing.T) {
}
}
}
func TestInitFuncTypes(t *testing.T) {
n := 100
var wg sync.WaitGroup
wg.Add(n)
for i := 0; i < n; i++ {
go func() {
defer wg.Done()
ipT := TypeOf(net.IP{})
for i := 0; i < ipT.NumMethod(); i++ {
_ = ipT.Method(i)
}
}()
}
wg.Wait()
}