Skip to content

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

Closed
1 task done
pgarrett-twc opened this issue Dec 31, 2024 · 4 comments · Fixed by #226
Closed
1 task done

Bug: thread safety issues during validation #224

pgarrett-twc opened this issue Dec 31, 2024 · 4 comments · Fixed by #226
Assignees
Labels
bug Something isn't working internal Changes in internal part of the library

Comments

@pgarrett-twc
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

When I run schema.validate(doc) {} from multiple threads simultaneously, I frequently get one of these errors:

java.util.NoSuchElementException: ArrayDeque is empty.
	at kotlin.collections.ArrayDeque.removeLast(ArrayDeque.kt:165)
	at io.github.optimumcode.json.schema.internal.DefaultReferenceResolver.popSchemaPath(ReferenceResolver.kt:65)
	...

or

java.lang.ArrayIndexOutOfBoundsException: arraycopy: last source index 549 out of bounds for object array[366]
	at java.base/java.lang.System.arraycopy(Native Method)
	at kotlin.collections.ArraysKt___ArraysJvmKt.copyInto(_ArraysJvm.kt:932)
	at kotlin.collections.ArrayDeque.copyElements(ArrayDeque.kt:74)
	at kotlin.collections.ArrayDeque.ensureCapacity(ArrayDeque.kt:66)
	at kotlin.collections.ArrayDeque.addLast(ArrayDeque.kt:136)
	at io.github.optimumcode.json.schema.internal.DefaultReferenceResolver.pushSchemaPath(ReferenceResolver.kt:61)
	...

When I run the same code either single-threaded, or with a ThreadLocal copy of the JsonSchema, 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

@pgarrett-twc pgarrett-twc added the bug Something isn't working label Dec 31, 2024
@OptimumCode
Copy link
Owner

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.

@OptimumCode OptimumCode added the internal Changes in internal part of the library label Jan 1, 2025
@OptimumCode OptimumCode moved this from 📋 Backlog to 🏗 In progress in Multi-platform JSON schema validator Jan 1, 2025
OptimumCode added a commit that referenced this issue Jan 1, 2025
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
@github-project-automation github-project-automation bot moved this from 🏗 In progress to ✅ Done in Multi-platform JSON schema validator Jan 1, 2025
@OptimumCode
Copy link
Owner

Hi, @pgarrett-twc. The fix is available in the latest snapshot version 0.3.1-SNAPSHOT.

Once the release is published I will add a comment to this issue.

@pgarrett-twc
Copy link
Author

Wow, thanks for the quick turnaround!

@OptimumCode
Copy link
Owner

Available in release 0.3.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working internal Changes in internal part of the library
Projects
Development

Successfully merging a pull request may close this issue.

2 participants