Skip to content

Commit

Permalink
Fix another race condition.
Browse files Browse the repository at this point in the history
From 2c667f6.

Thanks @pwnall for the heads-up.

Conflicts:

	activerecord/lib/active_record/core.rb
  • Loading branch information
jonleighton committed Jan 20, 2012
1 parent f36dcaf commit fab664a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
5 changes: 5 additions & 0 deletions 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
Expand Down
3 changes: 0 additions & 3 deletions 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
Expand Down Expand Up @@ -39,8 +38,6 @@ module ClassMethods
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
Expand Down
13 changes: 10 additions & 3 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -5,6 +5,7 @@

require 'yaml'
require 'set'
require 'thread'
require 'active_support/benchmarkable'
require 'active_support/dependencies'
require 'active_support/descendants_tracker'
Expand Down Expand Up @@ -390,12 +391,18 @@ class Base

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)
Expand Down
8 changes: 8 additions & 0 deletions 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
Expand All @@ -19,6 +20,13 @@ def self.base_class; self; end

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

0 comments on commit fab664a

Please sign in to comment.