[erlang-bugs] Problems with SSL ssl_esock.exe (R12B-5 and R12B-4)

Денис Артёмов art_den@REDACTED
Thu Nov 20 13:06:31 CET 2008


I started to test erlang SSL library and find some unpleasant stuff: sometimes ssl_esock.exe starts to use 100% of CPU time on MS Windows. After some searching I found same problems in russian ejabberd forum. Researched a little, I found out that 100% load of CPU arises after uncorrect closing of SSL connection (when only socket connection is closed, not a SSL one). If to close SSL connection carefully, there is no any problem then.

*** How to reproduce the problem ***

The erlang program have to work as SSL server. The problem arise after to connect to server and to disconnect (uncorrectly).

Erlang listing of server.erl:
==================================================================

-module(server).

-export([start/0]).

start() ->
    application:start(ssl),

    ssl:seed("jhdnfkjernfeijkrnferkjn"),

     Options = 
    [{active, true},
     {verify, 2},
     {mode, binary},
     {depth, 2},
     {cacertfile, "SnmpManagerProServ.pem"}, % My ssl keys and sertificates
     {certfile,   "SnmpManagerProServ.pem"}, 
     {keyfile,    "SnmpManagerProServ-key.pem"},
     {password,   "1234567890"}
     ],

    {Res, LSock} = ssl:listen(8888, Options),

    io:fwrite("Res=~p~n", [Res]),

    accept(LSock)
.

accept(LSock) ->
    {ok, ASock} = ssl:transport_accept(LSock),
    io:fwrite("ssl:transport_accept, ASock=~p~n", [ASock]),

    AccRes = ssl:ssl_accept(ASock),
    io:fwrite("ssl:ssl_accept, AccRes=~p~n", [AccRes]),

    spawn(fun() -> accept(LSock) end),

    read(ASock).

read(Sock) ->
    receive 
        {ssl_closed, _} ->
            ssl:close(Sock);
        Data -> 
            io:fwrite("Received=~p ~n", [Data]),
            read(Sock)
    end.

==================================================================

To start server, just execute server:start()

To connect to erlang SSL server, I used buildin OpenSSL s_client. To close SSL connection uncorrect, it is enough to connect and then press "Ctrl+C" in OpenSSL s_client window. Immediately ssl_esock.exe start to "eat" of 100% CPU.


*** Simple solution ***

I recompiled ssl_esock.exe with debug data, run it from debugger and saw that 100% cpu load happens in esock.c file. 

esock_poll_fd_isset_read(&pollfd, cp->fd) is alwais signaled that it is possible to read:

    else if (esock_poll_fd_isset_read(&pollfd, cp->fd)) {
        /* Read from SSL and write to proxy */
        DEBUGF(("-----------------------------------\n"));
        DEBUGF(("JOINED: read from ssl fd = %d\n",
            cp->fd));
        cc = esock_ssl_read(cp, rwbuf, RWBUFLEN);
        DEBUGF(("read from fd = %d, cc = %d\n", cp->fd, cc));

but esock_ssl_read reads nothing and returns -1.

I insert simple condition to avoid it:

    else if (esock_poll_fd_isset_read(&pollfd, cp->fd)) {
        /* Read from SSL and write to proxy */
        DEBUGF(("-----------------------------------\n"));
        DEBUGF(("JOINED: read from ssl fd = %d\n",
            cp->fd));
        cc = esock_ssl_read(cp, rwbuf, RWBUFLEN);
        DEBUGF(("read from fd = %d, cc = %d\n", cp->fd, cc));
        /* My solution */
        if (cc < 0) 
        {
            close_and_remove_connection(cp);
        }

and it start to work fine for me (I know of cource this is treatment of symptoms, not of major problem)

Hope developers will fix this bug soon.

PS: Sorry for my bad English :)
PPS: Win XP RUS SP2 or Win XP RUS SP3, OpenSSL 0.9.8i taken from http://www.slproweb.com/products/Win32OpenSSL.html , Erlang for MS Widnows R12B-5 or R12B-4




More information about the erlang-bugs mailing list