Skip to content

Commit

Permalink
Added a render lock to stop concurrency from mashing up state in a sh…
Browse files Browse the repository at this point in the history
…ared model.

When using a shared model for validation, concurrency can cause state read and write to crash, this is because rendering sets flags on the structs to mark them as pre-rendered and this causes all kinds of problems.

This lock now keeps things orderly when running concurrent requests for validation that require schema rendering.

Signed-off-by: Dave Shanley <[email protected]>
  • Loading branch information
daveshanley committed Sep 18, 2023
1 parent e304fea commit 75f5a10
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion schema_validation/validate_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"regexp"
"strconv"
"strings"
"sync"
)

// SchemaValidator is an interface that defines the methods for validating a *base.Schema (V3+ Only) object.
Expand Down Expand Up @@ -64,6 +65,8 @@ func (s *schemaValidator) ValidateSchemaBytes(schema *base.Schema, payload []byt
return validateSchema(schema, payload, nil, s.logger)
}

var renderLock = &sync.Mutex{}

func validateSchema(schema *base.Schema, payload []byte, decodedObject interface{}, log *zap.SugaredLogger) (bool, []*errors.ValidationError) {

var validationErrors []*errors.ValidationError
Expand All @@ -73,8 +76,12 @@ func validateSchema(schema *base.Schema, payload []byte, decodedObject interface
return false, validationErrors
}

// render the schema, to be used for validation
// render the schema, to be used for validation, stop this from running concurrently, mutations are made to state
// and, it will cause async issues.
renderLock.Lock()
renderedSchema, _ := schema.RenderInline()
renderLock.Unlock()

jsonSchema, _ := utils.ConvertYAMLtoJSON(renderedSchema)

if decodedObject == nil && len(payload) > 0 {
Expand Down

0 comments on commit 75f5a10

Please sign in to comment.