Skip to content

LRWebSocketConnection should listen for any socket errors#2

Open
compulim wants to merge 1 commit into
livereload:masterfrom
compulim:iss-errevent
Open

LRWebSocketConnection should listen for any socket errors#2
compulim wants to merge 1 commit into
livereload:masterfrom
compulim:iss-errevent

Conversation

@compulim

@compulim compulim commented Jun 5, 2015

Copy link
Copy Markdown

Background

LRWebSocketConnection should listen for any socket errors, otherwise, when the underlying socket emit error event due to TCP error, it will quit the host process.

Sample scenario

The code below will crash livereload-server instantly

!function (WebSocket) {
    var ws = new WebSocket('ws://localhost:35729/livereload');

    ws.on('open', function () {
        process.exit(0);
    });
}(require('ws'));

It will crash with the following error.

events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: read ECONNRESET
    at exports._errnoException (util.js:746:11)
    at TCP.onread (net.js:559:26)

@compulim

Copy link
Copy Markdown
Author

Ping.

@yuki-takei

yuki-takei commented Jul 9, 2017

Copy link
Copy Markdown

Override _createConnection

const LRWebSocketServer = require('livereload-server/lib/server');

/**
* prevent to crash socket with:
* -------------------------------------------------
* Error: read ECONNRESET
*     at exports._errnoException (util.js:1022:11)
*     at TCP.onread (net.js:569:26)
* -------------------------------------------------
*
* @see https://github.com/napcs/node-livereload/pull/15
*
*/
hackLRWebSocketServer() {
  const orgCreateConnection = LRWebSocketServer.prototype._createConnection;

  // replace https://github.com/livereload/livereload-server/blob/v0.2.3/lib/server.coffee#L74
  LRWebSocketServer.prototype._createConnection = function(socket) {
    // call original method with substituting 'this' obj
    orgCreateConnection.call(this, socket);

    socket.on('error', (err) => {
      console.warn(`[WARN] Worthless error in client socket: '${err}'`);
    });
  }
}

Driptap pushed a commit to Driptap/easy-livereload that referenced this pull request Jul 23, 2018
- Listens for error events on the websocket instance to avoid uncaught
ECONNRESET exceptions.

When no listener is attached to error event from the websocket - the
ECONNRESET expception is thrown: websockets/ws#1256

This is also somewhat relevant:
livereload/livereload-server#2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants