[Libwebsockets] Is there a way to reuse a lws_context without destroying it

Ash 20001 ash20001 at hotmail.com
Thu Jun 15 08:43:50 CEST 2017


  1.  lws 2.2 made no difference
  2.  i took a wireshark capture and found out the server indeed was sending a FIN to the client. But the same server if I don't reuse the context, it works, no spurious FIN. The server is running a java version of websockets.  Any idea why context reuse could be screwing up the server? i assume some state somewhere in the context.



________________________________
From: Andy Green <andy at warmcat.com>
Sent: Wednesday, June 14, 2017 6:17 PM
To: Ash 20001; libwebsockets at ml.libwebsockets.org
Subject: Re: [Libwebsockets] Is there a way to reuse a lws_context without destroying it



On June 15, 2017 9:08:26 AM GMT+08:00, Ash 20001 <ash20001 at hotmail.com> wrote:
>I got the logs with the DEBUG flag on, does this mean the server closed
>the connection?
>
>1003 :  2017/6/14 18:05:19.590DBG  :  [5708] lws:
>lws_header_table_attach: wsi 0x7fddf002de00: ah 0x7fddf0009f70: count 1
>(on exit)
>1004 :  2017/6/14 18:05:19.590DBG  :  [5708] lws: lws_client_connect:
>direct conn
>1005 :  2017/6/14 18:05:19.590DBG  :  [5708] lws: lws_client_connect_2
>1006 :  2017/6/14 18:05:19.590DBG  :  [5708] lws: lws_client_connect_2:
>address 105.145.65.86
>1022 :  2017/6/14 18:05:19.593DBG  :  [5708] lws:
>insert_wsi_socket_into_fds: 0x7fddf002de00: tsi=0, sock=14,
>pos-in-fds=2
>1023 :  2017/6/14 18:05:19.593DBG  :  [5708] lws:
>LWS_CALLBACK_WSI_CREATE
>1024 :  2017/6/14 18:05:19.593DBG  :  [5708] lws: nonblocking connect
>retry
>1025 :  2017/6/14 18:05:19.593DBG  :  [5708] lws: wsc loop starting
>1026 :  2017/6/14 18:05:19.593DBG  :  [5708] lws: service_fd: closing
>due to 0 length read

This is how lws learns the peer closed the connection.

You can use tcpdump or so to look at the packets that were exchanged if it's not SSL.

-Andy

