Closed jemc closed 9 years ago
This has got me wondering if some weird problems I'm having is with the lack of thread safety in state machine. Currently I'm running it in production with Rails 4 (i.e thread_safe!). :fearful:
@sdbondi - yeah, this gem is most certainly not thread safe :wink:. I ended up using other gems, though, so I don't care much about this issue ticket anymore.
@jemc Are you using your hegemon gem?
Actually, in the end I ended up using micromachine and a Mutex
. :wink:
@jemc Thanks :+1: The state changes currently occur in my multi threaded workers so I've put a class level Mutex around the operations and added some more processes - which should at least get me though the transition to another solution.
So, I realize that state_machine makes no claim to being thread-safe, and I realize that implementing thread-aware protections into state_machine is not a trivial task, especially if you wanted the feature to be comprehensive enough to actually start claiming to be thread-safe. I also realize that such a feature could actually have a negative performance impact on those who are only accessing their Machines from one thread and therefore have no use for this feature. So I'm no asking for it to be comprehensive, and I'm not asking for it to be mandatory. I'm hoping to get some kind of opt-in thread protection specifically for transitions.
To be clear, what I am asking for is the option to make the Machine helpers that event creates (https://github.com/pluginaweek/state_machine/blob/master/lib/state_machine/event.rb#L236-L254) wrapped in a Machine-level Mutex (or Monitor, if they need to be re-entrant). While this may not solve all of the possible issues you may have while accessing a Machine from multiple threads, it solves the problems I have with my use case.
If you don't want to include that opt-in mechanism, I'd ask for some documented API by which I could make this happen in my code without monkey-patching your internals (or having to wrap the Machine in a delegator object).
Let me know what your thoughts are on this.
Here is my current monkey-patched hack I've been playing with: