Browse files

Sever infinite loop for mutual dependencies. Closes #2997.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3181 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 19f59b6 commit 52325f6bf868c5efa80fbcf06d139a31816251fb @jeremy jeremy committed Nov 24, 2005
View
22 activesupport/lib/active_support/dependencies.rb
@@ -45,18 +45,26 @@ def require_or_load(file_name)
# Append .rb if we have a bare file name.
load_file_name = (file_name =~ /\.rb$/ ? file_name : "#{file_name}.rb")
- # Enable warnings iff this file has not been loaded before.
- if history.include?(file_name)
- load load_file_name
- else
- enable_warnings { load load_file_name }
+ # Record that we've seen this file *before* loading it to avoid an
+ # infinite loop with mutual dependencies.
+ loaded << file_name
+
+ begin
+ # Enable warnings iff this file has not been loaded before.
+ if history.include?(file_name)
+ load load_file_name
+ else
+ enable_warnings { load load_file_name }
+ end
+ rescue
+ loaded.delete file_name
+ raise
end
else
require file_name
end
- # Record that we've seen this file.
- loaded << file_name
+ # Record history *after* loading so first load gets warnings.
history << file_name
end
View
2 activesupport/test/dependencies/mutual_one.rb
@@ -0,0 +1,2 @@
+$mutual_dependencies_count += 1
+require_dependency 'mutual_two'
View
2 activesupport/test/dependencies/mutual_two.rb
@@ -0,0 +1,2 @@
+$mutual_dependencies_count += 1
+require_dependency 'mutual_one'
View
1 activesupport/test/dependencies/raises_exception.rb
@@ -1,2 +1,3 @@
$raises_exception_load_count += 1
raise 'Loading me failed, so do not add to loaded or history.'
+$raises_exception_load_count += 1
View
18 activesupport/test/dependencies_test.rb
@@ -79,4 +79,22 @@ def test_warnings_should_be_enabled_on_first_load
ensure
Dependencies.mechanism = old_mechanism
end
+
+ def test_mutual_dependencies_dont_infinite_loop
+ $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/dependencies"
+ old_mechanism, Dependencies.mechanism = Dependencies.mechanism, :load
+
+ $mutual_dependencies_count = 0
+ assert_nothing_raised { require_dependency 'mutual_one' }
+ assert_equal 2, $mutual_dependencies_count
+
+ Dependencies.clear
+
+ $mutual_dependencies_count = 0
+ assert_nothing_raised { require_dependency 'mutual_two' }
+ assert_equal 2, $mutual_dependencies_count
+ ensure
+ $LOAD_PATH.shift
+ Dependencies.mechanism = old_mechanism
+ end
end

0 comments on commit 52325f6

Please sign in to comment.