rails/rails-observers

ActionController::Caching::Sweeper controller instance is not thread safe

Angelmmiguel opened this issue · 1 comments

Hello,

We're using this gem to manage and clean the cache of our site. Thank you for it!

We use puma as the web server for Rails. In production, we configure it to use several threads. Some time ago, we started to get exceptions of an undefined expire_action method:

undefined method `expire_action' for #<ReleaseSweeper:0x000000073b69b8 @_routes=nil, @controller=nil> Did you mean? expire_api_actions

We've some loops in our sweeper and at some point, the @controller variable is lost between the different puma threads.

To solve this issue, we've applied this monkey patch from this comment.

# Fix https://github.com/rails/rails/issues/643
module ActionController::Caching
  class Sweeper < ActiveRecord::Observer
    def controller
      Thread.current["#{self.class.name}_controller"]
    end

    def controller=(controller)
      Thread.current["#{self.class.name}_controller"] = controller
    end

  private

    def method_missing(method, *arguments, &block)
      return if controller.nil?
      controller.__send__(method, *arguments, &block)
    end
  end
end

I see the comment is from 2011, but this code didn't end in the gem. So, I don't know if there's any reason for not adding this code.

Should I create a pull request?

Yes please