Reputation: 1234
Is Rails 3 cattr_accessor thread-safe?
Upvotes: 5
Views: 1372
Reputation: 1035
No, it's not. Just get a quick look at the cattr_reader code:
# File activesupport/lib/active_support/core_ext/class/attribute_accessors.rb, line 28
def cattr_reader(*syms)
options = syms.extract_options!
syms.each do |sym|
class_eval( unless defined? @@#{sym} @@#{sym} = nil end def self.#{sym} @@#{sym} end, __FILE__, __LINE__ + 1)
unless options[:instance_reader] == false
class_eval( def #{sym} @@#{sym} end, __FILE__, __LINE__ + 1)
end
end
end
And you could run simple test:
class A
cattr_accessor :b
end
t1 = Thread.new { A.b = 1; sleep 1; p (A.b == 1); }
t2 = Thread.new { A.b = 2 }
t1.join
t2.join
# outputs "false"
Here is a way to make it work thread-safely: http://rails-bestpractices.com/posts/2010/08/23/fetch-current-user-in-models/
Upvotes: 4
Reputation: 9093
Any modification of variables at a class level cannot be safe since you are sharing state at a level to which any thread can modify.
So cattr_accessor
, mattr_accessor
, @@var
and $var
are NOT thread safe.
Upvotes: 2