Hermanverschooten

Hermanverschooten

Ctrl-g q in dev and GenServer termination

Hi,

This has probably been asked a gazilion times…

Is there a reilable way to trap the stop of a GenServer in development, that means when you either stop using ctrl-g q or ctrl-c ctrl-c?

What I have tried:

  • Process.flag(:trap_exit,true) in init/1, with terminate/2
  • Process.monitoring from init/1 and handle_info/2
  • Spawning a process that monitors with receive

None of them worked.

On start of the GenServer I am registering with an external site and I want to unregister on stop.

This is only for development.

Most Liked

rvirding

rvirding

Creator of Erlang

From the very beginning processes did not have built parent-child relationships. It did keep processes very simple and it allowed us to work on how to build systems. The ideas of how to structure systems developed as our understanding of the problem grew and what was needed to build reliable systems that could handle errors in the right way.

We were working together with another group in Ericsson who were looking at designing a new architecture and used Erlang for experimenting. As they were very knowledgeable about such systems they came back with comments and ideas about how they thought they should look and work and what Erlang needed. Out this grew error handling and what later became supervisors. They also actually developed the first system Ericsson using Erlang!

Note that these ideas evolved before OTP was built and most of the ideas that became formalised and described in OTP came from these ideas. For examples we had of course servers before we got OTP gen_server and we were managing processes and restarting them when necessary before we had supervisors.

One very good thing with OTP is that they “packaged” these things and bought order into the whole library and system structure. This is an ENORMOUS benefit when building systems. You cannot overate it.

rvirding

rvirding

Creator of Erlang

From iex you can do :erlang.exit(pid(0,0,0), :kill) which will unconditionally terminate the system but generate a crash dunp in the file erl_crash.dump. Don’t ask :wink: :grinning_face:

rvirding

rvirding

Creator of Erlang

The init module is preloaded into the BEAM and is the module that is automatically run by the BEAM when it starts its first process, the one that is <0.0.0>, calling the function init:boot(BootArgs) and the first thing it does is to register itself under the name init. It then traps exits. Note that although it a “special” process in how it is started and the module it runs it is actually just a normal Erlang process which is why you can kill it by sending it kill signal which is untrappable.

Actually all ALL processes in Erlang are equal and there are no special processes in any sense. Erlang is a very egalitarian in system in the sense that there are no special process and no special modules. All processes are equal and all modules are equal. You can kill or delete anything. :wink: :grinning_face:

Hermanverschooten

Hermanverschooten

Yeah I figured.

I am adding this to my .iex.exs:

defmodule MyIEX do
  def exit, do: System.stop()
end

import MyIEX

Now I just have to remember to type exit when I quit.

Thanks.

rvirding

rvirding

Creator of Erlang

Sorry I wasn’t quite right here, for correct info check

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement