FAQ
RWMutex?
On Mon, Oct 8, 2012 at 5:24 PM, wrote:

Reviewers: golang-dev_googlegroups.com,

Message:
Hello golang-dev@googlegroups.com,

I'd like you to review this change to
https://code.google.com/p/go/


Description:
encoding/gob: fix data race in Register
Fixes issue 4214.

Please review this at http://codereview.appspot.com/**6637047/<http://codereview.appspot.com/6637047/>

Affected files:
M src/pkg/encoding/gob/decode.go
M src/pkg/encoding/gob/encode.go
M src/pkg/encoding/gob/type.go
M src/pkg/encoding/gob/type_**test.go


Index: src/pkg/encoding/gob/decode.go
==============================**==============================**=======
--- a/src/pkg/encoding/gob/decode.**go
+++ b/src/pkg/encoding/gob/decode.**go
@@ -717,7 +717,9 @@
errorf("name too long (%d bytes): %.20q...", len(name),
name)
}
// The concrete type must be registered.
+ registerLock.Lock()
typ, ok := nameToConcreteType[name]
+ registerLock.Unlock()
if !ok {
errorf("name not registered for interface: %q", name)
}
Index: src/pkg/encoding/gob/encode.go
==============================**==============================**=======
--- a/src/pkg/encoding/gob/encode.**go
+++ b/src/pkg/encoding/gob/encode.**go
@@ -441,7 +441,9 @@
}

ut := userType(iv.Elem().Type())
+ registerLock.Lock()
name, ok := concreteTypeToName[ut.base]
+ registerLock.Unlock()
if !ok {
errorf("type not registered for interface: %s", ut.base)
}
Index: src/pkg/encoding/gob/type.go
==============================**==============================**=======
--- a/src/pkg/encoding/gob/type.go
+++ b/src/pkg/encoding/gob/type.go
@@ -712,6 +712,7 @@
}

var (
+ registerLock sync.Mutex
nameToConcreteType = make(map[string]reflect.Type)
concreteTypeToName = make(map[reflect.Type]string)
)
@@ -723,6 +724,8 @@
// reserved for nil
panic("attempt to register empty name")
}
+ registerLock.Lock()
+ defer registerLock.Unlock()
ut := userType(reflect.TypeOf(value)**)
// Check for incompatible duplicates. The name must refer to the
// same user type, and vice versa.
Index: src/pkg/encoding/gob/type_**test.go
==============================**==============================**=======
--- a/src/pkg/encoding/gob/type_**test.go
+++ b/src/pkg/encoding/gob/type_**test.go
@@ -177,7 +177,10 @@
Register(tc.t)

tct := reflect.TypeOf(tc.t)
- if ct := nameToConcreteType[tc.name]; ct != tct {
+ registerLock.Lock()
+ ct := nameToConcreteType[tc.name]
+ registerLock.Unlock()
+ if ct != tct {
t.Errorf("nameToConcreteType[%**q] = %v, want
%v", tc.name, ct, tct)
}
// concreteTypeToName is keyed off the base type.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 7 | next ›
Discussion Overview
groupgolang-dev @
categoriesgo
postedOct 9, '12 at 12:25a
activeOct 9, '12 at 1:03a
posts7
users3
websitegolang.org

3 users in discussion

R: 3 posts Dsymonds: 2 posts Brad Fitzpatrick: 2 posts

People

Translate

site design / logo © 2022 Grokbase