[erlang-questions] Abandoned (ranch) connection processes?

Loïc Hoguin essen@REDACTED
Thu Feb 15 13:14:40 CET 2018


On 02/15/2018 12:56 PM, Fred Hebert wrote:
> On 02/15, Roger Lipscombe wrote:
>> OK. Let me rephrase that:
>>
>> - {active, once} obviously has something in place to handle data
>> arriving and closed sockets *in between* calls to {active, once} --
>> i.e. it'll be {active, false} for a brief interval. I last looked at
>> this code in 17.x (before the gen_statem refactoring), so I'm not sure
>> where it lives now.
>> - does it deal correctly with closed sockets that close before the
>> *first* call to {active, once}? In other words: can I expect an
>> ssl_closed message in this case? Is there something special about the
>> first call?
>>
> 
> Nothing like a quick experiment to show:
> 
>     5> {ok, S} = ssl:connect("google.com", 443, [{active, false}]).
>     {ok,{sslsocket,{gen_tcp,#Port<0.953>,tls_connection,
>                             undefined},
>                    <0.91.0>}}
>     6> ssl:close(S).
>     ok
>     7> ssl:setopts(S, [{active,once}]).
>     {error,closed}
>     8> flush().
>     ok
> 
> It looks like you do have to check the result of setopts to find that 
> the connections has failed early.

This isn't the same. For example if I do this with gen_tcp I get an 
{error,einval} indicating my code is wrong.

12> {ok, S} = gen_tcp:connect("google.com", 80, [{active, false}]).
{ok,#Port<0.474>}
13> gen_tcp:close(S).
ok
14> inet:setopts(S, [{active, once}]).
{error,einval}
15> flush().
ok

If instead of closing myself I wait for the remote endpoint to close the 
connection, I will not get an error from setopts:

17> {ok, S} = gen_tcp:connect("google.com", 80, [{active, false}]).
{ok,#Port<0.475>}
18> inet:setopts(S, [{active, once}]).
ok
19> flush().
ok
20> gen_tcp:send(S, "Hello\r\n").
{error,closed}

This contrasts with the behavior from ssl which does tell you the socket 
is closed earlier, but doesn't seem to be something consistent across 
both transports.

So if your code can use both transports and are not sending any data 
upon opening the connection you will need to have a timeout of some kind 
to detect that the socket got closed.

Cheers,

-- 
Loïc Hoguin
https://ninenines.eu



More information about the erlang-questions mailing list