Skip to content

Commit 75f5a10

Browse files
committed
Added a render lock to stop concurrency from mashing up state in a shared 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]>
1 parent e304fea commit 75f5a10

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

schema_validation/validate_schema.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"regexp"
1919
"strconv"
2020
"strings"
21+
"sync"
2122
)
2223

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

68+
var renderLock = &sync.Mutex{}
69+
6770
func validateSchema(schema *base.Schema, payload []byte, decodedObject interface{}, log *zap.SugaredLogger) (bool, []*errors.ValidationError) {
6871

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

76-
// render the schema, to be used for validation
79+
// render the schema, to be used for validation, stop this from running concurrently, mutations are made to state
80+
// and, it will cause async issues.
81+
renderLock.Lock()
7782
renderedSchema, _ := schema.RenderInline()
83+
renderLock.Unlock()
84+
7885
jsonSchema, _ := utils.ConvertYAMLtoJSON(renderedSchema)
7986

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

0 commit comments

Comments
 (0)