From bb5ad1b0f499ccd72ed1e67c8d6368fc1bc97ae5 Mon Sep 17 00:00:00 2001 From: Roman Shtylman Date: Tue, 26 Nov 2013 18:20:53 -0500 Subject: [PATCH] cleanup if server close throws --- proxy.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/proxy.js b/proxy.js index 79f7baa..2d2aa10 100644 --- a/proxy.js +++ b/proxy.js @@ -40,15 +40,20 @@ var Proxy = function(opt, cb) { function maybe_tcp_close() { clearTimeout(conn_timeout); conn_timeout = setTimeout(function() { - client_server.close(); - clearTimeout(conn_timeout); + + // sometimes the server is already closed but the event has not fired? + try { + clearTimeout(conn_timeout); + client_server.close(); + } catch (err) { + cleanup(); + } }, 5000); } maybe_tcp_close(); - // no longer accepting connections for this id - client_server.on('close', function() { + function cleanup() { debug('closed tcp socket for client(%s)', id); clearTimeout(conn_timeout); @@ -59,7 +64,10 @@ var Proxy = function(opt, cb) { }); self.emit('end'); - }); + } + + // no longer accepting connections for this id + client_server.on('close', cleanup); // new tcp connection from lt client client_server.on('connection', function(socket) {