Skip to content

Commit

Permalink
Don't STI load via the descendants/subclasses hooks during code reload
Browse files Browse the repository at this point in the history
The descendant_loader.rb hooks descendants and subclasses to do proper sti loading of
subclasses and descendants. It should not be used when code reload is happening.

This fixes a bug when issuing reload! in rails console with rails 7.
  • Loading branch information
jrafanie committed Aug 6, 2024
1 parent 562abe9 commit 2709cc7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 8 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

module Vmdb
class Application < Rails::Application
attr_accessor :reloading

# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
Expand Down Expand Up @@ -191,6 +193,12 @@ class Application < Rails::Application
# Reload Settings to get values from db now that it's safe to autoload
::Settings.reload!
Vmdb::Loggers.apply_config(::Settings.log)

# The descendant_loader.rb hooks descendants and subclasses to do proper sti loading of
# subclasses and descendants. It should not be used when code reload is happening.
Vmdb::Application.reloading = false
Vmdb::Application.reloader.before_class_unload { Vmdb::Application.reloading = true }
Vmdb::Application.reloader.to_complete { Vmdb::Application.reloading = false }
end

console do
Expand Down
4 changes: 2 additions & 2 deletions lib/extensions/descendant_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ module ArDescendantsWithLoader
# "xxx/manageiq/app/models/vm_or_template.rb:15:in `<class:VmOrTemplate>'",
# "xxx/manageiq/app/models/vm_or_template.rb:6:in `<main>'",
def descendants
if Vmdb::Application.instance.initialized? && !defined?(@loaded_descendants) && %w[__update_callbacks].exclude?(caller_locations(1..1).first.base_label)
if Vmdb::Application.instance.initialized? && !defined?(@loaded_descendants) && %w[__update_callbacks].exclude?(caller_locations(1..1).first.base_label) && !Vmdb::Application.reloading
@loaded_descendants = true
DescendantLoader.instance.load_subclasses(self)
end
Expand Down Expand Up @@ -327,7 +327,7 @@ def descendants
# "xxx/gems/activesupport-7.0.8.4/lib/active_support/descendants_tracker.rb:89:in `descendants'",
# ...
def subclasses
if Vmdb::Application.instance.initialized? && !defined?(@loaded_descendants) && %w[descendants reload_schema_from_cache subclasses].exclude?(caller_locations(1..1).first.base_label)
if Vmdb::Application.instance.initialized? && !defined?(@loaded_descendants) && %w[descendants reload_schema_from_cache subclasses].exclude?(caller_locations(1..1).first.base_label) && !Vmdb::Application.reloading
@loaded_descendants = true
DescendantLoader.instance.load_subclasses(self)
end
Expand Down

0 comments on commit 2709cc7

Please sign in to comment.