minu

minu

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)
  end

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

  @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}
  end

end

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.

Showing Posts 2 to 1

sodapopcan

sodapopcan

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.

cevado

cevado

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:

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews