-
Notifications
You must be signed in to change notification settings - Fork 6
Bug: thread safety issues during validation #224
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
Comments
Hi, @pgarrett-twc. Thank you for reporting this issue. The problem is very simple - the reference resolver that is shared between validations has a mutable state. And because of that you are getting the errors you see. I will take a look at how to avoid sharing the mutable state between invocations. |
Instead of having a shared reference resolver we now create a new one for each `validate` method invocation. This removes the shared mutable state when JsonSchema is invoked from multiple threads. This change should not affect performance much as the reference resolver is created only once per `validate` method invocation. Resolves #224
Hi, @pgarrett-twc. The fix is available in the latest snapshot version Once the release is published I will add a comment to this issue. |
Wow, thanks for the quick turnaround! |
Available in release 0.3.1 |
Is there an existing issue for this?
Current Behavior
When I run
schema.validate(doc) {}
from multiple threads simultaneously, I frequently get one of these errors:or
When I run the same code either single-threaded, or with a
ThreadLocal
copy of theJsonSchema
, there are no errors.Expected Behavior
It would be nice if the schema could be used safely from multiple simultaneous threads.
However, I don't think the documentation states whether the library is expected to be thread-safe, so maybe it is not intended to be? I'm just reporting this in case it is not expected, since this being resolved in the library would simplify my code a bit.
JSON schema
Schema(s) supplied via recreation repo. https://github.com/pgarrett-twc/jsonschema-threadsafety/tree/main/src/test/resources
Library version
0.3.0
Anything else?
I've pushed a sample repo with a test that usually reproduces the problem:
https://github.com/pgarrett-twc/jsonschema-threadsafety/blob/main/src/test/kotlin/ThreadSafetyTests.kt#L40-L53
I've reproduced this with both Java 17 and 21, on ARM MacOS.
These are the full stack traces of the errors I've seen.
stack1.txt
stack2.txt
The text was updated successfully, but these errors were encountered: