Skip to content

ActiveSupport::Reloader#reload! invokes to_prepare callbacks twice #28108

@f1sherman

Description

@f1sherman

Calling reload! invokes to_prepare callbacks twice. This is asserted in the tests but seems undesirable from a consistency and performance standpoint. This also differs from the Rails server behavior, which calls the to_prepare callbacks only once when reloading.

Steps to reproduce

# test.rb

begin
  require "bundler/inline"
rescue LoadError => e
  $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
  raise e
end

gemfile(true) do
  source "https://rubygems.org"
  gem "rails", github: "rails/rails"
end

require "active_support"
require "active_support/core_ext/object/blank"
require "minitest/autorun"

class BugTest < Minitest::Test
  def test_reloader_calls_to_prepare_once
    to_prepare_call_count = 0
    reloader = Class.new(ActiveSupport::Reloader)
    reloader.to_prepare { to_prepare_call_count += 1 }
    reloader.reload!
    assert_equal 1, to_prepare_call_count
  end
end
$ ruby test.rb
<SNIP>
# Running:

F

Finished in 0.006512s, 153.5627 runs/s, 153.5627 assertions/s.

  1) Failure:
BugTest#test_reloader_calls_to_prepare_once [test.rb:25]:
Expected: 1
  Actual: 2

1 runs, 1 assertions, 1 failures, 0 errors, 0 skips

Expected behavior

The to_prepare callback should be invoked once

Actual behavior

The to_prepare callback is invoked twice

System configuration

Rails version:
master (also reproduced in 5.0.1)

Ruby version:
2.4.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions