Killing GenServer from the inside out

Consider this simple GenServer module:

defmodule Crap do
  use GenServer

  ... #All the needed crap

  def handle_info(:kill_me_pls, state) do
    GenServer.stop(self)
    {:noreply, state}
  end

  def terminate(_, state) do
    IO.inspect "Look! I'm dead."
  end

end

And consider adding these expressions to repl:

{:ok, pid} = Crap.start_link

send_message_to_that_pid

And this is where my thoughts start from, because it Process.alive? pidreturns true, but the process does not respond, and is terminatenot called, although if I call GenServer.stop(pid), the call in repl on the β€œclean” process (which does not receive the message about the kill) kills it properly. If the stop caused the process that received: the kill_me_pls message hung a replica.

+4
source share
1 answer

GenServer "" - {:stop, reason, new_state} handle_* {:stop, reason, reply, new_state} handle_call. , , , GenServer.stop GenServer, GenServer handle_info, stop, / .

:

def handle_info(:kill_me_pls, state) do
  {:stop, :normal, state}
end

, Return values

+7

Source: https://habr.com/ru/post/1656131/


All Articles