[erlang-bugs] SSL socket not sending ssl_closed/ssl_error after a crash

Richard Jones rj@REDACTED
Fri Jun 17 23:59:25 CEST 2011


After making an SSL connection to a remote server, and setting the
socket {active, true}, I'm able to crash the gen_fsm that is part of
the ssl socket, without receiving ssl_closed, ssl_error or an EXIT
signal in the controlling process. This will cause the crash:
ssl:getopts(Sock, some_invalid_atom_here).

Presumably this should be impossible? I want to rely on receiving
ssl_closed or ssl_error so my controlling process can shut down when
the socket closes or crashes.

I've had a process leak due to this problem - ssl sockets closing, and
my code not noticing, leaving idle processes lingering. I'm not
triggering it in production using getopts/2 as below, but something
similar is happening somehow.

I've included some example code below, and a rather hefty error report
it generates.

Am I doing something daft, or is this a bug?

Regards,
RJ


Code, or with highlighting at http://ideone.com/T2ys0
=====


-module(ssltest).

-compile(export_all).

start() ->
    application:start(sasl),
    application:start(crypto),
    application:start(public_key),
    application:start(ssl),
    %% Just using github as a publicly available ssl port to for a demo:
    Opts = [list, {packet, line}, {active, false}],
    {ok, Sock} = ssl:connect("github.com", 443, Opts),
    Pid = spawn(fun client/0),
    ok = ssl:controlling_process(Sock, Pid),
    Pid ! {give, Sock},
    timer:sleep(1000),
    crash_weirdly(Sock),
    timer:sleep(1000),
    %% By now the client process should have received a ssl_closed/error msg
    %% but it doesnt get one, and is thus still running.
    case process_info(Pid) of
        undefined ->
            io:format("OK\n",[]),
            exit(normal);
        Props ->
            io:format("FAIL - no message received, process_info(~p):\n~p\n",
                      [Pid, Props]),
            exit(fail)
    end.

%% This appears to crash the gen_fsm of the ssl socket
%% but doesn't trigger a {ssl_closed, Sock} msg, or 'EXIT'
%% There are probably other ways it can crash like this too.
crash_weirdly(Sock) ->
    io:format("Crashing SSL socket\n", []),
    catch ssl:getopts(Sock, some_invalid_atom_here).


%% This process becomes the owner of the socket, and needs to terminate when
%% the socket is closed.
client() ->
    process_flag(trap_exit, true),
    %% Wait for controlling_process to be set:
    receive
        {give, Sock} ->
            ssl:setopts(Sock, [{active, true}]),
            io:format("Pid: ~p Sock: ~p made {active,true}\n", [self(),Sock]),
            recv_loop()
    end.


recv_loop() ->
    receive
        {ssl_closed, _} ->
            io:format("GOT SSL_CLOSED\n", []),
            ok;

        {ssl_error, _, Reason} ->
            io:format("GOT SSL_ERROR ~p\n", [Reason]),
            ok;

        X ->
            io:format("GOT: ~p\n", [X]),
            recv_loop()
    end.




