-
Notifications
You must be signed in to change notification settings - Fork 900
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
Only run load_subclasses after app is initialized #22904
Only run load_subclasses after app is initialized #22904
Conversation
In rails 6.1, nothing was calling descendants or subclasses during the app initialization. Rails changed in 7.0 to call subclasses from reload_schema_from_cache here: rails/rails@6f30cc0 It also changed to call descendants on the callback class (self) insead of the ActiveSupport::DescendantsTracker here: rails/rails@ffae3bd We are not expecting to be called from these locations. We can make this rails 7 compatible by ensuring the descendant loader loading of subclasses until after the app is booted, which was the implicit behavior previously.
FYI, to demonstrate rails 6.1 didn't hit this code path during rails boot: diff --git a/lib/extensions/descendant_loader.rb b/lib/extensions/descendant_loader.rb
index 488ab2500f..09455b37f3 100644
--- a/lib/extensions/descendant_loader.rb
+++ b/lib/extensions/descendant_loader.rb
@@ -263,6 +263,7 @@ class DescendantLoader
module ArDescendantsWithLoader
def descendants
+ raise if !defined? @loaded_descendants
if Vmdb::Application.instance.initialized? && !defined? @loaded_descendants
@loaded_descendants = true
DescendantLoader.instance.load_subclasses(self)
@@ -275,6 +276,7 @@ class DescendantLoader
# https://github.com/rails/rails/commit/8f8aa857e084b76b1120edaa9bb9ce03ba1e6a19
# We need to get in front of it, like we do for descendants.
def subclasses
+ raise if !defined? @loaded_descendants
if Vmdb::Application.instance.initialized? && !defined? @loaded_descendants
@loaded_descendants = true
DescendantLoader.instance.load_subclasses(self) It loads in console and rails runner:
As soon as I try to load descendants after app initialization, my hack to raise shows we hit this code path. In rails 7, this is hit during application boot.
Here's what it looks like without even loading descendants on rails 7: diff --git a/lib/extensions/descendant_loader.rb b/lib/extensions/descendant_loader.rb
index 488ab2500f..09455b37f3 100644
--- a/lib/extensions/descendant_loader.rb
+++ b/lib/extensions/descendant_loader.rb
@@ -263,6 +263,7 @@ class DescendantLoader
module ArDescendantsWithLoader
def descendants
+ raise if !defined? @loaded_descendants
if Vmdb::Application.instance.initialized? && !defined? @loaded_descendants
@loaded_descendants = true
DescendantLoader.instance.load_subclasses(self)
@@ -275,6 +276,7 @@ class DescendantLoader
# https://github.com/rails/rails/commit/8f8aa857e084b76b1120edaa9bb9ce03ba1e6a19
# We need to get in front of it, like we do for descendants.
def subclasses
+ raise if !defined? @loaded_descendants
if Vmdb::Application.instance.initialized? && !defined? @loaded_descendants
@loaded_descendants = true
DescendantLoader.instance.load_subclasses(self)
|
Checked commit jrafanie@a7aa6b4 with ruby 2.7.8, rubocop 1.56.3, haml-lint 0.51.0, and yamllint |
In rails 6.1, nothing was calling descendants or subclasses during the app initialization.
Rails changed in 7.0 to call subclasses from reload_schema_from_cache here: rails/rails@6f30cc0
It also changed to call descendants on the callback class (self) insead of the ActiveSupport::DescendantsTracker here:
rails/rails@ffae3bd
We are not expecting to be called from these locations.
We can make this rails 7 compatible by ensuring the descendant loader loading of subclasses until after the app is booted, which was the implicit behavior previously.
(Extracted from #22873)