#1 2018-04-03 14:30:18

Quorren
Member
Registered: 2016-10-03
Posts: 14

AsynchServer infinite loop and high(100%) cpu load.

Hi everyone. I have noted that my server have high cpu load after a while.
After small testing i found that simple scanning may be cause of such behaviour.

when i start my server  like this

Server:=TAsynchServer.Create('333',nil,nil,TMySocket,'test',TSQLLog, [] ,5);

and scanning like this

nmap -sT 192.168.4.62 -p 333

i have 100% cpu load immideatly.

Help me anyone)

Offline

#2 2018-04-04 08:56:13

Quorren
Member
Registered: 2016-10-03
Posts: 14

Re: AsynchServer infinite loop and high(100%) cpu load.

Maybe i have found a solution.
procedure

procedure TPollAsynchSockets.ProcessRead(timeoutMS: integer);

has cycle skip in case of socket read error

            res := AsynchRecv(slot.socket,@temp,sizeof(temp));
            if res<0 then       // error - probably "may block"
                 break;

i think we must close connection depending on errorcode?

            res := AsynchRecv(slot.socket,@temp,sizeof(temp));
            if res<0 then       // error - probably "may block"
              begin
              err:=WSAGetLastError();
                  case err of
                    WSAECONNRESET:CloseConnection;
                  end;
              break;
              end;

Offline

#3 2018-04-12 10:26:55

franfj
Member
Registered: 2018-03-28
Posts: 12

Re: AsynchServer infinite loop and high(100%) cpu load.

Same issue here.

Also I've found that with the current implementation is impossible to know the disconnection reason of a client. I've tried with "WSAGetLastError" on my connection "TAsynchConnection.BeforeDestroy" and always returns '0' because the ProcessRead function CloseConnection is only performed on a gracefully connection close.

But with the solution proposed this will be possible, however I'd change the code to the following:

            {
            if res<0 then       // error - probably "may block"
              break;
            if res=0  then begin // socket closed -> abort
              CloseConnection;
              exit;
            end;
            }
            if (res<0) and (WSAGetLastError() = WSAEWOULDBLOCK)  then  // error - probably "may block"
              break;
            if res<=0  then begin // socket closed or error -> abort
              CloseConnection;
              exit;
            end;

As the documentation suggest that only WSAEWOULDBLOCK is the only error that can be resolved trying again later...

Last edited by franfj (2018-04-12 11:57:18)

Offline

Board footer

Powered by FluxBB