>1027 :  2017/6/14 18:05:19.593DBG  :  [5708] lws: Close and handled
>1028 :  2017/6/14 18:05:19.593DBG  :  [5708] lws: lws_close_free_wsi:
>shutting down connection: 0x7fddf001f910
>1029 :  2017/6/14 18:05:19.593DBG  :  [5708] lws: service_fd: closing
>due to 0 length read
>1030 :  2017/6/14 18:05:19.593DBG  :  [5708] lws: Close and handled
>1031 :  2017/6/14 18:05:19.593DBG  :  [5708] lws: lws_close_free_wsi:
>real just_kill_connection: 0x7fddf001f910
>1032 :  2017/6/14 18:05:19.593DBG  :  [5708] lws:
>remove_wsi_socket_from_fds: wsi=0x7fddf001f910, sock=13, fds pos=1, end
>guy pos=3, endfd=0
>1033 :  2017/6/14 18:05:19.593DBG  :  [5708] lws: calling back CLOSED
>1034 :  2017/6/14 18:05:19.593DBG  :  [5708] lws: LWS_CALLBACK_CLOSED
>
>
>
>
>________________________________
>From: Andy Green <andy at warmcat.com>
>Sent: Wednesday, June 14, 2017 3:53 PM
>To: Ash 20001; libwebsockets at ml.libwebsockets.org
>Subject: Re: [Libwebsockets] Is there a way to reuse a lws_context
>without destroying it
>
>
>
>On June 15, 2017 6:45:33 AM GMT+08:00, Ash 20001 <ash20001 at hotmail.com>
>wrote:
>>1.  you are right, the test-client does handle closed/connection error
>>callbacks, but the code itself does not initiate a disconnect from the
>>server but rather it handles cases where the server dies...not sure if
>>that would make a difference. in my application the client is
>>disconnecting.
>
>No test-client also does initiate a disconnect from the server.  The
>connection gets a random 'lifetime' when it is made, after sending that
>many messages it closes the connection and after the ratelimit period
>the outer loop will create a new one.
>
>>2.  I can try a later websockets however is there any logging I can
>put
>>into libwebsockets to know why after connection i get a
>callback_closed
>>event after some time randomly? The server is not disconnecting from
>>what I can see.
>
>As I said
>
>>>In later lws in CONNECTION_ERROR, *in may point to a string
>describing
>>>the problem.
>
>If lws did something that failed and initiates the close, there are
>like 20 different reason strings to help debug it.
>
>If the peer closes the socket though, 'in' will be NULL.
>
>You can turn up the logging to get additional clues, build in DEBUG
>mode at cmake (see README.build.md)  and set the logging level high (eg
>-d 65535 on the test client).
>
>-Andy
>
>>
>>
>>________________________________
>>From: Andy Green <andy at warmcat.com>
>>Sent: Wednesday, June 14, 2017 3:36 PM
>>To: Ash 20001; libwebsockets at ml.libwebsockets.org
>>Subject: Re: [Libwebsockets] Is there a way to reuse a lws_context
>>without destroying it
>>
>>
>>
>>On June 15, 2017 1:29:46 AM GMT+08:00, Ash 20001
><ash20001 at hotmail.com>
>>wrote:
>>>sorry i forgot to mention that on reconnect i am connecting to a
>>>different port and potentially even different server ip. Is that why
>>it
>>>won't work with the same context?
>>
>>Doubt it, every connect starts fresh.
>>
>>>________________________________
>>>From: Ash 20001 <ash20001 at hotmail.com>
>>>Sent: Wednesday, June 14, 2017 8:47 AM
>>>To: Andy Green
>>>Subject: Re: [Libwebsockets] Is there a way to reuse a lws_context
>>>without destroying it
>>>
>>>
>>>I am using 1.7 websockets. When I try it again, I start immediately
>>
>>1.7 is too old... please see if you still have a problem on master
>lws.
>>
>>>getting callback_closed messages after lws_client_connect_via_info is
>>>called. I have no idea why I am getting these messages but if I
>>destroy
>>>the context and then call lws_Create_Context again, it works.
>>
>>In later lws in CONNECTION_ERROR, *in may point to a string describing
>>the problem.
>>
>>>
>>>In libwebsockets-test-client, it doesn't seem to handle callback
>>closed
>>>or callback connection error. Normally during these events I exit out
>>
>>It does handle them, or it doesn't know it should retry (mymemory only
>>goesback to v2.0-ish though).
>>
>>>of the main lws_Service loop, and then later I try to call
>>>connect_via_info again, restart the service loop, and then run into
>>the
>>>issue above. What should I do to prevent this?
>>
>>See if it still happens on later lws.
>>
>>>Yes openssl is being trashed and messing around with other threads
>>>using curl to an https source for instance so I am trying to
>>workaround
>>>it because I cannot change the openssl version.
>>
>>You're going to have to change something...
>>
>>-Andy
>>
>>>
>>>________________________________
>>>From: Andy Green <andy at warmcat.com>
>>>Sent: Wednesday, June 14, 2017 12:29 AM
>>>To: libwebsockets at ml.libwebsockets.org; Ash 20001;
>>>libwebsockets at ml.libwebsockets.org
>>>Subject: Re: [Libwebsockets] Is there a way to reuse a lws_context
>>>without destroying it
>>>
>>>
>>>
>>>On June 14, 2017 2:35:24 PM GMT+08:00, Ash 20001
>><ash20001 at hotmail.com>
>>>wrote:
>>>>Is there a way to call lws_create_context just once for program
>>>>lifetime as a client, and use that context for many connections (ie.
>>>>connect to a server, disconnect, repeat)? I have tried but it
>doesn't
>>>>seem to work on the second connection.
>>>
>>>Yes it should be fine.
>>>
>>>Libwebsockets-test-client does exactly this in its normal operation.
>>>
>>>What exactly happens (and does it happen on lws master?)
>>>
>>>>The reason I ask is because I am having SSL issues with other
>threads
>>>>that I have no control over in a process which use ssl functions in
>>>>CURL. lws_destroy_context globally destructs SSL state and causes
>>>>issues.  My openssl version is rather old 0.9.8 and cannot be
>>changed.
>>>
>>>Yeah.... we have seen it before.  Later openssl fixed it.
>>>
>>>It will trash openssl for the whole process.  Is that why you can't
>>get
>>>a second client connection in lws?
>>>
>>>-Andy
>>>
>>>>
>>>>Thanks!
>>>>
>>>>(Sorry if I sent two emails, not sure if the first one went through
>>>>because I was trying to subscribe to the mailing list).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20170615/fbbec2d6/attachment-0002.html>


More information about the Libwebsockets mailing list