Skip to content

ArgumentOutOfRangeException thrown when calling JsonSchema's Evaluate methods from multiple threads. #693

@filzrev

Description

@filzrev

Nuget Package

JsonSchema.Net

Package Version

6.0.5

Operating System

Windows

.Net Target (if relevant, please specify the version in the description)

.Net (5 or after)

A clear and concise description of the bug.

When calling schema.Evaluate from multiple thread simultaneoutly.
Following exception thrown randomly.

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at System.Linq.Enumerable.TryGetSingle[TSource](IEnumerable`1 source, Boolean& found)
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at Json.Schema.JsonSchema.CheckScopedConstraints(DynamicScope scope)
   at Json.Schema.JsonSchema.BuildConstraint(JsonPointer evaluationPath, JsonPointer baseInstanceLocation, JsonPointer relativeInstanceLocation, DynamicScope scope)
   at Json.Schema.JsonSchema.GetConstraint(JsonPointer relativeEvaluationPath, JsonPointer baseInstanceLocation, JsonPointer relativeInstanceLocation, EvaluationContext context)
   at Json.Schema.ItemsKeyword.GetConstraint(SchemaConstraint schemaConstraint, IReadOnlyList`1 localConstraints, EvaluationContext context)
   at Json.Schema.JsonSchema.PopulateConstraint(SchemaConstraint constraint, EvaluationContext context)
   at Json.Schema.JsonSchema.Evaluate(JsonNode root, EvaluationOptions options)
   at ConsoleApp13.Program.<>c__DisplayClass0_0.<Main>b__0(Int32 s) 

It's seems occurred when using JsonSchema package 6.0.3 or later.
https://github.com/gregsdennis/json-everything/blob/cc65d0ae75a985ad07529dbd3d1f5b2220df98ce/JsonSchema/JsonSchema.cs#L287-L295

What did you expect?

Exception not thrown.

Please add test code or steps to reproduce the behavior.

JsonSchema schema = new JsonSchemaBuilder()
 .Type(SchemaValueType.Array)
 .PrefixItems(
     new JsonSchemaBuilder()
         .Type(SchemaValueType.Integer),
     new JsonSchemaBuilder()
         .Type(SchemaValueType.Boolean)
 )
 .Items(new JsonSchemaBuilder()
     .Type(SchemaValueType.String)
 );

var instance = new JsonArray { 1, true, "foo", "bar" };

Parallel.ForEach(Enumerable.Range(1, 100000), (x) =>
{
    var options = new EvaluationOptions(); // Create new options everytime to call `_constraints.Clear()`
    var results = schema.Evaluate(instance, options);
});

Is there any other information you'd like to share regarding this bug?

No response

Code of Conduct & Contributor Guidelines

  • I agree to follow this project's Code of Conduct and Contribution Guidelines.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions