Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Attempt at fixing a possible process leak

Yoshihiro Tanaka found that when lhttpc:request closes a worker
due to a timeout, it's possible that it happens after the port
is unlinked in prim_inet:close, but before it is properly closed.

This results in orphaned sockets/ports being left hanging in the
ether.

This fix attempts to wrap lhttpc_sock:close commands around a safe
build that should resolve it.

A potential fix would have been to have the manager monitor the
sockets itself, but this wouldn't have worked if the socket is new
and the manager has never seen it before, hence the current fix.
  • Loading branch information...
commit 3cf37fe21c7b9247c6c5f2c5a93f5480b2ad4f7c 1 parent 5bfddba
Fred Hebert ferd authored

Showing 2 changed files with 25 additions and 3 deletions. Show diff stats Hide diff stats

  1. +1 1  src/lhttpc.app.src
  2. +24 2 src/lhttpc_sock.erl
2  src/lhttpc.app.src
@@ -29,7 +29,7 @@
29 29 %%% @end
30 30 {application, lhttpc,
31 31 [{description, "Lightweight HTTP Client"},
32   - {vsn, "1.2.7"},
  32 + {vsn, "1.2.8"},
33 33 {modules, [lhttpc,lhttpc_sup,lhttpc_client,lhttpc_sock,lhttp_lb]},
34 34 {registered, [lhttpc_sup]},
35 35 {applications, [kernel, stdlib, ssl, crypto]},
26 src/lhttpc_sock.erl
@@ -154,6 +154,28 @@ setopts(Socket, Options, false) ->
154 154 %% @end
155 155 -spec close(socket(), boolean()) -> ok | {error, atom()}.
156 156 close(Socket, true) ->
157   - ssl:close(Socket);
  157 + %% Safer exiting. Yoshihiro Tanaka from OpenX figured out
  158 + %% that we had potential race conditions while closing the
  159 + %% socket and timing out. This process flagging aims to
  160 + %% wrap some safety around this in case of a timeout
  161 + Flag = process_flag(trap_exit, true),
  162 + Res = ssl:close(Socket),
  163 + receive
  164 + {'EXIT',_Pid,timeout} -> exit(timeout)
  165 + after 0 ->
  166 + process_flag(trap_exit, Flag),
  167 + Res
  168 + end;
158 169 close(Socket, false) ->
159   - gen_tcp:close(Socket).
  170 + %% Safer exiting. Yoshihiro Tanaka from OpenX figured out
  171 + %% that we had potential race conditions while closing the
  172 + %% socket and timing out. This process flagging aims to
  173 + %% wrap some safety around this in case of a timeout
  174 + Flag = process_flag(trap_exit, true),
  175 + Res = gen_tcp:close(Socket),
  176 + receive
  177 + {'EXIT',_Pid,timeout} -> exit(timeout)
  178 + after 0 ->
  179 + process_flag(trap_exit, Flag),
  180 + Res
  181 + end.

0 comments on commit 3cf37fe

Please sign in to comment.
Something went wrong with that request. Please try again.