-
Notifications
You must be signed in to change notification settings - Fork 325
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
Race condition using schema configuration when caching in multi-threaded scenarios #744
Comments
We are doing the schema validation in the light-4j framework and it works for us as we don't change the configuration once it is created. That is why we didn't experience the issue like you do. Both option A and B will have extra cost and most people will have the static configuration in a multi-thread environment. Can we brainstorm to find a better option? |
Fair point. There is however no way around the current design whereby cached schemas are basically the same instances that may be reused concurrently. This needs either different instances per thread execution or something based on ThreadLocals. What I would suggest to address the (valid) performance concern you mention, is to use a ThreadLocal approach but only if explicitly requested by the user. I would add a new Using this approach there is zero impact on performance given that the new per-validation configuration applies only if explicitly set by the user. How does this sound? |
I think this should be OK. The default is to use a singleton. |
Class
SchemaValidatorsConfig
is currently used to define all configuration properties for validation before aJsonSchema
instance is loaded. This is set on the schema:In the second case, the configuration is set on the schema in case a different configuration was used when the schema was first initialised. This works fine if, using caching, we execute multiple validations sequentially, but is not correct in multi-threaded environments where we have parallel validations (for example a web application). The problem comes from setting the configuration on the cached schema itself which means that it may be changed by one thread while another thread is executing a validation.
As an example of the problem consider a web application using schema caching that allows users to choose the language for their JSON validation report between EN and FR. The following steps take place:
In brief, the issue is one of a shared concurrent cache with mutable state leading to race conditions. The current workaround for this is to either create a new
JsonSchemaFactory
per validation, or use a shared one with caching disabled.To correct the problem and fully support caching we could follow one of two approaches:
JsonSchema
instance is loaded from the cache, clone it in-depth before returning it, and set the configuration on the clone.ThreadLocal
to set and read theSchemaValidatorsConfig
instance.From the two options above, using a
ThreadLocal
would be the simplest and is the approach used also elsewhere for similar concerns (seeCollectorContext
).The text was updated successfully, but these errors were encountered: