skip_callback and set_callback are NOT thread safe. I was able to confirm this by trying to create some entries in sidekiq (a threaded asynchronous job processor). As soon as I activate callbacks again, a race condition arises that results in callbacks being called. If I comment on the re-activation code of the callback, there is no problem.
I found several possible solutions to the problem, including two stones:
- gemstone 'sneaky-save'
- gem 'skip_activerecord_callbacks'
The hidden gem seems the most direct and intentional. The gem significantly circumvents ActiveRecord storage methods and performs direct sql.
This is also the only thing I can say with confidence that it is thread-safe. It is also a very small and understandable stone. The disadvantage is that it does not cause a check. Thus, you will need to invoke checks yourself.
Anand A. Byte has collected a large number of possible options. I am skeptical that all five options are thread safe. The two gems mentioned above are listed along with other possible options in Ananda's post here: http://www.allerin.com/blog/save-an-object-skipping-callbacks-in-rails-3-application/
source share