Skip to content

Commit

Permalink
Clear all current instances before a reload.
Browse files Browse the repository at this point in the history
If users added an attribute or otherwise changed a CurrentAttributes subclass
they'd see exceptions on the next page load.

Because `ActiveSupport::CurrentAttributes.current_instances` would keep
references to the old instances from the previous request.

We can fix this by clearing out the `current_attributes` before we unload
constants. Then any change to the model can be autoloaded again since its
slot isn't taken by an old instance.

We'll still have to call reset before we clear so external collaborators,
like Time.zone, won't linger with their current value throughout other code.
  • Loading branch information
kaspth committed May 28, 2017
1 parent 96be813 commit 85211ea
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 5 additions & 0 deletions activesupport/lib/active_support/current_attributes.rb
Expand Up @@ -126,6 +126,11 @@ def reset_all # :nodoc:
current_instances.each_value(&:reset)
end

def clear_all # :nodoc:
reset_all
current_instances.clear
end

private
def generated_attribute_methods
@generated_attribute_methods ||= Module.new.tap { |mod| include mod }
Expand Down
5 changes: 3 additions & 2 deletions activesupport/lib/active_support/railtie.rb
Expand Up @@ -8,8 +8,9 @@ class Railtie < Rails::Railtie # :nodoc:
config.eager_load_namespaces << ActiveSupport

initializer "active_support.reset_all_current_attributes_instances" do |app|
app.executor.to_run { ActiveSupport::CurrentAttributes.reset_all }
app.executor.to_complete { ActiveSupport::CurrentAttributes.reset_all }
app.reloader.before_class_unload { ActiveSupport::CurrentAttributes.clear_all }
app.executor.to_run { ActiveSupport::CurrentAttributes.reset_all }
app.executor.to_complete { ActiveSupport::CurrentAttributes.reset_all }
end

initializer "active_support.deprecation_behavior" do |app|
Expand Down

0 comments on commit 85211ea

Please sign in to comment.