Permalink
Browse files

avoid crashes due to socket errors during shutdown

  • Loading branch information...
1 parent e1b3267 commit 8af27fd87071fb7f858bd5dbadcafc54890195fa @einaros einaros committed Mar 30, 2012
Showing with 10 additions and 4 deletions.
  1. +10 −4 lib/WebSocket.js
View
@@ -270,7 +270,10 @@ WebSocket.prototype.stream = function(options, cb) {
*/
WebSocket.prototype.terminate = function() {
- if (this._socket) this._socket.end();
+ if (this._socket) {
+ try { this._socket.end(); }
+ catch (e) { this._socket.destroy(); }
+ }
else if (this.readyState == WebSocket.CONNECTING) {
this._readyState = WebSocket.CLOSED;
}
@@ -464,14 +467,16 @@ function initAsClient(address, options) {
// client closed before server accepted connection
self.emit('close');
removeAllListeners(self);
- socket.end();
+ try { socket.end(); }
+ catch (e) { socket.destroy(); }
return;
}
var serverKey = res.headers['sec-websocket-accept'];
if (typeof serverKey == 'undefined' || serverKey !== expectedServerKey) {
self.emit('error', 'invalid server key');
removeAllListeners(self);
- socket.end();
+ try { socket.end(); }
+ catch (e) { socket.destroy(); }
return;
}
@@ -500,7 +505,8 @@ function establishConnection(ReceiverClass, SenderClass, socket, upgradeHead) {
self._readyState = WebSocket.CLOSED;
if (socket) {
removeAllListeners(socket);
- socket.end();
+ try { socket.end(); }
+ catch (e) { socket.destroy(); }
self._socket = null;
socket = null;
}

0 comments on commit 8af27fd

Please sign in to comment.