=ERROR REPORT==== 17-Jun-2011::22:44:33 ===
** State machine <0.142.0> terminating
** Last message in was {'$gen_sync_all_state_event',
                           {<0.135.0>,#Ref<0.0.0.666>},
                           {get_opts,some_invalid_atom_here}}
** When State == connection
**      Data  == {state,client,
                     {#Ref<0.0.0.662>,<0.143.0>},
                     gen_tcp,tcp,tcp_closed,tcp_error,"github.com",443,
                     #Port<0.2884>,
                     {ssl_options,[],verify_none,#Fun<ssl.2.101768059>,false,
                         false,undefined,1,[],[],undefined,undefined,[],
                         undefined,
                         [<<0,57>>,
                          <<0,56>>,
                          <<0,53>>,
                          <<0,22>>,
                          <<0,19>>,
                          <<0,10>>,
                          <<0,51>>,
                          <<0,50>>,
                          <<0,47>>,
                          <<0,5>>,
                          <<0,4>>,
                          <<0,21>>,
                          <<0,9>>],
                         #Fun<ssl.0.92355367>,true,18446744073709551900,false,
                         []},
                     {socket_options,list,line,0,0,true},
                     {connection_states,
                         {connection_state,
                             {security_parameters,
                                 <<0,53>>,
                                 1,7,1,16,256,32,unknown,2,20,0,
                                 <<254,226,8,153,158,133,167,137,122,65,223,95,
                                   199,62,150,14,245,185,34,18,222,22,80,53,
                                   144,55,219,45,219,144,154,61,115,146,69,154,
                                   169,92,12,70,174,132,26,179,172,211,57,46>>,
                                 <<77,251,202,192,105,255,244,98,162,152,147,
                                   244,74,171,108,19,241,117,195,42,111,43,65,
                                   254,226,214,61,144,22,62,205,97>>,
                                 <<77,251,202,192,60,117,73,65,49,38,238,91,11,
                                   208,144,28,154,0,211,16,126,128,35,92,106,
                                   253,30,96,72,8,185,60>>,
                                 undefined},
                             undefined,
                             {cipher_state,
                                 <<107,193,209,70,14,51,191,243,6,30,235,102,
                                   177,66,108,91>>,
                                 <<82,122,121,50,67,146,95,105,141,89,204,41,
                                   100,220,66,0,155,39,193,179,77,82,117,3,82,
                                   50,239,196,223,45,153,192>>,
                                 undefined},
                             <<138,74,111,39,183,247,74,90,128,251,163,51,162,
                               134,187,95,239,150,95,60>>,
                             1,true,
                             <<199,217,7,37,129,205,108,88,226,52,211,51>>,
                             <<112,230,216,51,212,101,251,25,135,84,199,203>>},
                         {connection_state,
                             {security_parameters,undefined,1,undefined,
                                 undefined,undefined,undefined,undefined,
                                 undefined,undefined,undefined,undefined,
                                 undefined,
                                 <<77,251,202,192,89,182,0,44,93,225,217,162,
                                   14,42,169,222,17,209,142,212,247,18,33,182,
                                   63,47,102,161,116,187,144,170>>,
                                 undefined,undefined},
                             undefined,undefined,undefined,undefined,true,
                             undefined,undefined},
                         {connection_state,
                             {security_parameters,
                                 <<0,53>>,
                                 1,7,1,16,256,32,unknown,2,20,0,
                                 <<254,226,8,153,158,133,167,137,122,65,223,95,
                                   199,62,150,14,245,185,34,18,222,22,80,53,
                                   144,55,219,45,219,144,154,61,115,146,69,154,
                                   169,92,12,70,174,132,26,179,172,211,57,46>>,
                                 <<77,251,202,192,105,255,244,98,162,152,147,
                                   244,74,171,108,19,241,117,195,42,111,43,65,
                                   254,226,214,61,144,22,62,205,97>>,
                                 <<77,251,202,192,60,117,73,65,49,38,238,91,11,
                                   208,144,28,154,0,211,16,126,128,35,92,106,
                                   253,30,96,72,8,185,60>>,
                                 undefined},
                             undefined,
                             {cipher_state,
                                 <<67,15,24,23,53,236,18,90,72,41,97,211,175,
                                   56,148,242>>,
                                 <<88,144,90,102,28,113,30,48,224,50,164,48,94,
                                   43,35,54,0,203,91,66,113,135,169,4,52,9,154,
                                   82,90,15,250,71>>,
                                 undefined},
                             <<97,53,165,232,130,210,58,150,35,107,110,217,252,
                               26,77,201,16,112,28,231>>,
                             1,true,
                             <<199,217,7,37,129,205,108,88,226,52,211,51>>,
                             <<112,230,216,51,212,101,251,25,135,84,199,203>>},
                         {connection_state,
                             {security_parameters,undefined,1,undefined,
                                 undefined,undefined,undefined,undefined,
                                 undefined,undefined,undefined,undefined,
                                 undefined,
                                 <<77,251,202,192,28,184,76,68,123,76,60,142,
                                   106,240,173,4,49,192,225,191,249,104,4,239,
                                   222,69,131,35,191,245,19,1>>,
                                 undefined,undefined},
                             undefined,undefined,undefined,undefined,true,
                             undefined,undefined}},
                     [],<<>>,<<>>,
                     {{<<127,40,71,77,128,20,238,48,237,90,195,130,105,141,116,
                         224,32,128,0,0,0,0,0,0,135,84,199,203,0,0,0,0,0,0,0,0,
                         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,
                         0,0>>,
                       <<241,223,170,74,130,30,208,157,176,34,157,47,22,53,
                         101,223,229,36,29,122,32,128,0,0,0,0,0,0,135,84,199,
                         203,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                         0,0,0,0,0,0,0,0,0,0,4,0,0,0>>},
                      {<<28,22,93,1,181,148,104,2,201,120,127,203,130,141,153,
                         239,160,127,0,0,0,0,0,0,52,27,250,73,248,152,87,65,
                         188,19,124,110,164,44,199,195,178,69,135,100,166,29,
                         53,1,4,61,203,149,153,73,127,74,49,89,99,113,20,0,0,
                         12,199,217,7,37,129,205,108,88,226,52,211,51,0,0,0,0,
                         0,0,0,0,0,0,0,0,52,0,0,0>>,
                       <<28,51,142,246,15,126,227,240,185,11,6,135,7,54,173,
                         1,97,186,89,238,160,127,0,0,0,0,0,0,52,27,250,73,
                         248,152,87,65,188,19,124,110,164,44,199,195,178,69,
                         135,100,166,29,53,1,4,61,203,149,153,73,127,74,49,
                         89,99,113,20,0,0,12,199,217,7,37,129,205,108,88,226,
                         52,211,51,0,0,0,0,0,0,0,0,0,0,0,0,52,0,0,0>>}},
                     [],undefined,
                     {session,
                         <<93,85,39,160,113,79,61,119,168,42,21,212,128,173,57,
                           245,196,81,177,34,215,105,207,104,143,170,179,33,
                           205,107,59,108>>,
                         <<48,130,7,42,48,130,6,18,160,3,2,1,2,2,16,14,119,118,
                           138,93,7,240,229,121,89,202,42,157,80,130,181,48,13,
                           6,9,42,134,72,134,247,13,1,1,5,5,0,48,105,49,11,48,
                           9,6,3,85,4,6,19,2,85,83,49,21,48,19,6,3,85,4,10,19,
                           12,68,105,103,105,67,101,114,116,32,73,110,99,49,25,
                           48,23,6,3,85,4,11,19,16,119,119,119,46,100,105,103,
                           105,99,101,114,116,46,99,111,109,49,40,48,38,6,3,85,
                           4,3,19,31,68,105,103,105,67,101,114,116,32,72,105,
                           103,104,32,65,115,115,117,114,97,110,99,101,32,69,
                           86,32,67,65,45,49,48,30,23,13,49,49,48,53,50,55,48,
                           48,48,48,48,48,90,23,13,49,51,48,55,50,57,49,50,48,
                           48,48,48,90,48,129,202,49,29,48,27,6,3,85,4,15,12,
                           20,80,114,105,118,97,116,101,32,79,114,103,97,110,
                           105,122,97,116,105,111,110,49,19,48,17,6,11,43,6,1,
                           4,1,130,55,60,2,1,3,19,2,85,83,49,27,48,25,6,11,43,
                           6,1,4,1,130,55,60,2,1,2,19,10,67,97,108,105,102,111,
                           114,110,105,97,49,17,48,15,6,3,85,4,5,19,8,67,51,50,
                           54,56,49,48,50,49,11,48,9,6,3,85,4,6,19,2,85,83,49,
                           19,48,17,6,3,85,4,8,19,10,67,97,108,105,102,111,114,
                           110,105,97,49,22,48,20,6,3,85,4,7,19,13,83,97,110,
                           32,70,114,97,110,99,105,115,99,111,49,21,48,19,6,3,
                           85,4,10,19,12,71,105,116,72,117,98,44,32,73,110,99,
                           46,49,19,48,17,6,3,85,4,3,19,10,103,105,116,104,117,
                           98,46,99,111,109,48,130,1,34,48,13,6,9,42,134,72,
                           134,247,13,1,1,1,5,0,3,130,1,15,0,48,130,1,10,2,130,
                           1,1,0,237,211,137,195,93,112,114,9,243,51,79,26,114,
                           116,217,182,90,149,80,187,104,97,159,247,251,31,25,
                           225,218,4,49,175,21,124,26,127,249,115,175,29,229,
                           67,43,86,9,0,69,105,74,232,196,91,223,194,119,82,81,
                           25,91,209,43,217,57,101,54,160,50,25,28,65,115,251,
                           50,178,61,159,152,236,130,91,11,55,100,57,44,183,16,
                           131,114,205,240,234,36,75,250,217,148,46,195,133,21,
                           57,169,58,246,136,218,244,39,137,166,149,79,132,162,
                           55,78,124,37,120,58,201,131,109,2,23,149,120,125,71,
                           168,85,131,238,19,200,25,26,179,60,241,95,254,59,2,
                           225,133,251,17,102,171,9,93,159,76,67,240,199,36,94,
                           41,114,40,206,212,117,104,79,36,114,41,174,57,40,
                           252,223,141,79,77,131,115,116,12,111,17,155,167,221,
                           98,222,255,226,235,23,230,255,12,191,192,45,49,59,
                           214,89,162,242,221,135,74,72,123,109,51,17,20,77,52,
                           159,50,56,246,200,25,157,241,182,61,197,70,239,81,
                           11,138,198,51,237,72,97,196,29,23,27,189,124,182,
                           103,233,57,207,165,82,128,10,244,234,205,2,3,1,0,1,
                           163,130,3,106,48,130,3,102,48,31,6,3,85,29,35,4,24,
                           48,22,128,20,76,88,203,37,240,65,79,82,244,40,200,
                           129,67,155,166,168,160,230,146,229,48,29,6,3,85,29,
                           14,4,22,4,20,135,209,143,25,110,228,135,111,83,140,
                           119,145,7,80,223,163,191,85,71,32,48,37,6,3,85,29,
                           17,4,30,48,28,130,10,103,105,116,104,117,98,46,99,
                           111,109,130,14,119,119,119,46,103,105,116,104,117,
                           98,46,99,111,109,48,129,129,6,8,43,6,1,5,5,7,1,1,4,
                           117,48,115,48,36,6,8,43,6,1,5,5,7,48,1,134,24,104,
                           116,116,112,58,47,47,111,99,115,112,46,100,105,103,
                           105,99,101,114,116,46,99,111,109,48,75,6,8,43,6,1,5,
                           5,7,48,2,134,63,104,116,116,112,58,47,47,119,119,
                           119,46,100,105,103,105,99,101,114,116,46,99,111,109,
                           47,67,65,67,101,114,116,115,47,68,105,103,105,67,
                           101,114,116,72,105,103,104,65,115,115,117,114,97,
                           110,99,101,69,86,67,65,45,49,46,99,114,116,48,12,6,
                           3,85,29,19,1,1,255,4,2,48,0,48,97,6,3,85,29,31,4,90,
                           48,88,48,42,160,40,160,38,134,36,104,116,116,112,58,
                           47,47,99,114,108,51,46,100,105,103,105,99,101,114,
                           116,46,99,111,109,47,101,118,50,48,48,57,97,46,99,
                           114,108,48,42,160,40,160,38,134,36,104,116,116,112,
                           58,47,47,99,114,108,52,46,100,105,103,105,99,101,
                           114,116,46,99,111,109,47,101,118,50,48,48,57,97,46,
                           99,114,108,48,130,1,196,6,3,85,29,32,4,130,1,187,48,
                           130,1,183,48,130,1,179,6,9,96,134,72,1,134,253,108,
                           2,1,48,130,1,164,48,58,6,8,43,6,1,5,5,7,2,1,22,46,
                           104,116,116,112,58,47,47,119,119,119,46,100,105,103,
                           105,99,101,114,116,46,99,111,109,47,115,115,108,45,
                           99,112,115,45,114,101,112,111,115,105,116,111,114,
                           121,46,104,116,109,48,130,1,100,6,8,43,6,1,5,5,7,2,
                           2,48,130,1,86,30,130,1,82,0,65,0,110,0,121,0,32,0,
                           117,0,115,0,101,0,32,0,111,0,102,0,32,0,116,0,104,0,
                           105,0,115,0,32,0,67,0,101,0,114,0,116,0,105,0,102,0,
                           105,0,99,0,97,0,116,0,101,0,32,0,99,0,111,0,110,0,
                           115,0,116,0,105,0,116,0,117,0,116,0,101,0,115,0,32,
                           0,97,0,99,0,99,0,101,0,112,0,116,0,97,0,110,0,99,0,
                           101,0,32,0,111,0,102,0,32,0,116,0,104,0,101,0,32,0,
                           68,0,105,0,103,0,105,0,67,0,101,0,114,0,116,0,32,0,
                           67,0,80,0,47,0,67,0,80,0,83,0,32,0,97,0,110,0,100,0,
                           32,0,116,0,104,0,101,0,32,0,82,0,101,0,108,0,121,0,
                           105,0,110,0,103,0,32,0,80,0,97,0,114,0,116,0,121,0,
                           32,0,65,0,103,0,114,0,101,0,101,0,109,0,101,0,110,0,
                           116,0,32,0,119,0,104,0,105,0,99,0,104,0,32,0,108,0,
                           105,0,109,0,105,0,116,0,32,0,108,0,105,0,97,0,98,0,
                           105,0,108,0,105,0,116,0,121,0,32,0,97,0,110,0,100,0,
                           32,0,97,0,114,0,101,0,32,0,105,0,110,0,99,0,111,0,
                           114,0,112,0,111,0,114,0,97,0,116,0,101,0,100,0,32,0,
                           104,0,101,0,114,0,101,0,105,0,110,0,32,0,98,0,121,0,
                           32,0,114,0,101,0,102,0,101,0,114,0,101,0,110,0,99,0,
                           101,0,46,48,29,6,3,85,29,37,4,22,48,20,6,8,43,6,1,5,
                           5,7,3,1,6,8,43,6,1,5,5,7,3,2,48,17,6,9,96,134,72,1,
                           134,248,66,1,1,4,4,3,2,6,192,48,14,6,3,85,29,15,1,1,
                           255,4,4,3,2,5,160,48,13,6,9,42,134,72,134,247,13,1,
                           1,5,5,0,3,130,1,1,0,20,82,113,31,134,157,109,53,62,
                           134,187,102,26,139,133,152,185,0,76,203,66,181,70,
                           252,6,231,68,57,200,232,82,216,17,20,35,179,114,150,
                           233,20,148,158,47,0,40,247,213,4,69,64,0,198,244,87,
                           66,66,222,9,137,151,17,13,20,92,107,189,11,247,24,
                           163,95,103,2,243,9,56,99,191,193,18,157,48,186,142,
                           165,84,116,89,83,103,161,27,80,91,38,218,253,19,126,
                           89,23,191,73,239,148,126,69,164,253,58,73,50,240,
                           106,255,137,141,169,97,169,170,155,150,70,200,28,
                           224,24,28,230,251,130,244,10,171,82,166,202,232,84,
                           34,217,219,42,61,90,34,123,128,234,7,5,212,249,199,
                           240,83,89,95,187,119,126,222,147,112,65,78,35,203,
                           120,121,121,196,46,234,215,102,42,24,247,209,197,
                           124,226,18,120,130,141,29,236,130,158,1,162,229,2,
                           190,120,161,185,89,88,197,76,111,79,165,49,180,73,
                           91,94,152,30,46,56,246,25,196,57,162,74,251,121,5,
                           184,242,89,229,38,18,112,173,192,232,117,35,31,24,
                           209,11,224,159,101,228,195,215,73,135,91,114,108,
                           177,47,172,111>>,
                         0,
                         <<0,53>>,
                         <<254,226,8,153,158,133,167,137,122,65,223,95,199,62,
                           150,14,245,185,34,18,222,22,80,53,144,55,219,45,219,
                           144,154,61,115,146,69,154,169,92,12,70,174,132,26,
                           179,172,211,57,46>>,
                         true,undefined},
                     20499,ssl_session_cache,
                     {3,1},
                     undefined,false,rsa,
                     {{1,2,840,113549,1,1,1},
                      {'RSAPublicKey',

30022791620261825757993662585953092115593876007790889075857977169380755026843488295590920332822959949671733783772267273663127572234689557556838440961311958034872446138607383478249154120700224788846090518954426547213891114527577249466052578760409101386764739954597264062457764350325580813575208479828785125746659984354118047108089208903185979168757084287242652658664280506839310200006418365390463140656501936394629471169620807647477079812243508155549460290614662952471527791986452768187753921075252815690139573016552982548499689965311352938938970566167612062244558031493494815274290207588576741886915392581117360270029,
                          65537},
                      'NULL'},
                     undefined,undefined,undefined,
                     <<3,1,206,145,41,17,221,72,203,148,200,153,94,83,137,250,
                       208,132,136,231,216,156,242,214,230,137,138,173,184,97,
                       148,200,195,6,175,5,154,136,35,172,78,163,255,27,208,67,
                       35,188>>,
                     #Ref<0.0.0.656>,
                     {<0.135.0>,#Ref<0.0.0.659>},
                     0,<<>>,true,undefined,false,
                     {[],[]}}
** Reason for termination =
** {function_clause,[{ssl_connection,get_socket_opts,
                                     [#Port<0.2884>,some_invalid_atom_here,
                                      {socket_options,list,line,0,0,true},
                                      []]},
                     {ssl_connection,handle_sync_event,4},
                     {gen_fsm,handle_msg,7},
                     {proc_lib,init_p_do_apply,3}]}

=CRASH REPORT==== 17-Jun-2011::22:44:33 ===
  crasher:
    initial call: ssl_connection:init/1
    pid: <0.142.0>
    registered_name: []
    exception exit: {function_clause,
                        [{ssl_connection,get_socket_opts,
                             [#Port<0.2884>,some_invalid_atom_here,
                              {socket_options,list,line,0,0,true},
                              []]},
                         {ssl_connection,handle_sync_event,4},
                         {gen_fsm,handle_msg,7},
                         {proc_lib,init_p_do_apply,3}]}
      in function  gen_fsm:terminate/7
    ancestors: [ssl_connection_sup,ssl_sup,<0.59.0>]
    messages: []
    links: [<0.63.0>]
    dictionary: []
    trap_exit: false
    status: running
    heap_size: 1597
    stack_size: 24
    reductions: 9050
  neighbours:

=SUPERVISOR REPORT==== 17-Jun-2011::22:44:33 ===
     Supervisor: {local,ssl_connection_sup}
     Context:    child_terminated
     Reason:     {function_clause,
                     [{ssl_connection,get_socket_opts,
                          [#Port<0.2884>,some_invalid_atom_here,
                           {socket_options,list,line,0,0,true},
                           []]},
                      {ssl_connection,handle_sync_event,4},
                      {gen_fsm,handle_msg,7},
                      {proc_lib,init_p_do_apply,3}]}
     Offender:   [{pid,<0.142.0>},
                  {name,undefined},
                  {mfargs,
                      {ssl_connection,start_link,
                          [client,"github.com",443,#Port<0.2884>,
                           {{ssl_options,[],verify_none,
                                #Fun<ssl.2.101768059>,false,false,undefined,1,
                                [],[],undefined,[],[],undefined,
                                [<<0,57>>,
                                 <<0,56>>,
                                 <<0,53>>,
                                 <<0,22>>,
                                 <<0,19>>,
                                 <<0,10>>,
                                 <<0,51>>,
                                 <<0,50>>,
                                 <<0,47>>,
                                 <<0,5>>,
                                 <<0,4>>,
                                 <<0,21>>,
                                 <<0,9>>],
                                #Fun<ssl.0.92355367>,true,
                                18446744073709551900,false,[]},
                            {socket_options,list,line,0,0,false}},
                           <0.135.0>,
                           {gen_tcp,tcp,tcp_closed,tcp_error}]}},
                  {restart_type,temporary},
                  {shutdown,4000},
                  {child_type,worker}]



More information about the erlang-bugs mailing list