CredoHotUpgrades
Credo rules that catch Elixir code that cannot survive a BEAM hot code upgrade.
A hot upgrade replaces the code of a running module without restarting the node, which means some patterns that are harmless in an ordinary release become real bugs: a closure that outlives the module version that built it, a process that never re-enters its loop through the module, a side effect that silently runs a second time.
Every rule here catches one of those patterns in source, before a release is even built, the same way mix credo already catches other classes of bugs.
The mechanism itself (appup, relup, release_handler) and the practices a hot-upgradable project follows are documented in deployex/beam-hotupgrades.
This package exists to check a codebase against that guide automatically, in CI and in an editor, rather than by reading it every time.
Status: early. The checks are pattern-based, like the rest of Credo, not a full data-flow analysis. They catch what they are looking for and nothing else, read a rule's explanation for what that is before trusting a clean run.
What this is not
This is a linter, not an upgrade mechanism.
It has no node, builds nothing, and boots nothing.
It will not tell you that a struct gained a field with no matching code_change/3, or that a supervisor's child list changed between two releases, those are a diff between two builds, which is Jellyfish's job, verified for real by relup-guard-elixir booting an actual release and applying an actual upgrade.
This package only looks at one version of the source, for patterns that are wrong regardless of what the other version looks like.
Run it first, in the same step that already compiles the project, because it costs milliseconds.
Run relup-guard-elixir after, because it costs a real release build and boot, and because it catches a different, overlapping-but-not-identical class of bug.
Install
# mix.exs
{:credo, "~> 1.7", only: [:dev, :test], runtime: false},
{:credo_hot_upgrades, "~> 0.1", only: [:dev, :test], runtime: false}
Add the plugin to your .credo.exs (creating one if you have none):
%{
configs: [
%{
name: "default",
plugins: [{CredoHotUpgrades.Plugin, []}]
}
]
}
The plugin registers every check below, there is no list to maintain by hand, and a new check arrives with a credo_hot_upgrades upgrade.
Then, as part of the step that already compiles the project:
mix compile && mix credo --strict
Turn a specific rule off the way you would any Credo check:
checks: %{disabled: [{CredoHotUpgrades.Check.NoNif, []}]}
Silence a single line you know is fine the same way:
# credo:disable-for-next-line CredoHotUpgrades.Check.ClosureInState
The checks
Each one is a real, verified failure mode, not a guess. The order follows where each pattern appears in deployex/beam-hotupgrades, so the two can be read side by side. Where a check's finding was checked against OTP source or a live node rather than assumed, that is noted.
| id | check | catches |
|---|---|---|
CHU0001 |
CompileTimeEnv |
a module attribute that reads Application.get_env/System.get_env at compile time, baking in the build machine's value |
CHU0002 |
ClosureInState |
a closure kept in the state a GenServer callback returns, in :ets, :persistent_term, Process.put, or a message |
CHU0003 |
TelemetryLocalHandler |
:telemetry.attach/attach_many given a local or anonymous handler, which fails after the module is replaced |
CHU0004 |
LoggerLocalFilter |
:logger.add_primary_filter/add_handler_filter given a local or anonymous filter, removed silently on the same failure |
CHU0005 |
ClosureInOptions |
a closure kept in a child spec or a library's own options (configure:, tag_values:, and the rest) |
CHU0006 |
OnLoad |
@on_load, which runs again on every hot upgrade that loads the module, not only at boot |
CHU0007 |
LocalReceiveLoop |
a hand-written loop that calls itself by its bare name inside a receive, and never leaves the code version it started on |
CHU0008 |
LocalAcceptLoop |
the same problem, for a loop built around :gen_tcp.accept/:ssl.transport_accept/:socket.accept instead of receive |
CHU0009 |
ProcessDictionaryState |
Process.put/2 in a GenServer callback, data code_change/3 can never see or migrate |
CHU0010 |
ApplicationStartSideEffects |
a side effect in Application.start/2 that a hot upgrade will never re-run |
CHU0011 |
NoNif |
a module that loads a NIF, whose native code a hot upgrade cannot replace in place |
CHU0012 |
SupervisorInitSideEffects |
a side effect in a supervisor's init/1, which a hot upgrade of that supervisor runs a second time |
CHU0013 |
LiveViewClosure |
a closure kept in a LiveView socket by attach_hook, assign, or the dom_id: of a stream, which crashes the open page after its module is replaced |
CHU0001, CHU0004, CHU0006, CHU0009 and CHU0012 were each checked against OTP 29 source and a live BEAM node before being written: a module attribute really does freeze its build-time value, :logger really does remove a filter that raises the same way Telemetry detaches a failing handler, @on_load really does run again on a reload, code_change/3 really does see only the state a callback returns and never the process dictionary, and supervisor:code_change/3 really does call Module:init(Args) again.
What it does not catch
Anything that needs two versions to compare: a struct that gained a field with no matching code_change/3, a child added to or removed from a supervisor, a consolidated protocol a release adds.
Those are Jellyfish's and relup-guard's job, not this one's, see What this is not.
A function passed as an attribute inside a ~H template, <.table sort={&sort/2}>, is not seen: the template is a string to Credo, not code.
A closure stored through several indirections (a variable built in one function, passed through two more, then stored in a fourth) may not be caught, each rule matches the shapes in its own examples, not every path a value could travel to reach the same sink.
Releasing
Bump @version in mix.exs in its own PR, merge it to main, then from main:
mix release
Tags the commit, pushes it and the tag, and publishes to Hex.
License
MIT.