larshei

larshei

Trapping Exits / Gracefully shutting down GenServers

I am currently scratching my head, because I am already trapping exits at one point, but I am not able to get it to work at another point.

Working Setup

In a project, I have the following setup:

  1. DynamicSupervisor :my_supervisor as part of the applications Supervision Tree
  2. A GenServer module MyServer.

:my_process is started using MyServer.start(), which looks like this:

  def start(name) do
    Logger.debug "#{__MODULE__} - #{name} starting"

    DynamicSupervisor.start_child(
      :my_supervisor,
      {
        __MODULE__,
        name
      }
    )
  end

so the Process should be attached to the Supervisor.
Then, in the MyServer.init(), exits are trapped:

  ...
  Process.flag(:trap_exit, true)
  ...

and a handle_info for handle :EXIT signals:

  def handle_info({:EXIT, _pid, reason}, state) do
    Logger.warn("#{state.fleet_name} - Stopped with reason #{inspect reason}")
    {:stop, reason, state}
  end

This all works as expected. If I run e.g. :init.stop(), the Logger warning is printed.

Non-working Setup

The setup is almost the same:

  1. DynamicSupervisor :tracker_supervisor as part of the applications Supervision Tree
  2. A GenServer module TrackerServer in a separate library

In the library:

defmodule MyLibrary.TrackerServer do
  def start_link(args) do
    GenServer.start_link(
      __MODULE__,
      args,
      name: args[:name] || __MODULE__
    )
  end

  def init(args) do
    ...
    Process.flag(:trap_exit, true)
    ...
    {:ok, initial_state}
  end

  def handle_info({:EXIT, _from, reason}, state) do
    Logger.warn("Tracking #{state.name} - Stopped with reason #{inspect reason}")
  end
end

In my project, i wrapped the GenServer.start_link() in a start() function:

  def start(name) do
    Logger.debug "#{name} - Starting Tracking"

    result = DynamicSupervisor.start_child(
      :tracker_supervisor,
      {
        MyLibrary.TrackerServer,
        [
          name: {:via, Registry, {MyRegistry, name}},
          ...
        ]
      }
    )
  end

The two examples are not unrelated.

The first example MyServer is running in my application and in its init function, the TrackerServer Process is also started. MyServer forwards some data to TrackerServer, but there is no real dependency between those two, e.g. when the TrackingServer crashes and does not exist, it is not a problem. It should be restarted by the supervisor and things should go back to normal afterwards. Tracking is mostly for statistics and missing some information here is not critical.

However, I would like to persist the state on :EXIT of the TrackerServer to actually keep what was tracked so far. But it seems the Process :EXIT handle info is never called.

I do not understand why one version works and the other does not :thinking: What am I missing here? :confused:

Marked As Solved

lud

lud

Is your process linked to another process that is not the supervisor ?

If you save the following code to an .exs file and execute it you will see that the handle_info/2 function is not called, because exit messages from the parent (in that case, the supervisor) are handled by the GenServer module itself, and not your code. But GenServer will call your terminate/2 callback.

defmodule Child do
  require Logger
  use GenServer

  def start_link(arg) do
    GenServer.start_link(__MODULE__, arg)
  end

  def init(arg) do
    Process.flag(:trap_exit, true)
    {:ok, arg}
  end

  def handle_info({:EXIT, _pid, reason}, state) do
    Logger.warn("#{inspect(reason)} in handle_info")
    {:stop, reason, state}
  end

  def terminate(reason, _state) do
    Logger.warn("#{inspect(reason)} in terminate")
  end
end

{:ok, sup} = DynamicSupervisor.start_link(strategy: :one_for_one)

{:ok, _pid} = DynamicSupervisor.start_child(sup, {Child, "a"})

Supervisor.stop(sup)

So, when :init.stop() is called, is it possible that the exit message that you receive is from another worker or something?

Also Liked

larshei

larshei

Oh you are right!

I can see that I once knew this, because the working implementation actually has the terminate callback implemented (which calls the same functions as the :EXIT handle_info) but I forgot about this :sweat_smile:

Thanks!

Last Post!

larshei

larshei

Oh you are right!

I can see that I once knew this, because the working implementation actually has the terminate callback implemented (which calls the same functions as the :EXIT handle_info) but I forgot about this :sweat_smile:

Thanks!

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement