CredoHotUpgrades.Check.CacheWithoutCodeChange (CredoHotUpgrades v1.0.0)

Copy Markdown View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

This check has a base priority of normal and works with any version of Elixir.

Explanation

A GenServer that fills a :persistent_term or an ETS table keeps a cache outside of its own state. A hot upgrade replaces the code and leaves that cache as the old code wrote it:

def handle_info(:refresh, state) do
  :persistent_term.put(@key, %{users: load_users()})
  {:noreply, state}
end

A release that adds roles: to that map, and reads it, is installed while the map still has no such key. Every reader raises until the cache is next refreshed, minutes later for a cache on a timer, never for one filled once in init/1.

Define code_change/3 and rebuild the cache there. The release handler calls it for this process while the upgrade is being installed:

def code_change(_old_vsn, state, _extra) do
  :persistent_term.put(@key, build())
  {:ok, state}
end

That shortens the time the new code meets the old shape from the refresh interval to a fraction of a second, it does not remove it: the modules that read the cache can be loaded a few instructions before this callback runs. A reader that must never fail reads the new key with a default, Map.get(cache, :roles, []), for the one release that adds it.

code_change/3 is only called in a release where this module changed. Keep what decides the shape of the cache in this module, so that a release that changes the shape changes the module too.

This check reports a module that uses GenServer, writes with :persistent_term.put/2 or :ets.insert/2, and defines no code_change/3. It does not look at what the cache holds, a cache whose shape never changes is reported too.

Check-Specific Parameters

There are no specific parameters for this check.

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.