wss: Secure Websocket leads to out of memory

Discussions about Z-Way software and Z-Wave technology in general
Post Reply
injecteer
Posts: 13
Joined: 29 Sep 2016 16:28

wss: Secure Websocket leads to out of memory

Post by injecteer » 16 Feb 2017 13:59

Recently I switched my server over to wss:// protocol and upon re-connection I get "Out of memory" errors.
Just before connecting to a ws(s) endpoint, a short http-ping request is made to ensure server's accessibility.

The code looks like:

Code: Select all

  function connect() {
    var self = this;
    http.request( { url:self.pingUrl, async:true, 
      success:function( resp ){ self.connectWS(); }, 
      error:function( resp ){ console.log( 'Ping ' + self.pingUrl + ' not accessible: ' + JSON.stringify( resp ) ); } 
    } );
  }

  function connectWS() {
    var self = this;
    var ws = new sockets.websocket( 'wss://myserv.com' );
    self.ws = ws;
    ws.onclose = function ( e ) {
      console.log( 'WebSocket closed. Will try to re-open it...' );
      self.timer = setInterval( function () {
        if( !self.ws || self.ws.readyState == self.ws.CLOSED ) self.connect();
      }, 120000 );
    };
  }
[2017-02-16 11:27:20.088] [core] WebSocket closed. Will try to re-open it...
[2017-02-16 11:29:20.227] [core] Ping https://ping.myserv.com not access
ible: {"status":-1,"statusText":"Out of memory"}


If I restart the server, it runs smoothly again.

What can I do to ease the memory pain?

User avatar
PoltoS
Posts: 5081
Joined: 26 Jan 2011 19:36

Re: wss: Secure Websocket leads to out of memory

Post by PoltoS » 17 Feb 2017 02:58

Do you mean that on WSS you can not use anymore normal http.request? Sounds pretty strange!

Can you share the minimal code to reproduct the error. If possible, with some public services, so we can run it too.

injecteer
Posts: 13
Joined: 29 Sep 2016 16:28

Re: wss: Secure Websocket leads to out of memory

Post by injecteer » 17 Feb 2017 14:52

The flow is:
* send http.request() to "ping" the server, and if it's accessible
* open the websocket via wss

Then, if the websocket connection breaks (e.g. the server restarts) the flow starts over again and the http ping throws the out of memory.

User avatar
PoltoS
Posts: 5081
Joined: 26 Jan 2011 19:36

Re: wss: Secure Websocket leads to out of memory

Post by PoltoS » 27 Feb 2017 23:01

I think the problem is in the circular dependencies - you call connectWS from connect from connectWS from connect from .... and so on. You need to deal very gracefully to instruct garbage collector not to get a memory leak

brizz
Posts: 6
Joined: 18 Oct 2017 19:15

Re: wss: Secure Websocket leads to out of memory

Post by brizz » 18 Oct 2017 19:45

Hi!

Sorry to resurrect a slightly older thread, but has any progress been made on this issue? Using 2.3.6 I still can't use Websockets. Whenever I try to use Websockets (either server or client) closing the socket seems to completely break SSL/HTTPS functionality on the server. Any HTTPS calls result in {"status":-1,"statusText":"Out of memory"} as injecteer mentioned, and if I log out of /smarthome I can no longer log back in - the error logs show:

Code: Select all

[2017-10-18 17:32:46.292] [E] [HTTP] Callback execution error: Error: Invalid digest
    at Error (native)
    at Object.crypto.sha512 (zway/crypto:25:51)
    at hashPassword (automation/Utils.js:72:48)
    at ZAutomationAPIWebRequest._.extend.verifyLogin (automation/ZAutomationAPIProvider.js:281:172)
    at ZAutomationAPIWebRequest.ZAutomationWebRequest.handleRequest (automation/WebserverRequestRouter.js:259:35)
    at automation/WebserverRequestRouter.js:32:29
    at WebServer.document_root (automation/Webserver.js:35:11)
I've attached a sample module that causes this. Simply install the module, start an instance and then stop that instance (thus calling self.sockServer.close()) and the issue occurs. It's not just limited to servers, though - trying to connect a Websocket client also has the same problem.

If there's anything else I can provide to help debug/fix, let me know!
Attachments
websocketserver_module.zip
(10.4 KiB) Downloaded 82 times

User avatar
PoltoS
Posts: 5081
Joined: 26 Jan 2011 19:36

Re: wss: Secure Websocket leads to out of memory

Post by PoltoS » 18 Oct 2017 23:42

It looks like internal state of OpenSSL gets mangled. We were suggested to use newer OpenSSL, that has more isolation from one instance to another. We plan to investigate this in near future

brizz
Posts: 6
Joined: 18 Oct 2017 19:15

Re: wss: Secure Websocket leads to out of memory

Post by brizz » 19 Oct 2017 07:08

That's good to hear! The current API and system are fantastic, so having lots of fun writing modules and integrating things I use. Being able to implement a few websocket services would be really cool.

Post Reply