Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling the cleanup of the gen_server state
    primarykey
    data
    text
    <p>I have a <code>gen_server</code> running which it must clean up its state whenever it is stopped normally or it crash unexpectedly. The cleanup basically consists in deleting a few files.</p> <p>At this moment, when the <code>gen_server</code> crash or it is stopped normally, the cleanup is done in <code>terminate/2</code>.</p> <p>Is there any reason why <code>terminate/2</code> would not be called if the <code>gen_server</code> crash?</p> <p>Should be any other process monitoring the <code>gen_server</code> waiting to do the cleanup if the <code>gen_server</code> dies unexpectedly?</p> <p>So, the code is like this:</p> <pre><code>terminate(normal, State) -&gt; % Invoked when the process stops % Clean up the mess terminate(Error, State) -&gt; % Invoked when the process crashes % Clean up the mess </code></pre> <hr> <p>EDIT: I found this email in the official mailing list which is talking about the same thing: </p> <p><a href="http://groups.google.com/group/erlang-programming/browse_thread/thread/9a1ba2d974775ce8" rel="noreferrer">http://groups.google.com/group/erlang-programming/browse_thread/thread/9a1ba2d974775ce8</a></p> <p>As Adam says below, if we want to avoid to trap the exists in the <code>gen_server</code>, we could use different approaches.</p> <p>But if we trap the exists, <code>terminate/2</code> seems to be a safe place to do the cleanup as it always will be called. Furthermore we must handle correctly when <code>'EXIT'</code> is sent to <code>terminate/2</code> and to <code>handle_call/3</code> trying to propagate the errors correctly between workers and supervisors.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload