[Libwebsockets] lws_close_reason with "Max connection reached" message

Andy Green andy at warmcat.com
Wed Jul 6 13:23:44 CEST 2016



On July 6, 2016 7:07:05 PM GMT+08:00, Thomas Spitz <thomas.spitz at hestia-france.com> wrote:
>Hi Andy,
>
>Thanks again for the very quick reply.
>
>It works like a charm
>
>For my info (and for further debugging):
>When I close the connection from the server side using "return -1" on
>"LWS_CALLBACK_ESTABLISHED"; using netstat -tn I see a TIME_WAIT for the
>given client that stays almost 1 minute before disappearing. With
>previous
>master, I saw a TIMEDOUT in log messages after around 20s (with log
>level
>set at 7). Now, I get no TIMEDOUT anymore but using Level 31 I can see
>a
>timeout set of 20 secs:
>
>lwsts[10481]: closing connection at lws_read bail:
>> lwsts[10481]: sending close indication...
>>
>> *lwsts[10481]: lws_set_timeout: 0xd2cf8: 1 secs*lwsts[10481]: sent
>close
>> indication, awaiting ack
>> lwsts[10481]: fd=16, revents=1
>> lwsts[10481]: lws_service_fd_tsi: 0xd2cf8: inherited ah rx
>> lwsts[10481]: lws_read: incoming len 217  state 9
>> lwsts[10481]: illegal opcode
>> lwsts[10481]: interpret_incoming_packet has bailed
>> lwsts[10481]: closing connection at lws_read bail:
>> lwsts[10481]: lws_close_free_wsi: shutting down connection: 0xd2cf8
>(sock
>> 16, state 9)
>>
>> *lwsts[10481]: lws_set_timeout: 0xd2cf8: 20 secs*lwsts[10481]: fd=16,
>> revents=17
>> lwsts[10481]: lws_service_fd_tsi: 0xd2cf8: inherited ah rx
>> lwsts[10481]: lws_read: incoming len 217  state 11
>> lwsts[10481]: illegal opcode
>> lwsts[10481]: interpret_incoming_packet has bailed
>> lwsts[10481]: closing connection at lws_read bail:
>> lwsts[10481]: lws_close_free_wsi: real just_kill_connection: 0xd2cf8
>> (sockfd 16)
>> lwsts[10481]: remove_wsi_socket_from_fds: removing same prot wsi
>0xd2cf8
>> lwsts[10481]: have prev 0xa9b94, setting him to our next 0xd42e0
>> lwsts[10481]: have next 0xd42e0
>> lwsts[10481]: remove_wsi_socket_from_fds: wsi=0xd2cf8, sock=16, fds
>pos=5,
>> end guy pos=6, endfd=0
>> lwsts[10481]: calling back CLOSED
>
>Do you have an explanation for this TIME_WAIT 1 minute occurence?

It's a tcp level thing... the side that closes the connection is supposed to be in TIME_WAIT for 2 x MSL, so any delayed packets from the peer of the closed connection die of old age, and so can't turn up later when the connection has been reopened for something else, corrupting it.

MSL (Maximum Segment Lifetime) is set by the OS, you can meddle it but unless you're so busy you're running out of ports, no point.

-Andy

>Best regards,
>Thomas
>
>
>2016-07-06 11:58 GMT+02:00 Andy Green <andy at warmcat.com>:
>
>> On Wed, 2016-07-06 at 11:27 +0200, Thomas Spitz wrote:
>> > Hello,
>> > When my lws server has reached its maximum of client, I am closing
>> > the connection abruptly using return -1 on
>LWS_CALLBACK_ESTABLISHED.
>> >
>> > I would like to give some further information to the client for the
>> > reason why I do that.
>> > I am trying to use lws_close_reason in the following way:
>> > > unsigned char * closeReasonData = "MAX CONNECTION REACHED!";
>> > >
>lws_close_reason(wsi,LWS_CLOSE_STATUS_NORMAL,closeReasonData,sizeof
>> > > (closeReasonData));
>> > But I get the following error:
>> > > ws_close_reason: Assertion `wsi->mode == LWSCM_WS_SERVING || wsi-
>> > > >mode == LWSCM_WS_CLIENT' failed.
>> >  I suppose I have to set LWSCM_WS_SERVING somewhere?
>>
>> No, that's an internal state about what kind of connection it is and
>> what it's doing, you don't have to do anything about it.  Lws tracks
>it
>> from what you did with the public apis and what the connection has
>been
>> up to with its peer.
>>
>> Looking at the code, he calls _ESTABLISHED callback a little bit
>before
>> setting the mode LWSCM_WS_SERVING, it used to be he would drop the ah
>> (header parsing info) when he changed modes is why.  But that is no
>> longer the case since a few versions ago with the introduction of the
>> ah pool.
>>
>> So there's no reason for that any more I think... I pushed a patch on
>> master that makes the ESTABLISHED callback come a little bit later
>> after the mode is set, it seems to work OK here please give it a try.
>>
>>
>https://github.com/warmcat/libwebsockets/commit/926d7a8c0cd78ba18ec799e
>> fd9bba112e5814111
>>
>>
>> > By the way is it appropriate to use LWS_CLOSE_STATUS_NORMAL and
>data
>> > for this "Max connextion reached" purpose?
>>
>> You could maybe use LWS_CLOSE_STATUS_UNEXPECTED_CONDITION maybe,
>NORMAL
>> makes it sound a bit too normal.  There isn't a defined one like
>> TOO_BUSY or whatever.  It's just for diagnostics though so probably
>> nothing cares either way.
>>
>> -Andy
>>
>>
>> > I am using master 2.0.0 from 2016/07/16
>> >
>> > Thanks in advance,
>> > Thomas
>> > _______________________________________________
>> > Libwebsockets mailing list
>> > Libwebsockets at ml.libwebsockets.org
>> > http://libwebsockets.org/mailman/listinfo/libwebsockets
>>




More information about the Libwebsockets mailing list