Gen_server respawn a new iex session in case of exception

I forgot to return the expected return value in handle_call callback and when I called a function that triggered the callback, it failed with an error and gave me a completely new iex session.

Does gen_server respawn a new process if there is an exception? I thought I had to implement Supervisors to respawn a new process in case of failure.

iex:2> {:ok, cache} = Cache.start_link
{:ok, #PID<0.134.0>}
iex:3> cache
#PID<0.134.0>
iex:4> Cache.write(:stooges, [23])    

19:12:38.733 [error] GenServer CE terminating
** (stop) bad return value: %{stooges: [23]}
Last message (from #PID<0.130.0>): %{stooges: [23]}
State: %{}
Client #PID<0.130.0> is alive

    (stdlib) gen.erl:167: :gen.do_call/4
    (elixir) lib/gen_server.ex:1006: GenServer.call/3
    (stdlib) erl_eval.erl:680: :erl_eval.do_apply/6
    (elixir) src/elixir.erl:275: :elixir.eval_forms/4
    (iex) lib/iex/evaluator.ex:257: IEx.Evaluator.handle_eval/5
    (iex) lib/iex/evaluator.ex:237: IEx.Evaluator.do_eval/3
    (iex) lib/iex/evaluator.ex:215: IEx.Evaluator.eval/3
    (iex) lib/iex/evaluator.ex:103: IEx.Evaluator.loop/1
** (EXIT from #PID<0.130.0>) shell process exited with reason: bad return value: %{stooges: [23]}

After the crash

Interactive Elixir (1.9.4) - press Ctrl+C to exit (type h() ENTER for help)
        `
       ;:
      `+;
      ++',
     .+++,
     :'++'.
    .+'++';       Welcome to elixir-land
    ,+'+''':
   `:+'+';';;     http://elixir-lang.org 
   ,:++';;;;'.    http://www.phoenixframework.org/
   ::'+';;;;''    https://hex.pm/
   :;;++;;';''    http://www.elixirschool.com/
   .;;'''';;;:    https://github.com/christopheradams/elixir_style_guide
    ,:::'';,,.
    ,,,,,:::,
     ,,,,,,,
      ``.`

iex:1> cache
** (CompileError) iex:1: undefined function cache/0

It completely spawned a new iex session where I cant even access the cache variable

UPDATE

Ok I get it. It’s because I used start_link which linked the Cache pid into my iex session. Andthat’s the reason why my iex session was killed when there was an error in Cache

Yep, that links them together so one going down takes the other down too. ^.^

1 Like