diff options
author | Jon Leighton <j@jonathanleighton.com> | 2012-01-20 18:17:36 +0000 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2012-01-20 18:31:41 +0000 |
commit | fab664a8e9c94d7e3c52fed70ea2c0f569d142fb (patch) | |
tree | 9f87d7210c4054531138dd270ab4328680a80b3c | |
parent | f36dcaf488b4357a52f43e3912628428956d351f (diff) | |
download | rails-fab664a8e9c94d7e3c52fed70ea2c0f569d142fb.tar.gz rails-fab664a8e9c94d7e3c52fed70ea2c0f569d142fb.tar.bz2 rails-fab664a8e9c94d7e3c52fed70ea2c0f569d142fb.zip |
Fix another race condition.
From 2c667f69aa2daac5ee6c29ca9679616e2a71532a.
Thanks @pwnall for the heads-up.
Conflicts:
activerecord/lib/active_record/core.rb
-rw-r--r-- | activerecord/CHANGELOG.md | 5 | ||||
-rw-r--r-- | activerecord/lib/active_record/attribute_methods.rb | 3 | ||||
-rw-r--r-- | activerecord/lib/active_record/base.rb | 13 | ||||
-rw-r--r-- | activerecord/test/cases/attribute_methods/read_test.rb | 8 |
4 files changed, 23 insertions, 6 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 4bf7684874..05c32ecf86 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,8 @@ +## Rails 3.2.1 (unreleased) ## + +* Fix possible race condition when two threads try to define attribute + methods for the same class. + ## Rails 3.2.0 (January 20, 2012) ## * Added a `with_lock` method to ActiveRecord objects, which starts diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 31cd2edc12..f3200aa78a 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -1,6 +1,5 @@ require 'active_support/core_ext/enumerable' require 'active_support/deprecation' -require 'thread' module ActiveRecord # = Active Record Attribute Methods @@ -39,8 +38,6 @@ module ActiveRecord def define_attribute_methods # Use a mutex; we don't want two thread simaltaneously trying to define # attribute methods. - @attribute_methods_mutex ||= Mutex.new - @attribute_methods_mutex.synchronize do return if attribute_methods_generated? superclass.define_attribute_methods unless self == base_class diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index fa5846de39..198db715b2 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -5,6 +5,7 @@ end require 'yaml' require 'set' +require 'thread' require 'active_support/benchmarkable' require 'active_support/dependencies' require 'active_support/descendants_tracker' @@ -390,12 +391,18 @@ module ActiveRecord #:nodoc: class << self # Class methods def inherited(child_class) #:nodoc: - # force attribute methods to be higher in inheritance hierarchy than other generated methods - child_class.generated_attribute_methods - child_class.generated_feature_methods + child_class.initialize_generated_modules super end + def initialize_generated_modules #:nodoc: + @attribute_methods_mutex = Mutex.new + + # force attribute methods to be higher in inheritance hierarchy than other generated methods + generated_attribute_methods + generated_feature_methods + end + def generated_feature_methods @generated_feature_methods ||= begin mod = const_set(:GeneratedFeatureMethods, Module.new) diff --git a/activerecord/test/cases/attribute_methods/read_test.rb b/activerecord/test/cases/attribute_methods/read_test.rb index 7665f1c12e..6562041e9b 100644 --- a/activerecord/test/cases/attribute_methods/read_test.rb +++ b/activerecord/test/cases/attribute_methods/read_test.rb @@ -1,5 +1,6 @@ require "cases/helper" require 'active_support/core_ext/object/inclusion' +require 'thread' module ActiveRecord module AttributeMethods @@ -19,6 +20,13 @@ module ActiveRecord include ActiveRecord::AttributeMethods + def self.define_attribute_methods + # Created in the inherited/included hook for "proper" ARs + @attribute_methods_mutex ||= Mutex.new + + super + end + def self.column_names %w{ one two three } end |