Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixed data race in result (schemata caching) #182

Merged
merged 1 commit into from Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 45 additions & 17 deletions result.go
Expand Up @@ -52,8 +52,8 @@
// Schemata for slice items
itemSchemata []itemSchemata

cachedFieldSchemta map[FieldKey][]*spec.Schema
cachedItemSchemata map[ItemKey][]*spec.Schema
cachedFieldSchemata map[FieldKey][]*spec.Schema
cachedItemSchemata map[ItemKey][]*spec.Schema

wantsRedeemOnMerge bool
}
Expand Down Expand Up @@ -140,8 +140,8 @@

// FieldSchemata returns the schemata which apply to fields in objects.
func (r *Result) FieldSchemata() map[FieldKey][]*spec.Schema {
if r.cachedFieldSchemta != nil {
return r.cachedFieldSchemta
if r.cachedFieldSchemata != nil {
return r.cachedFieldSchemata
}

ret := make(map[FieldKey][]*spec.Schema, len(r.fieldSchemata))
Expand All @@ -153,7 +153,8 @@
ret[key] = append(ret[key], fs.schemata.multiple...)
}
}
r.cachedFieldSchemta = ret
r.cachedFieldSchemata = ret

return ret
}

Expand All @@ -177,7 +178,7 @@
}

func (r *Result) resetCaches() {
r.cachedFieldSchemta = nil
r.cachedFieldSchemata = nil
r.cachedItemSchemata = nil
}

Expand All @@ -194,10 +195,11 @@
if r.fieldSchemata == nil {
r.fieldSchemata = make([]fieldSchemata, len(obj))
}
// clone other schemata, as other is about to be redeemed to the pool
r.fieldSchemata = append(r.fieldSchemata, fieldSchemata{
obj: obj,
field: field,
schemata: other.rootObjectSchemata,
schemata: other.rootObjectSchemata.Clone(),
})
}
if other.wantsRedeemOnMerge {
Expand All @@ -220,12 +222,14 @@
if r.itemSchemata == nil {
r.itemSchemata = make([]itemSchemata, slice.Len())
}
// clone other schemata, as other is about to be redeemed to the pool
r.itemSchemata = append(r.itemSchemata, itemSchemata{
slice: slice,
index: i,
schemata: other.rootObjectSchemata,
schemata: other.rootObjectSchemata.Clone(),
})
}

if other.wantsRedeemOnMerge {
pools.poolOfResults.RedeemResult(other)
}
Expand Down Expand Up @@ -272,17 +276,21 @@

if other.fieldSchemata != nil {
if r.fieldSchemata == nil {
r.fieldSchemata = other.fieldSchemata
} else {
r.fieldSchemata = append(r.fieldSchemata, other.fieldSchemata...)
r.fieldSchemata = make([]fieldSchemata, 0, len(other.fieldSchemata))
}
for _, field := range other.fieldSchemata {
field.schemata = field.schemata.Clone()
r.fieldSchemata = append(r.fieldSchemata, field)
}
}

if other.itemSchemata != nil {
if r.itemSchemata == nil {
r.itemSchemata = other.itemSchemata
} else {
r.itemSchemata = append(r.itemSchemata, other.itemSchemata...)
r.itemSchemata = make([]itemSchemata, 0, len(other.itemSchemata))
}
for _, field := range other.itemSchemata {
field.schemata = field.schemata.Clone()
r.itemSchemata = append(r.itemSchemata, field)
}
}
}
Expand Down Expand Up @@ -465,8 +473,8 @@
r.rootObjectSchemata.multiple = r.rootObjectSchemata.multiple[:0]
r.fieldSchemata = r.fieldSchemata[:0]
r.itemSchemata = r.itemSchemata[:0]
for k := range r.cachedFieldSchemta {
delete(r.cachedFieldSchemta, k)
for k := range r.cachedFieldSchemata {
delete(r.cachedFieldSchemata, k)

Check warning on line 477 in result.go

View check run for this annotation

Codecov / codecov/patch

result.go#L477

Added line #L477 was not covered by tests
}
for k := range r.cachedItemSchemata {
delete(r.cachedItemSchemata, k)
Expand Down Expand Up @@ -502,7 +510,7 @@
return s.multiple
}

// appendSchemata appends the schemata in other to s. It mutated s in-place.
// appendSchemata appends the schemata in other to s. It mutates s in-place.
func (s *schemata) Append(other schemata) {
if other.one == nil && len(other.multiple) == 0 {
return
Expand Down Expand Up @@ -533,3 +541,23 @@
}
}
}

func (s schemata) Clone() schemata {
var clone schemata

if s.one != nil {
clone.one = new(spec.Schema)
*clone.one = *s.one
}

if len(s.multiple) > 0 {
clone.multiple = make([]*spec.Schema, len(s.multiple))
for idx := 0; idx < len(s.multiple); idx++ {
sp := new(spec.Schema)
*sp = *s.multiple[idx]
clone.multiple[idx] = sp
}
}

return clone
}
10 changes: 0 additions & 10 deletions schema_props_test.go
Expand Up @@ -51,16 +51,6 @@ func TestSchemaPropsValidator_EdgeCases(t *testing.T) {
res := s.Validate(data)
require.NotNil(t, res)
require.Empty(t, res.Errors)

/* TODO(fred)
t.Run("validator should run once", func(t *testing.T) {
// we should not do that: the pool chain list is populated with a duplicate: needs a reset
t.Cleanup(resetPools)
require.NotPanics(t, func() {
_ = s.Validate(data)
})
})
*/
})

t.Run("should NOT validate unformatted string", func(t *testing.T) {
Expand Down