Skip to content

Bug: thread safety issues during validation #224

Closed
@pgarrett-twc

Description

@pgarrett-twc

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinginternalChanges in internal part of the library

Projects

Status

✅ Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions