Skip to content

Commit

Permalink
Fix race condition 💣
Browse files Browse the repository at this point in the history
  • Loading branch information
jonleighton committed Jan 13, 2012
1 parent c159b01 commit 70b762d
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions 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
Expand Down Expand Up @@ -36,10 +37,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

@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?
Expand Down

0 comments on commit 70b762d

Please sign in to comment.