Permalink
Browse files

Fix race condition 💣

  • Loading branch information...
1 parent 8a3dcd7 commit 2c667f69aa2daac5ee6c29ca9679616e2a71532a @jonleighton jonleighton committed Jan 13, 2012
Showing with 11 additions and 4 deletions.
  1. +11 −4 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 ClassMethods
# 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
@pwnall

pwnall Jan 15, 2012

Contributor

I think this line has to be in the included block for the code not to be racy?

+
+ @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?

0 comments on commit 2c667f6

Please sign in to comment.