Make aasm thread-safe. (#442)

Ever since AASM supported multiple named state machines in a single
class, it has accessed and stored its state machines in class variables.
However accesses to the state machines are not thread-safe causing
exceptions when multiple threads attempt to access the same state
machine at the same time during lazy initialization.
Using a Concurrent Map makes calls to state machines, and lazy
initialization thread safe.
This commit is contained in:
Reid Morrison 2017-03-16 03:28:15 -04:00 committed by Anil Kumar Maurya
parent 06d3e23a05
commit a8a8f10d16
2 changed files with 7 additions and 2 deletions

View File

@ -16,6 +16,8 @@ Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.required_ruby_version = '>= 1.9.3'
s.add_dependency 'concurrent-ruby', '~> 1.0'
s.add_development_dependency 'rake'
s.add_development_dependency 'sdoc'
s.add_development_dependency 'rspec', ">= 3"

View File

@ -1,8 +1,11 @@
require 'concurrent'
module AASM
class StateMachineStore
@stores = Concurrent::Map.new
class << self
def stores
@stores ||= {}
@stores
end
# do not overwrite existing state machines, which could have been created by
@ -38,7 +41,7 @@ module AASM
end
def initialize
@machines = {}
@machines = Concurrent::Map.new
end
def clone