Skip to content

Is JSON::Coder intended to be thread safe ? #1064

Description

@daveferrykami

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions