Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LRWebSocketConnection should listen for any socket errors #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

compulim
Copy link

@compulim compulim commented Jun 5, 2015

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
Author

Ping.

@yuki-takei
Copy link

yuki-takei commented Jul 9, 2017

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