hoxworth/json-schema — Issue #24: Validator is not thread safe
Author: dekellum
Created: 2012-01-19 02:34:40+00:00
State: open

In a threaded environment we need to synchronize around all calls to Validator.validate!(). Otherwise we get bogus errors like the one shown below. In this case, the referenced file does indeed contain complexText; as it does when successfully run single threaded.

Why are we using threads?: Threaded application server (rack, sinatra on jruby), and trying to validate json against a schema just after it is produced but before it is returned.

I suspect this is due to use of @@class_vars for state in Validator, but I still don't quite understanding it well enough to know if would be reasonable to remove those for instance variables, that would avoid the issue. How might I best help on this? Can I offer a test case that demonstrates the problem to start with?

~~~
JSON::Schema::ValidationError: The referenced schema 'file:///home/david/src/evri/evrinid/content-store/model/schema/common.json#complexText' cannot be found in schema file:///home/david/src/evri/evrinid/content-store/model/schema/content.json#
/home/david/src/json-schema/lib/json-schema/validator.rb:41:in `validation_error'
/home/david/src/json-schema/lib/json-schema/attributes/ref.rb:50:in `validate'
/home/david/src/json-schema/lib/json-schema/validator.rb:70:in `validate'
org/jruby/RubyHash.java:1170:in `each'
/home/david/src/json-schema/lib/json-schema/validator.rb:68:in `validate'
/home/david/src/json-schema/lib/json-schema/schema.rb:38:in `validate'
/home/david/src/json-schema/lib/json-schema/attributes/properties.rb:15:in `validate'
org/jruby/RubyHash.java:1170:in `each'
/home/david/src/json-schema/lib/json-schema/attributes/properties.rb:6:in `validate'
/home/david/src/json-schema/lib/json-schema/validator.rb:70:in `validate'
org/jruby/RubyHash.java:1170:in `each'
/home/david/src/json-schema/lib/json-schema/validator.rb:68:in `validate'
/home/david/src/json-schema/lib/json-schema/schema.rb:38:in `validate'
/home/david/src/json-schema/lib/json-schema/attributes/items.rb:10:in `validate'
org/jruby/RubyArray.java:1612:in `each'
org/jruby/RubyEnumerable.java:951:in `each_with_index'
/home/david/src/json-schema/lib/json-schema/attributes/items.rb:7:in `validate'
/home/david/src/json-schema/lib/json-schema/validator.rb:70:in `validate'
org/jruby/RubyHash.java:1170:in `each'
/home/david/src/json-schema/lib/json-schema/validator.rb:68:in `validate'
/home/david/src/json-schema/lib/json-schema/schema.rb:38:in `validate'
/home/david/src/json-schema/lib/json-schema/attributes/properties.rb:15:in `validate'
org/jruby/RubyHash.java:1170:in `each'
/home/david/src/json-schema/lib/json-schema/attributes/properties.rb:6:in `validate'
/home/david/src/json-schema/lib/json-schema/validator.rb:70:in `validate'
org/jruby/RubyHash.java:1170:in `each'
/home/david/src/json-schema/lib/json-schema/validator.rb:68:in `validate'
/home/david/src/json-schema/lib/json-schema/schema.rb:38:in `validate'
/home/david/src/json-schema/lib/json-schema/attributes/ref.rb:47:in `validate'
/home/david/src/json-schema/lib/json-schema/validator.rb:70:in `validate'
org/jruby/RubyHash.java:1170:in `each'
/home/david/src/json-schema/lib/json-schema/validator.rb:68:in `validate'
/home/david/src/json-schema/lib/json-schema/schema.rb:38:in `validate'
/home/david/src/json-schema/lib/json-schema/validator.rb:140:in `validate'
/home/david/src/json-schema/lib/json-schema/validator.rb:258:in `validate!'
~~~