Let’s say I’ve got a Ruby class in my Rails project that is setting an instance variable.
class Something
def self.objects
@objects ||= begin
# some logic that builds an array, which is ultimately stored in @objects
end
end
end
Is it possible that @objects could be set multiple times? Is it possible that during one request, while executing code between the begin/end above, that this method could be called during a second request? This really comes down to a question of how Rails server instances are forked, I suppose.
Should I instead be using a Mutex or thread synchronization? e.g.:
class Something
def self.objects
return @objects if @objects
Thread.exclusive do
@objects ||= begin
# some logic that builds an array, which is ultimately stored in @objects
end
end
end
end
It’s possible (and desirable) to run Rails in a multi-threaded mode even in MRI. This can be accomplished by changing a line in
production.rb.In MRI, two threads cannot run code simultaneously, but a context switch can happen at any time. In Rubinius and JRuby, threads can run code simultaneously.
Let’s look at the code you showed:
The
||=code gets expanded to something like:This means that there are actually two steps to the process:
@objects@objectsis falsy, set@objectsto the results of thebegin/endexpressionIt may be possible for the context to switch between these steps. It is certainly possible for the context to switch in the middle of step 2. This means that you may end up running the block multiple times instead of once. In MRI, this may be acceptable, but it’s perfectly straight forward to lock a mutex around the expression, so do it.