ruby-on-railscachingfile-ownership

Rails 6 application fails due to cache directory changing ownership to root


I have a Rails 6 application running on Debian buster. In one place I am using "low-level" caching. Here is the relevant code:

# Get the value.
def self.ae_enabled?()
  Rails.cache.fetch("ae_enabled", expires_in: 1.hour)
end

# Change the value.
def self.ae_toggle()
  ac = AdminConfiguration.find_by(name: "ae-enabled")                    

  ac.value = ! ac.value
  ac.save()                        

  # Invalidate the cache.
  Rails.cache.delete("ae_enabled")        

  return ac
end

This works fine ... for a while. At some point, and for reasons I cannot figure out, the cache directory tmp/cache/3F1/ where the above value is cached changes ownership from www-data:www-data (the user Apache runs under) to root:root. Once this happens Apache can no longer read this cached value and the application throws an error.

The odd thing is none of the other directories under tmp/cache/ have their permissions change, it is only the one associated with this low-level cache.

Why is that particular cache directory changing ownership?

Technical details: Rails version 6.0.3.3.


Solution

  • Apache usually does not relate to rails cache, unless you're using passenger, in which case it may be passenger's bug/misconfiguration, check if user sandboxing is enabled and configured correctly.

    A typical rails deployment usually has multiple processes:

    1. a web server handling static files and proxying requests to rails (usually nginx, you've mentioned apache)
    2. rails web server (in case of passenger - "inside" the previous, but in fact there's still a child process)
    3. some background workers or processes run from cron

    File ownership confusion most probably originates from one of the above writing to disk while running under a different os user.

    Look into how your processes are started. First suspect is some cron job that may be configured as system-wide, these run under root.