Surprising behaviour when recompiling in iex with a running process

Recently I tried “recompile” function/macro in iex out of curiosity (usually I just ctrl+c twice and run previous command) and I just can’t wrap my head around it.

Consider following example GenServer:

defmodule App.Worker do
  use GenServer

  def start_link(_) do
    GenServer.start_link(__MODULE__, nil)

  @impl true
  def init(_) do
    send(self(), :increment)
    {:ok, 1}

  @impl true
  def handle_info(:increment, state) do
    Process.send_after(self(), :increment, 1000)
    state = state + 1
    IO.inspect("Counter: #{state}, process: #{self() |> inspect}")
    {:noreply, state}


If I start it with iex -S mix it prints a sequence of 1,2,3 etc. All good. Now I edit the code to add 10 instead of 1 on each increment, recompile() in iex, and I see that new code runs, but with OLD state. E.g. if I recompile when the counter was at 11, next output will be 21.

It seems that the process and the state that it was is not restarted with recompile (same pid, same state), but new updated functions work with the same process and state.

I can’t seem to find anything in documentation that would describe this behaviour.

I thought that it was the famous “hot code reload”, I tried implementing code_change callback in this GenServer, but it seems it is never called on recompile, so it must be something else.

recompilation on IEX is not the same as hot code reload. recompilation just change the module code loaded in memory while hot code reload have more processes there that gonna trigger the code_change call.

that’s the expected behaviour. if you want to change the state you need to start a new gen server.

there is no explicit place talking about that in particular but in the docs are explanations that goes around that expected behaviour:


In terms of code_change, there is discussion here about that (sorry, I don’t know much about it).

Otherwise, it sounds like you have the fairly common erroneous mental model that modules “own” processes. The two are actually unrelated. While the GenServer pattern does associate a module with a process that is merely an application pattern not enforced by the VM or anything like that. In the case of a GenServer, you’re just telling the process “Hey, call these function from this module when certain events happen,” but there is nothing stopping you from running unrelated code on that same process. All that to say that if a module changes, that has no inherent bearing on the process itself.