tls: handle errors on socket before releasing it
authorFedor Indutny <fedor.indutny@gmail.com>
Wed, 7 Aug 2013 12:50:36 +0000 (16:50 +0400)
committerFedor Indutny <fedor.indutny@gmail.com>
Wed, 14 Aug 2013 17:10:32 +0000 (21:10 +0400)
Fix sudden uncatchable ECONNRESETs, when using https server.

lib/_tls_wrap.js

index 2dc5743..ae6950b 100644 (file)
@@ -144,6 +144,8 @@ function TLSSocket(socket, options) {
   this.authorized = false;
   this.authorizationError = null;
 
+  this.on('error', this._tlsError);
+
   if (!this._handle)
     this.once('connect', this._init.bind(this));
   else
@@ -234,6 +236,13 @@ TLSSocket.prototype._tlsError = function(err) {
     this.emit('error', err);
 };
 
+TLSSocket.prototype._releaseControl = function() {
+  if (this._controlReleased)
+    return;
+  this._controlReleased = true;
+  this.removeListener('error', this._tlsError);
+};
+
 TLSSocket.prototype._finishInit = function() {
   if (process.features.tls_npn) {
     this.npnProtocol = this.ssl.getNegotiatedProtocol();
@@ -454,7 +463,7 @@ function Server(/* [options], listener */) {
       }
 
       if (!socket.destroyed) {
-        socket._controlReleased = true;
+        socket._releaseControl();
         self.emit('secureConnection', socket);
       }
     });
@@ -604,7 +613,7 @@ exports.connect = function(/* [port, host], options, cb */) {
   });
 
   function onHandle() {
-    socket._controlReleased = true;
+    socket._releaseControl();
 
     if (options.session)
       socket.setSession(options.session);