Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Attempt to fix port leak, also fixed clean up of sockets. #1

Merged
merged 1 commit into from about 2 years ago

2 participants

Yoshihiro TANAKA Fred Hebert
Yoshihiro TANAKA

There is a possibility of port leak when processes are killed using exit/2 BIF when they are connecting/closing sockets, executing in prim_inet module.
Also fixed the format of remaining free sockets in terminate/2.

Yoshihiro Tanaka Attempt to fix port leak, also fixed clean up of sockets.
There is a possibility of port leak when processes are killed using exit/2 BIF when they are connecting/closing sockets, executing in prim_inet module.
Also fixed the format of remaining free sockets in terminate/2.
70fd653
Fred Hebert
Owner
ferd commented

Ah, so if I get it right, the fix I originally sent by mail fixed only half the problem -- there's still a delay before the socket is set up right?

I'm a bit busy (participating in Spawnfest), so I'll try to manually merge it (can't be done automatically) first thing Monday morning, and then create a new tag for everyone to have a stable version.

Yoshihiro TANAKA

Yes, some of then are slow. Thank you for the comment, good luck on Spawnfest !!

Fred Hebert ferd merged commit 70fd653 into from
Fred Hebert ferd closed this
Fred Hebert
Owner
ferd commented

Hi, the change is finally merged in, back into master. I've also made a new tag with 1.2.8 containing that specific fix if you want everything to be stable.

Thanks again for the helpful reports, finding the bug, and completing the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jul 07, 2012
Yoshihiro Tanaka Attempt to fix port leak, also fixed clean up of sockets.
There is a possibility of port leak when processes are killed using exit/2 BIF when they are connecting/closing sockets, executing in prim_inet module.
Also fixed the format of remaining free sockets in terminate/2.
70fd653
This page is out of date. Refresh to see the latest.

Showing 2 changed files with 37 additions and 5 deletions. Show diff stats Hide diff stats

  1. +1 1  src/lhttpc_lb.erl
  2. +36 4 src/lhttpc_sock.erl
2  src/lhttpc_lb.erl
@@ -156,7 +156,7 @@ code_change(_OldVsn, State, _Extra) ->
156 156 terminate(_Reason, #state{host=H, port=P, ssl=Ssl, free=Free, clients=Tid}) ->
157 157 ets:delete(Tid),
158 158 ets:delete(?MODULE,{H,P,Ssl}),
159   - [lhttpc_sock:close(Socket,Ssl) || Socket <- Free],
  159 + [lhttpc_sock:close(Socket,Ssl) || {Socket, _TimerRef} <- Free],
160 160 ok.
161 161
162 162 %%%%%%%%%%%%%%%
40 src/lhttpc_sock.erl
@@ -59,9 +59,25 @@
59 59 -spec connect(host(), integer(), socket_options(), timeout(), boolean()) ->
60 60 {ok, socket()} | {error, atom()}.
61 61 connect(Host, Port, Options, Timeout, true) ->
62   - ssl:connect(Host, Port, Options, Timeout);
  62 + % Avoid port leak with potential race condition in case of timeout
  63 + Flag = process_flag(trap_exit, true),
  64 + Res = ssl:connect(Host, Port, Options, Timeout),
  65 + receive
  66 + {'EXIT',_Pid,timeout} -> exit(timeout)
  67 + after 0 ->
  68 + process_flag(trap_exit, Flag),
  69 + Res
  70 + end;
63 71 connect(Host, Port, Options, Timeout, false) ->
64   - gen_tcp:connect(Host, Port, Options, Timeout).
  72 + % Avoid port leak with potential race condition in case of timeout
  73 + Flag = process_flag(trap_exit, true),
  74 + Res = gen_tcp:connect(Host, Port, Options, Timeout),
  75 + receive
  76 + {'EXIT',_Pid,timeout} -> exit(timeout)
  77 + after 0 ->
  78 + process_flag(trap_exit, Flag),
  79 + Res
  80 + end.
65 81
66 82 %% @spec (Socket, SslFlag) -> {ok, Data} | {error, Reason}
67 83 %% Socket = socket()
@@ -154,6 +170,22 @@ setopts(Socket, Options, false) ->
154 170 %% @end
155 171 -spec close(socket(), boolean()) -> ok | {error, atom()}.
156 172 close(Socket, true) ->
157   - ssl:close(Socket);
  173 + % Avoid port leak with potential race condition in case of timeout
  174 + Flag = process_flag(trap_exit, true),
  175 + Res = ssl:close(Socket),
  176 + receive
  177 + {'EXIT',_Pid,timeout} -> exit(timeout)
  178 + after 0 ->
  179 + process_flag(trap_exit, Flag),
  180 + Res
  181 + end;
158 182 close(Socket, false) ->
159   - gen_tcp:close(Socket).
  183 + % Avoid port leak with potential race condition in case of timeout
  184 + Flag = process_flag(trap_exit, true),
  185 + Res = gen_tcp:close(Socket),
  186 + receive
  187 + {'EXIT',_Pid,timeout} -> exit(timeout)
  188 + after 0 ->
  189 + process_flag(trap_exit, Flag),
  190 + Res
  191 + end.

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.