<div dir="ltr">Hi,<div><br></div><div>I'm a little confused about when gen_server:terminate/2 will be called. Mostly just in one area WRT a recommendation in LYSE, the "Clients & Servers" and the "Who Supervises the Supervisors" chapters to call trap_exit in the init/1 function. To quote:</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><p style="margin:0px 0px 1.4em;padding:0px 50px 0px 0px;border:0px;outline:0px;vertical-align:baseline;color:rgb(86,86,86);line-height:1.4em;background-image:initial;background-repeat:initial"><font face="arial, helvetica, sans-serif"><code style="margin:0px;padding:0px;border-width:0px 0px 1px;border-bottom-style:dotted;border-bottom-color:rgb(170,221,153);outline:0px;vertical-align:baseline;color:rgb(85,85,85);background-image:initial;background-color:rgb(255,255,255);background-repeat:initial">"terminate/2</code> will also be called when its parent (the process that spawned it) dies, if and only if the<span style="background-color:rgb(255,255,255)"> <code style="margin:0px;padding:0px;border-width:0px 0px 1px;border-bottom-style:dotted;border-bottom-color:rgb(170,221,153);outline:0px;vertical-align:baseline;color:rgb(85,85,85);background-image:initial;background-repeat:initial">gen_server</code> </span>is trapping exits."</font></p></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><p style="margin:0px 0px 1.4em;padding:0px 50px 0px 0px;border:0px;outline:0px;vertical-align:baseline;color:rgb(86,86,86);line-height:1.4em;background-image:initial;background-repeat:initial"><font face="arial, helvetica, sans-serif">and... </font></p><p style="margin:0px 0px 1.4em;padding:0px 50px 0px 0px;border:0px;outline:0px;vertical-align:baseline;color:rgb(86,86,86);line-height:1.4em;background-image:initial;background-repeat:initial"><font face="arial, helvetica, sans-serif"><span style="font-size:12.8000001907349px;line-height:17.9200000762939px">"Two things go on in the </span><code style="margin:0px;padding:0px;border-width:0px 0px 1px;border-bottom-style:dotted;border-bottom-color:rgb(170,221,153);outline:0px;font-size:1.1em;vertical-align:baseline;color:rgb(85,85,85);line-height:17.9200000762939px;background:rgb(238,255,204)">init/1</code><span style="font-size:12.8000001907349px;line-height:17.9200000762939px"> function. First we start trapping exits. If you recall the description of the </span><code style="margin:0px;padding:0px;border-width:0px 0px 1px;border-bottom-style:dotted;border-bottom-color:rgb(170,221,153);outline:0px;font-size:1.1em;vertical-align:baseline;color:rgb(85,85,85);line-height:17.9200000762939px;background:rgb(238,255,204)">terminate/2</code><span style="font-size:12.8000001907349px;line-height:17.9200000762939px"> from the </span><a class="" href="http://learnyousomeerlang.com/clients-and-servers" style="margin:0px;padding:0px;border:0px;outline:0px;font-size:12.8000001907349px;vertical-align:baseline;color:rgb(119,135,68);line-height:17.9200000762939px;background-image:initial;background-repeat:initial">Generic Servers chapter</a><span style="font-size:12.8000001907349px;line-height:17.9200000762939px">, we need to do this if we want </span><code style="margin:0px;padding:0px;border-width:0px 0px 1px;border-bottom-style:dotted;border-bottom-color:rgb(170,221,153);outline:0px;font-size:1.1em;vertical-align:baseline;color:rgb(85,85,85);line-height:17.9200000762939px;background:rgb(238,255,204)">terminate/2</code><span style="font-size:12.8000001907349px;line-height:17.9200000762939px"> to be called when the server's parent shuts down its children."</span></font></p></blockquote><div>The Erlang documentation on Supervisors states<font face="arial, helvetica, sans-serif"> "<span style="color:rgb(0,0,0)">Note that all child processes implemented using the standard OTP behavior modules automatically adhere to the shutdown protocol.". I'm having trouble reconciling these LYSE with the Erlang Supervisor docs. I'm assuming they're both correct and that the LYSE comment refers to gen_servers that aren't managed by a supervisor. Is this correct or is there another explanation for the seemingly contradictory statements?</span></font></div><div><font face="arial, helvetica, sans-serif"><span style="color:rgb(0,0,0)"><br></span></font></div><div><font face="arial, helvetica, sans-serif"><span style="color:rgb(0,0,0)">Thanks,<br>Rich</span></font></div></div>