In JRuby we hit a race condition: ActiveSupport uses a single shared JSON::Coder
instance and calls #dump on it from multiple threads.
ActiveSupport creates one shared Coder:
https://github.com/rails/rails/blob/v8.1.3.1/activesupport/lib/active_support/json/encoding.rb#L152
…and reuses it on every encode:
https://github.com/rails/rails/blob/v8.1.3.1/activesupport/lib/active_support/json/encoding.rb#L188
The error we got, on a flat object was
JSON::NestingError: nesting of 100 is too deep. Did you try to serialize objects with circular references?
Question: is a single JSON::Coder instance intended to be safe to call
concurrently from multiple threads (i.e. is #dump/#generate on one Coder
intended to be thread-safe)?
Versions: json 2.21.2 (java), JRuby 10.0.5.0, activesupport 8.1.3.1.
I have a small threaded reproduction I can add if useful.
In JRuby we hit a race condition: ActiveSupport uses a single shared
JSON::Coderinstance and calls
#dumpon it from multiple threads.ActiveSupport creates one shared Coder:
https://github.com/rails/rails/blob/v8.1.3.1/activesupport/lib/active_support/json/encoding.rb#L152
…and reuses it on every encode:
https://github.com/rails/rails/blob/v8.1.3.1/activesupport/lib/active_support/json/encoding.rb#L188
The error we got, on a flat object was
JSON::NestingError: nesting of 100 is too deep. Did you try to serialize objects with circular references?Question: is a single
JSON::Coderinstance intended to be safe to callconcurrently from multiple threads (i.e. is
#dump/#generateon one Coderintended to be thread-safe)?
Versions: json 2.21.2 (java), JRuby 10.0.5.0, activesupport 8.1.3.1.
I have a small threaded reproduction I can add if useful.