From 5f843feb90ddf10674f58cebfb52f9ee4b9b060f Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Mon, 24 Dec 2012 19:50:25 -0300 Subject: [PATCH] namespace: make sure not to fire `connection` if underlying client closed after `next` is called from a middleware --- lib/namespace.js | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/namespace.js b/lib/namespace.js index 047482132d..d301b231ec 100644 --- a/lib/namespace.js +++ b/lib/namespace.js @@ -141,21 +141,25 @@ Namespace.prototype.add = function(client, fn){ var self = this; this.run(socket, function(err){ process.nextTick(function(){ - if (err) return socket.err(err.data || err.message); - - // track socket - self.sockets.push(socket); - - // it's paramount that the internal `onconnect` logic - // fires before user-set events to prevent state order - // violations (such as a disconnection before the connection - // logic is complete) - socket.onconnect(); - if (fn) fn(); - - // fire user-set events - self.emit('connect', socket); - self.emit('connection', socket); + if ('open' == client.conn.readyState) { + if (err) return socket.error(err.data || err.message); + + // track socket + self.sockets.push(socket); + + // it's paramount that the internal `onconnect` logic + // fires before user-set events to prevent state order + // violations (such as a disconnection before the connection + // logic is complete) + socket.onconnect(); + if (fn) fn(); + + // fire user-set events + self.emit('connect', socket); + self.emit('connection', socket); + } else { + debug('next called after client was closed - ignoring socket'); + } }); }); return socket;