From 6b3010f95b57e630cb66c9839b58d9f802269923 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Mon, 13 Jan 2014 11:27:09 +0000 Subject: [PATCH] Guard against null client in MQTT nodes Fixes #130 There was a timing window where a client could connect to a broker just as new flows were deployed that would cause the on-connect callback to be called after client has been set to null. This caused an NPE. The fix is to check client isn't null in the event handler. --- nodes/core/io/lib/mqttConnectionPool.js | 31 +++++++++++++------------ 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/nodes/core/io/lib/mqttConnectionPool.js b/nodes/core/io/lib/mqttConnectionPool.js index ee6f90e0d..425d3b2b4 100644 --- a/nodes/core/io/lib/mqttConnectionPool.js +++ b/nodes/core/io/lib/mqttConnectionPool.js @@ -84,21 +84,22 @@ module.exports = { } }; client.on('connect',function() { - - util.log('[mqtt] connected to broker tcp://'+broker+':'+port); - - connecting = false; - for (var s in subscriptions) { - var topic = subscriptions[s].topic; - var qos = subscriptions[s].qos; - var callback = subscriptions[s].callback; - client.subscribe(topic,qos); - } - //console.log("connected - publishing",queue.length,"messages"); - while(queue.length) { - var msg = queue.shift(); - //console.log(msg); - client.publish(msg.topic,msg.payload,msg.qos,msg.retain); + if (client) { + util.log('[mqtt] connected to broker tcp://'+broker+':'+port); + + connecting = false; + for (var s in subscriptions) { + var topic = subscriptions[s].topic; + var qos = subscriptions[s].qos; + var callback = subscriptions[s].callback; + client.subscribe(topic,qos); + } + //console.log("connected - publishing",queue.length,"messages"); + while(queue.length) { + var msg = queue.shift(); + //console.log(msg); + client.publish(msg.topic,msg.payload,msg.qos,msg.retain); + } } }); client.on('connectionlost', function(err) {