From b530c1a43decd99b26fee43b6a79101f7d7c6f3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nathana=C3=ABl=20L=C3=A9caud=C3=A9?= Date: Fri, 25 Nov 2016 13:14:51 -0500 Subject: [PATCH] TCPGet: Ensure done() is called only once (#1068) * Add additional safety checks to avoid acting on non-existent objects * TCPGet: yet more checks * TCPGet: seperate connected properties * TCPGet: properly handle node.done() --- nodes/core/io/31-tcpin.js | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/nodes/core/io/31-tcpin.js b/nodes/core/io/31-tcpin.js index 96b62effd..fd99bd4b7 100644 --- a/nodes/core/io/31-tcpin.js +++ b/nodes/core/io/31-tcpin.js @@ -541,7 +541,19 @@ module.exports = function(RED) { if (clients[connection_id]) { clients[connection_id].connected = false; } - if (node.done) { node.done(); } + + var anyConnected = false; + + for (var client in clients) { + if (clients[client].connected) { + anyConnected = true; + break; + } + } + if (node.done && !anyConnected) { + clients = {}; + node.done(); + } }); clients[connection_id].client.on('error', function() { @@ -556,6 +568,7 @@ module.exports = function(RED) { }); clients[connection_id].client.on('timeout',function() { + //console.log("TIMEOUT"); clients[connection_id].connected = false; node.status({fill:"grey",shape:"dot",text:"tcpin.errors.connect-timeout"}); //node.warn(RED._("tcpin.errors.connect-timeout")); @@ -579,9 +592,20 @@ module.exports = function(RED) { for (var client in clients) { clients[client].client.destroy(); } - clients = {}; node.status({}); - done(); + + var anyConnected = false; + for (var c in clients) { + if (clients[c].connected) { + anyConnected = true; + break; + } + } + + if (!anyConnected) { + clients = {}; + done(); + } }); }