Skip to content
This repository
Browse code

Fix another race condition.

From 2c667f6.

Thanks @pwnall for the heads-up.

Conflicts:

	activerecord/lib/active_record/core.rb
  • Loading branch information...
commit fab664a8e9c94d7e3c52fed70ea2c0f569d142fb 1 parent f36dcaf
Jon Leighton authored January 20, 2012
5  activerecord/CHANGELOG.md
Source Rendered
... ...
@@ -1,3 +1,8 @@
  1
+## Rails 3.2.1 (unreleased) ##
  2
+
  3
+*   Fix possible race condition when two threads try to define attribute
  4
+    methods for the same class.
  5
+
1 6
 ## Rails 3.2.0 (January 20, 2012) ##
2 7
 
3 8
 *   Added a `with_lock` method to ActiveRecord objects, which starts
3  activerecord/lib/active_record/attribute_methods.rb
... ...
@@ -1,6 +1,5 @@
1 1
 require 'active_support/core_ext/enumerable'
2 2
 require 'active_support/deprecation'
3  
-require 'thread'
4 3
 
5 4
 module ActiveRecord
6 5
   # = Active Record Attribute Methods
@@ -39,8 +38,6 @@ module ClassMethods
39 38
       def define_attribute_methods
40 39
         # Use a mutex; we don't want two thread simaltaneously trying to define
41 40
         # attribute methods.
42  
-        @attribute_methods_mutex ||= Mutex.new
43  
-
44 41
         @attribute_methods_mutex.synchronize do
45 42
           return if attribute_methods_generated?
46 43
           superclass.define_attribute_methods unless self == base_class
13  activerecord/lib/active_record/base.rb
@@ -5,6 +5,7 @@
5 5
 
6 6
 require 'yaml'
7 7
 require 'set'
  8
+require 'thread'
8 9
 require 'active_support/benchmarkable'
9 10
 require 'active_support/dependencies'
10 11
 require 'active_support/descendants_tracker'
@@ -390,12 +391,18 @@ class Base
390 391
 
391 392
     class << self # Class methods
392 393
       def inherited(child_class) #:nodoc:
393  
-        # force attribute methods to be higher in inheritance hierarchy than other generated methods
394  
-        child_class.generated_attribute_methods
395  
-        child_class.generated_feature_methods
  394
+        child_class.initialize_generated_modules
396 395
         super
397 396
       end
398 397
 
  398
+      def initialize_generated_modules #:nodoc:
  399
+        @attribute_methods_mutex = Mutex.new
  400
+
  401
+        # force attribute methods to be higher in inheritance hierarchy than other generated methods
  402
+        generated_attribute_methods
  403
+        generated_feature_methods
  404
+      end
  405
+
399 406
       def generated_feature_methods
400 407
         @generated_feature_methods ||= begin
401 408
           mod = const_set(:GeneratedFeatureMethods, Module.new)
8  activerecord/test/cases/attribute_methods/read_test.rb
... ...
@@ -1,5 +1,6 @@
1 1
 require "cases/helper"
2 2
 require 'active_support/core_ext/object/inclusion'
  3
+require 'thread'
3 4
 
4 5
 module ActiveRecord
5 6
   module AttributeMethods
@@ -19,6 +20,13 @@ def self.base_class; self; end
19 20
 
20 21
           include ActiveRecord::AttributeMethods
21 22
 
  23
+          def self.define_attribute_methods
  24
+            # Created in the inherited/included hook for "proper" ARs
  25
+            @attribute_methods_mutex ||= Mutex.new
  26
+
  27
+            super
  28
+          end
  29
+
22 30
           def self.column_names
23 31
             %w{ one two three }
24 32
           end

0 notes on commit fab664a

Please sign in to comment.
Something went wrong with that request. Please try again.