You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Let me preface this by saying that this is probably pretty non-standard, but I discovered a bug that is pretty reproducible and it occurs when we're trying to operate two state machines at the same time.
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.7.1'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.3', '>= 6.0.3.4'
# Use sqlite3 as the database for Active Record
gem 'sqlite3', '~> 1.4'
# Use Puma as the app server
gem 'puma', '~> 4.1'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem "state_machines-activerecord", "~> 0.6.0"
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '~> 3.2'
end
class Vehicle < ActiveRecord::Base
state_machine initial: :parked do
state :parked, :driving
event :start do
transition any => :driving
end
event :park do
transition any => :parked
end
before_transition any => :parked do |vehicle, transition|
p :before_transition
p transition
p transition.object_id
p "-"*50
vehicle.location_state_event = "garage"
end
after_transition any => any do |vehicle, transition|
p :after_transition
p transition
p transition.object_id
p "-"*50
end
end
state_machine :location_state, initial: :garaged do
state :garaged, :outside
event :garage do
transition any => :garaged
end
event :drive do
transition any => :outside
end
before_transition any => any do |vehicle, transition|
p :before_transition_location
p transition
p transition.object_id
p "-"*50
end
after_transition any => any do |vehicle, transition|
p :after_transition_location
p transition
p transition.object_id
p "-"*50
end
end
end
Now run the following commands in the rails console in this order
v = Vehicle.create!
v.drive!
v.start!
v.park
here's what I get. Note the following before you jump in
the transition objects (before and after) for a given state machine are usually the same, you can tell by their matching object_ids
the first time we park the vehicle, you'd expect to see an after_transition object output for the location_state machine. there isn't one
when we park the vehicle a second time, we then see an after_transition object output, but the transition object in the before_transition and the after_transition are not the same, and if you look at the data in them, the transition object in the after_transition, is containing the data for the PREVIOUS transition
if you continue to park the vehicle the after_transition object, will consistently have the transition object for the PREVIOUS transition.
I know this is non-standard, so if you could just give me some pointers for where to look to dig a little deeper on this I'd really be grateful. I started to look at the code here, but I wasn't immediately able to identify the StateMachine::Transition code to delve a bit deeper into this.
Thanks!
The text was updated successfully, but these errors were encountered:
concept47
changed the title
off by one transition bug, when using two state machines at the same time.
Off-by-one after_transition bug, when using two state machines at the same time.
Nov 19, 2020
Let me preface this by saying that this is probably pretty non-standard, but I discovered a bug that is pretty reproducible and it occurs when we're trying to operate two state machines at the same time.
Here is an example app created with this command
Gemfile looks like this
Now run the following commands in the rails console in this order
here's what I get. Note the following before you jump in
I know this is non-standard, so if you could just give me some pointers for where to look to dig a little deeper on this I'd really be grateful. I started to look at the code here, but I wasn't immediately able to identify the StateMachine::Transition code to delve a bit deeper into this.
Thanks!
The text was updated successfully, but these errors were encountered: