Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stack level too deep when accessing an ActiveStorage object #131

Closed
tagliala opened this issue Jan 21, 2022 · 3 comments · Fixed by #132
Closed

Stack level too deep when accessing an ActiveStorage object #131

tagliala opened this issue Jan 21, 2022 · 3 comments · Fixed by #132

Comments

@tagliala
Copy link
Member

tagliala commented Jan 21, 2022

Ruby: 3.0.3/2.7.5
Rails: 7.0.1/6.1.4.4

Reproducible test case at https://github.com/diowa/ruby3-rails6-bootstrap-heroku/tree/active-storage-chronomodel

Relevant commit: diowa/ruby3-rails6-bootstrap-heroku@9211ff2

Before

$ rails runner "puts User.first.avatar.download"
Running via Spring preloader in process 77316
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512"><!-- Font Awesome Free 5.15.4 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) --><path d="M248 8C111 8 0 119 0 256s111 248 248 248 248-111 248-248S385 8 248 8zm0 96c48.6 0 88 39.4 88 88s-39.4 88-88 88-88-39.4-88-88 39.4-88 88-88zm0 344c-58.7 0-111.3-26.6-146.5-68.2 18.8-35.4 55.6-59.8 98.5-59.8 2.4 0 4.8.4 7.1 1.1 13 4.2 26.6 6.9 40.9 6.9 14.3 0 28-2.7 40.9-6.9 2.3-.7 4.7-1.1 7.1-1.1 42.9 0 79.7 24.4 98.5 59.8C359.3 421.4 306.7 448 248 448z"/></svg>

After

$ rails runner "puts User.first.avatar.download"
Running via Spring preloader in process 77726
~/.rvm/gems/ruby-3.0.3/gems/activesupport-6.1.4.4/lib/active_support/core_ext/string/inflections.rb:61:in `singularize': stack level too deep (SystemStackError)
	from ~/.rvm/gems/ruby-3.0.3/gems/activerecord-6.1.4.4/lib/active_record/table_metadata.rb:26:in `associated_with?'
	from ~/.rvm/gems/ruby-3.0.3/gems/activerecord-6.1.4.4/lib/active_record/relation/predicate_builder.rb:88:in `block in expand_from_hash'
	from ~/.rvm/gems/ruby-3.0.3/gems/activerecord-6.1.4.4/lib/active_record/relation/predicate_builder.rb:84:in `each'
	from ~/.rvm/gems/ruby-3.0.3/gems/activerecord-6.1.4.4/lib/active_record/relation/predicate_builder.rb:84:in `flat_map'
	from ~/.rvm/gems/ruby-3.0.3/gems/activerecord-6.1.4.4/lib/active_record/relation/predicate_builder.rb:84:in `expand_from_hash'
	from ~/.rvm/gems/ruby-3.0.3/gems/activerecord-6.1.4.4/lib/active_record/relation/predicate_builder.rb:29:in `build_from_hash'
	from ~/.rvm/gems/ruby-3.0.3/gems/activerecord-6.1.4.4/lib/active_record/relation/query_methods.rb:1091:in `build_where_clause'
	from ~/.rvm/gems/ruby-3.0.3/gems/activerecord-6.1.4.4/lib/active_record/relation/query_methods.rb:645:in `where!'
	 ... 10825 levels...
	from ~/.rvm/gems/ruby-3.0.3/gems/spring-2.1.1/lib/spring/application/boot.rb:19:in `<top (required)>'
	from <internal:~/.rvm/rubies/ruby-3.0.3/lib/ruby/site_ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
	from <internal:~/.rvm/rubies/ruby-3.0.3/lib/ruby/site_ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
	from -e:1:in `<main>'

😊😊😊

@tagliala
Copy link
Member Author

tagliala commented Jan 21, 2022

The problem is here:

owner.respond_to?(:as_of_time) && owner.as_of_time.present?

Probably depends on the fact that ActiveStorage::Attachment delegates missing to Blob

https://github.com/rails/rails/blob/87d4d0f4126f64d991d40a1827de50935ddfdbff/activestorage/app/models/active_storage/attachment.rb#L16

More in general, Chronomodel has issues when using model with delegate_missing_to another model

# attachment.rb
class Attachment < ApplicationRecord
  belongs_to :blob
  delegate_missing_to :blob
end

# blob.rb
class Blob < ApplicationRecord
  has_many :attachments
end

tagliala added a commit that referenced this issue Jan 21, 2022
@tagliala
Copy link
Member Author

Failing spec bd830d7

@tagliala
Copy link
Member Author

tagliala commented Jan 21, 2022

"Temporary" Workaround:

# frozen_string_literal: true

# TODO: Remove me if ifad/chronomodel#131 will be fixed

module ChronoModel
  module Patches
    module NoChronoRecord
      def as_of_time
        false
      end
    end
  end
end

Rails.configuration.to_prepare do
  ActiveStorage::Attachment.include ChronoModel::Patches::NoChronoRecord
end

37585e99-8818-4a6c-b389-4dc98aab94e6

tagliala added a commit that referenced this issue Jan 22, 2022
Checks if the module `AsOfTimeHolder` is included in the model rather
than using `respond_to?` to prevent stack overflow when a model
delegates missing methods to an association.

Fix #131
tagliala added a commit that referenced this issue Jan 22, 2022
Checks if the module `AsOfTimeHolder` is included in the model rather
than using `respond_to?` to prevent stack overflow when a model
delegates missing methods to an association.

Fix #131
tagliala added a commit that referenced this issue Jan 22, 2022
Checks if the module `AsOfTimeHolder` is included in the model rather
than using `respond_to?` to prevent stack overflow when a model
delegates missing methods to an association.

Fix #131
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant