Skip to content
Browse files

Merge branch 'feature/port_leak_fix2' of github.com:hirotnk/lhttpc in…

…to socket-leak

Conflicts:
	src/lhttpc_sock.erl
  • Loading branch information...
2 parents 3cf37fe + 70fd653 commit fbf9a46e0180a57f75f53bdec37be618ba5979e7 @ferd committed Jul 9, 2012
Showing with 21 additions and 11 deletions.
  1. +1 −1 src/lhttpc_lb.erl
  2. +20 −10 src/lhttpc_sock.erl
View
2 src/lhttpc_lb.erl
@@ -156,7 +156,7 @@ code_change(_OldVsn, State, _Extra) ->
terminate(_Reason, #state{host=H, port=P, ssl=Ssl, free=Free, clients=Tid}) ->
ets:delete(Tid),
ets:delete(?MODULE,{H,P,Ssl}),
- [lhttpc_sock:close(Socket,Ssl) || Socket <- Free],
+ [lhttpc_sock:close(Socket,Ssl) || {Socket, _TimerRef} <- Free],
ok.
%%%%%%%%%%%%%%%
View
30 src/lhttpc_sock.erl
@@ -59,9 +59,25 @@
-spec connect(host(), integer(), socket_options(), timeout(), boolean()) ->
{ok, socket()} | {error, atom()}.
connect(Host, Port, Options, Timeout, true) ->
- ssl:connect(Host, Port, Options, Timeout);
+ % Avoid port leak with potential race condition in case of timeout
+ Flag = process_flag(trap_exit, true),
+ Res = ssl:connect(Host, Port, Options, Timeout),
+ receive
+ {'EXIT',_Pid,timeout} -> exit(timeout)
+ after 0 ->
+ process_flag(trap_exit, Flag),
+ Res
+ end;
connect(Host, Port, Options, Timeout, false) ->
- gen_tcp:connect(Host, Port, Options, Timeout).
+ % Avoid port leak with potential race condition in case of timeout
+ Flag = process_flag(trap_exit, true),
+ Res = gen_tcp:connect(Host, Port, Options, Timeout),
+ receive
+ {'EXIT',_Pid,timeout} -> exit(timeout)
+ after 0 ->
+ process_flag(trap_exit, Flag),
+ Res
+ end.
%% @spec (Socket, SslFlag) -> {ok, Data} | {error, Reason}
%% Socket = socket()
@@ -154,10 +170,7 @@ setopts(Socket, Options, false) ->
%% @end
-spec close(socket(), boolean()) -> ok | {error, atom()}.
close(Socket, true) ->
- %% Safer exiting. Yoshihiro Tanaka from OpenX figured out
- %% that we had potential race conditions while closing the
- %% socket and timing out. This process flagging aims to
- %% wrap some safety around this in case of a timeout
+ % Avoid port leak with potential race condition in case of timeout
Flag = process_flag(trap_exit, true),
Res = ssl:close(Socket),
receive
@@ -167,10 +180,7 @@ close(Socket, true) ->
Res
end;
close(Socket, false) ->
- %% Safer exiting. Yoshihiro Tanaka from OpenX figured out
- %% that we had potential race conditions while closing the
- %% socket and timing out. This process flagging aims to
- %% wrap some safety around this in case of a timeout
+ % Avoid port leak with potential race condition in case of timeout
Flag = process_flag(trap_exit, true),
Res = gen_tcp:close(Socket),
receive

0 comments on commit fbf9a46

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