<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    I've had the same problem in my lhttpc fork, and after some
    concerted work with a few users of the fork at OpenX, we made the
    following fix in the lhttpc wrapper
    (<a class="moz-txt-link-freetext" href="https://github.com/ferd/lhttpc/blob/master/src/lhttpc_sock.erl#L172">https://github.com/ferd/lhttpc/blob/master/src/lhttpc_sock.erl#L172</a>):<br>
    <meta http-equiv="content-type" content="text/html;
      charset=ISO-8859-1">
    <pre><div class="line" id="LC174">    <span class="nv">Flag</span> <span class="o">=</span> <span class="nb">process_flag</span><span class="p">(</span><span class="n">trap_exit</span><span class="p">,</span> <span class="n">true</span><span class="p">),</span></div><div class="line" id="LC175">    <span class="nv">Res</span> <span class="o">=</span> gen_tcp<span class="p">:</span><span class="n">close</span><span class="p">(</span><span class="nv">Socket</span><span class="p">),</span></div><div class="line" id="LC176">    <span class="k">receive</span></div><div class="line" id="LC177">        <span class="p">{</span><span class="n">'EXIT'</span><span class="p">,_</span><span class="nv">Pid</span><span class="p">,</span><span class="n">timeout</span><span class="p">}</span> <span class="o">-></span> <span class="nb">exit</span><span class="p">(</span><span class="n">timeout</sp
an><span class="p">)</span></div><div class="line" id="LC178">    <span class="k">after</span> <span class="mi">0</span> <span class="o">-></span></div><div class="line" id="LC179">        <span class="nb">process_flag</span><span class="p">(</span><span class="n">trap_exit</span><span class="p">,</span> <span class="nv">Flag</span><span class="p">),</span></div><div class="line" id="LC180">        <span class="nv">Res</span></div><div class="line" id="LC181">    <span class="k">end</span><span class="p"></span></div></pre>
    We're able to do it because we expect, 99.999% of the time that the
    race condition will happen with a specific exit message we chose
    ourselves in that given context (we control the process where this
    happens 100%). I can't think of a universally good way to fix it in
    Erlang itself, where you'd risk eating up messages you're not
    supposed to handle, though.<br>
    <br>
    <div class="moz-cite-prefix">On 12-10-11 5:06 PM, Dmitry Belyaev
      wrote:<br>
    </div>
    <blockquote
      cite="mid:CB428AF6-F13B-433F-B5AF-FBD41F8E5C1C@gmail.com"
      type="cite">
      <div>Some days ago we found that we have thousands of leaked
        sockets in our project.</div>
      <div><br>
      </div>
      <div>These sockets were ports with state like this:</div>
      <div>
        <div>[{name,"tcp_inet"},</div>
        <div> {links,[]},</div>
        <div> {connected,<0.54.0>},...]</div>
      </div>
      <div><br>
      </div>
      <div>We made investigation and found the cause of the leaks.</div>
      <div><br>
      </div>
      <div>We have inets option {exit_on_close, false} to read
        statistics from the socket after it was closed by the peer.
        Process that controls the socket does not trap_exit and is
        linked with some another process.</div>
      <div>At the end of connection controller calls gen_tcp:close/1 and
        sometimes the linked process dies at that the same moment. We
        found out that the gen_tcp:close/1 calls prim_inet:close/1, the
        first action of which is unlink from controlling process. So,
        when controller is unlinked from the port and is killed by the
        signal, port stays in the system because of exit_on_close
        feature.</div>
      <div><br>
      </div>
      <div>I've made a module that sometimes may reveal the problem. <a
          moz-do-not-send="true" href="https://gist.github.com/3875485">https://gist.github.com/3875485</a></div>
      <div>On my system I half of dozen calls to close_bug:start(1000)
        does find such leaked ports. </div>
      <div>I haven't found the right solution for the problem yet, so no
        patches at the moment.</div>
      <div><br>
      </div>
      <div>Thank you for your attention.</div>
      <div><br>
        <div>
          <div style="word-wrap: break-word; -webkit-nbsp-mode: space;
            -webkit-line-break: after-white-space; ">
            <div>-- </div>
            <div>Dmitry Belyaev</div>
          </div>
        </div>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
erlang-bugs mailing list
<a class="moz-txt-link-abbreviated" href="mailto:erlang-bugs@erlang.org">erlang-bugs@erlang.org</a>
<a class="moz-txt-link-freetext" href="http://erlang.org/mailman/listinfo/erlang-bugs">http://erlang.org/mailman/listinfo/erlang-bugs</a>
</pre>
    </blockquote>
    <br>
  </body>
</html>