Skip to content

Version 2.19.0 causes an infinite loop with the LTS version of activesupport 4.2.11.40 #950

Closed as not planned

Description

@rwadstein

I realize we are still on an older version of Rails 4 and we are actively converting it to go to a newer version.
In the meantime, just want to point out going from 2.18.1 to 2.19.0 caused an infinite loop when using the #to_json method.

Currently, we are just using != '2.19.0' in the Gemfile in hopes that this will be addressed, but realize that such an older version may not constitute a fix. Just for reference there is no problem when using it with Rails 5 LTS.

Activity

  1. byroot commented on Mar 6, 2026

    @byroot
    Member

    It's likely a consequence of #941

    I can't promise I can fix it, but I'd appreciate if you could provide a reproduction script so I can investigate the feasibility of fixing it.

  2. byroot commented on Mar 6, 2026

    @byroot
    Member

    I tried to repro with the little information available, but no luck:

    require "bundler/inline"
    
    gemfile do
      gem "activesupport", "4.2.11.3"
      gem "json", "2.19.0"
    end
    
    class Some
      def as_json(...)
        {obj: 1}
      end
    end
    
    puts([{foo: 1, bar: Some.new}].to_json)

    So until I get a repro script or at least some more details, my hands are tied.

  3. rwadstein commented on Mar 9, 2026

    @rwadstein
    Author

    No problem. Thanks for the help. I figured this might be an issue for Rails LTS versions rather than for json since yours works with the last official release. Since Rails LTS are the ones that have extended the last supported version of Rails 4.2, I'll try to reach out to them.
    thanks again.

  4. rwadstein commented on Mar 9, 2026

    @rwadstein
    Author

    I was double checking a few things, and found a script that will easily create the problem, but requires going back to ruby 2.7
    Rails LTS supports allows ruby 3+ with the newer versions of BigDecimal, but going back to ruby 2.7 and I can still create the same problem.

    require "bundler/inline"
    
    gemfile do
      gem "activesupport", "4.2.11.3"
      gem "json", "2.19.0"
      gem "bigdecimal", "1.4.4"
    end
    
    require 'active_support/all'
    
    puts([{foo: 1, bar: "asdf"}].to_json)
    
    Traceback (most recent call last):
           16: from /usr/local/bundle/gems/activesupport-4.2.11.3/lib/active_support/core_ext/object/json.rb:34:in 
    ...
            1: from /usr/local/bundle/gems/activesupport-4.2.11.3/lib/active_support/core_ext/object/json.rb:34:in `to_json_with_active_support_encoder'
    SystemStackError (stack level too deep)
  5. rwadstein commented on Mar 9, 2026

    @rwadstein
    Author

    We are actively trying to go to Rails 5, so we can easily just lock it to < 2.19 for now.
    I guess at this point, this is just for context if you decide you want to fix it or not.

  6. byroot commented on Mar 9, 2026

    @byroot
    Member

    Ok I see. I'm afraid I can't fix that, except by reverting what I believe to be an important improvement.

    Rails 4.x is tightly coupled to to_json methods being defined on specific classes.

    You can monkey patch to fix the problem though:

    require "bundler/inline"
    
    gemfile do
      source 'https://rubygems.org'
      gem "stringio"
      gem "activesupport", "4.2.11.3"
      gem "json", "2.19.0"
      gem "bigdecimal", "1.4.4"
      gem "logger"
      gem "base64"
      gem "mutex_m"
      gem "benchmark"
    end
    
    require 'active_support/all'
    
    # Remove custom methods from everywhere but `Object`.
    [Enumerable, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass].each do |klass|
      klass.class_eval do
        remove_method :to_json
        remove_method :to_json_without_active_support_encoder
      end
    end
    
    puts([{foo: 1, bar: "asdf"}].to_json)

    Sorry, I can't do any better than that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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