aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute_methods.rb
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2012-01-13 22:57:21 +0000
committerJon Leighton <j@jonathanleighton.com>2012-01-13 22:57:21 +0000
commit2c667f69aa2daac5ee6c29ca9679616e2a71532a (patch)
tree3c2190b5c0ec632c8469a51223ddacc8bceac590 /activerecord/lib/active_record/attribute_methods.rb
parent8a3dcd716c2c6b0fbd72633a1e58050aead75ee5 (diff)
downloadrails-2c667f69aa2daac5ee6c29ca9679616e2a71532a.tar.gz
rails-2c667f69aa2daac5ee6c29ca9679616e2a71532a.tar.bz2
rails-2c667f69aa2daac5ee6c29ca9679616e2a71532a.zip
Fix race condition :bomb:
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods.rb')
-rw-r--r--activerecord/lib/active_record/attribute_methods.rb15
1 files changed, 11 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb
index 02543db2ce..f8815fefc9 100644
--- a/activerecord/lib/active_record/attribute_methods.rb
+++ b/activerecord/lib/active_record/attribute_methods.rb
@@ -1,5 +1,6 @@
require 'active_support/core_ext/enumerable'
require 'active_support/deprecation'
+require 'thread'
module ActiveRecord
# = Active Record Attribute Methods
@@ -35,10 +36,16 @@ module ActiveRecord
# Generates all the attribute related methods for columns in the database
# accessors, mutators and query methods.
def define_attribute_methods
- return if attribute_methods_generated?
- superclass.define_attribute_methods unless self == base_class
- super(column_names)
- @attribute_methods_generated = true
+ # 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
+ super(column_names)
+ @attribute_methods_generated = true
+ end
end
def attribute_methods_generated?