Skip to content

Commit b0c714b

Browse files
jvogitdaveshanley
authored andcommitted
fix: lock before checking sp.rendered to avoid datarace
1 parent d3dcac4 commit b0c714b

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

datamodel/high/base/schema_proxy.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,23 @@ func (sp *SchemaProxy) Schema() *Schema {
9393
return nil
9494
}
9595

96-
if sp.rendered != nil {
97-
return sp.rendered
98-
}
99-
10096
if sp.schema == nil || sp.schema.Value == nil {
10197
return nil
10298
}
10399

104100
sp.lock.Lock()
101+
defer sp.lock.Unlock()
102+
103+
if sp.rendered != nil {
104+
return sp.rendered
105+
}
105106

106107
//check the high-level cache first.
107108
idx := sp.schema.Value.GetIndex()
108109
if idx != nil && sp.schema.Value != nil {
109110
if sp.schema.Value.IsReference() && sp.schema.Value.GetReferenceNode() != nil && sp.schema.GetValueNode() != nil {
110111
loc := fmt.Sprintf("%s:%d:%d", idx.GetSpecAbsolutePath(), sp.schema.GetValueNode().Line, sp.schema.GetValueNode().Column)
111112
if seen, ok := idx.GetHighCache().Load(loc); ok {
112-
sp.lock.Unlock()
113113
idx.HighCacheHit()
114114
return seen.(*Schema)
115115
} else {
@@ -121,7 +121,6 @@ func (sp *SchemaProxy) Schema() *Schema {
121121
s := sp.schema.Value.Schema()
122122
if s == nil {
123123
sp.buildError = sp.schema.Value.GetBuildError()
124-
sp.lock.Unlock()
125124
return nil
126125
}
127126
sch := NewSchema(s)
@@ -143,7 +142,6 @@ func (sp *SchemaProxy) Schema() *Schema {
143142

144143
sch.ParentProxy = sp
145144
sp.rendered = sch
146-
sp.lock.Unlock()
147145
return sch
148146

149147
}

0 commit comments

Comments
 (0)