Skip to content

Instantly share code, notes, and snippets.

@kevgathuku
Created May 3, 2026 20:24
Show Gist options
  • Select an option

  • Save kevgathuku/b3276dfade227b4e588c016bb235a7e1 to your computer and use it in GitHub Desktop.

Select an option

Save kevgathuku/b3276dfade227b4e588c016bb235a7e1 to your computer and use it in GitHub Desktop.

Instead of using a custom implementation to get the state of a process:

defmodule MyModule do
 use GenServer

  def report(server) do
    GenServer.call(server, :report)
  end

  # More functions

  @impl true
  def handle_call(:report, _from, state) do
    # No modifications. Return the current state
    {:reply, state, state}
  end
end

This is all you need to do instead

{:ok, pid} = MyModule.start_link([])
# ... some more operations...
:sys.get_state(pid)

Well, it does say right there in the get_state docs that the whole purpose is to help the users not to reimplement it.

Now I know and so do you 😀

More resources:

https://hexdocs.pm/elixir/GenServer.html#module-debugging-with-the-sys-module

https://www.erlang.org/doc/man/sys.html#module

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment