<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Sep 7, 2014 at 6:29 PM, Justin HANEKOM <span dir="ltr"><<a href="mailto:justin.hanekom@rogers.com" target="_blank">justin.hanekom@rogers.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="color:rgb(0,0,0);font-size:13px;font-family:HelveticaNeue,'Helvetica Neue',Helvetica,Arial,'Lucida Grande',sans-serif;font-style:normal;background-color:transparent"><span>I'm trying to create a file server that loops, responding to 2 requests: list_dir; and read_file. For some reason the server loops sometimes, but for some unknown reason after successfully  </span><span style="background-color:transparent">responding to a read_file request does not receive/respond to any further requests.</span></div><div style="color:rgb(0,0,0);font-size:13px;font-family:HelveticaNeue,'Helvetica Neue',Helvetica,Arial,'Lucida Grande',sans-serif;font-style:normal;background-color:transparent"></div></blockquote></div><br>Hi!</div><div class="gmail_extra"><br></div><div class="gmail_extra">I just tried your code here and it seems to work like it should, in the happy-path case.</div><div class="gmail_extra"><br></div><div class="gmail_extra">Where your strategy seems to go a bit off is that you have no way of knowing if your process is still alive, and if it dies, why it died. This is where you want to use a supervisor to keep your process alive, and handle errors. Alternatively, you want to have a monitoring process using erlang:monitor(Process, Pid) in order to figure out if your server exits. For quick debugging, you can easily check for its status:</div><div class="gmail_extra"><br></div><div class="gmail_extra"><div class="gmail_extra">Eshell V6.1.2  (abort with ^G)</div><div class="gmail_extra">1> afile_server:start(".").</div><div class="gmail_extra"><0.44.0></div><div class="gmail_extra">2> exit(v(1), foo).</div><div class="gmail_extra">true</div><div class="gmail_extra">3> </div><div class="gmail_extra">3> is_process_alive(v(1)).</div><div class="gmail_extra">false</div><div><br></div></div><div class="gmail_extra"><div>but note that for a serious server, you need to have someone watching over your process and report if it goes wrong along the way. There is really no other way. You may be lured into having the process itself catch errors, but this leads to convoluted code quickly:</div><div><br></div><div>start(Dir) -></div><div>   spawn(fun() -> try loop(Dir) catch Class:Error -> error_logger:error_report([{class, Class}, {reason, Reason}]) end end).</div><div><br></div><div>(not tested, but you get the gist of it, surely).</div><div><br></div><div>Yet another way, is to trace any event on the server process:</div><div><br></div><div><div>Eshell V6.1.2  (abort with ^G)</div><div>1> Pid = afile_server:start().</div><div>** exception error: undefined function afile_server:start/0</div><div>2> Pid = afile_server:start(".").</div><div><0.46.0></div><div>3> dbg:tracer().</div><div>{ok,<0.48.0>}</div><div>4> dbg:p(Pid, all).</div><div>{ok,[{matched,nonode@nohost,1}]}</div><div>5> Pid ! {self(), list_dir}.</div><div>(<0.46.0>) << {<0.44.0>,list_dir} (Timestamp: {1410,108607,886896})</div><div>(<0.46.0>) in {afile_server,loop,1} (Timestamp: {1410,108607,886905})</div><div>(<0.46.0>) <0.19.0> ! {'$gen_call',{<0.46.0>,#Ref<0.0.0.76>},{list_dir,"."}} (Timestamp: {1410,</div><div>                                                                                          108607,</div><div>                                                                                          886912})</div><div>(<0.46.0>) out {gen,do_call,4} (Timestamp: {1410,108607,886915})</div><div>(<0.46.0>) << {#Ref<0.0.0.76>,</div><div>               {ok,["transit-erlang","afile_server.erl","afile_server.beam",</div><div>                    "zoo.txt"]}} (Timestamp: {1410,108607,887026})</div><div>(<0.46.0>) in {gen,do_call,4} (Timestamp: {1410,108607,887075})</div><div>(<0.46.0>) <0.44.0> ! {<0.46.0>,</div><div>                       {list_dir,{ok,["transit-erlang","afile_server.erl",</div><div>                                      "afile_server.beam","zoo.txt"]}}} (Timestamp: {1410,</div><div>                                                                                     108607,</div><div>                                                                                     887078})</div><div>{<0.44.0>,list_dir}</div><div>(<0.46.0>) out {afile_server,loop,1} (Timestamp: {1410,108607,887081})</div></div><div><div>[...]</div><div>7> exit(Pid, foo).</div><div>true</div><div>(<0.46.0>) exit foo (Timestamp: {1410,108643,568435})</div></div><div><br></div><div>The idiomatic Erlang way is to have someone else clean up after a failed process, but at least you have a couple of methods to debug your current situation.</div><div><br></div>-- <br>J.
</div></div>