moscajs / mosca

MQTT broker as a module
mosca.io
3.2k stars 513 forks source link

fatal error in forwardOfflinePackets #727

Open juanenriqueescobar opened 6 years ago

juanenriqueescobar commented 6 years ago

Hi, persistence has a bug:

mqtt/node_modules/mosca/node_modules/mongodb/lib/utils.js:98
process.nextTick(function() { throw err; });
^
TypeError: Cannot set property 'offline' of undefined
at Cursor.<anonymous> (/usr/share/rtlsa/taximqtt/node_modules/mosca/lib/persistence/abstract.js:129:22)
at emitOne (events.js:115:13)
at Cursor.emit (events.js:210:7)
at /usr/share/rtlsa/taximqtt/node_modules/mosca/node_modules/mongodb/lib/cursor.js:1050:14
at handleCallback (/usr/share/rtlsa/taximqtt/node_modules/mosca/node_modules/mongodb/lib/utils.js:96:12)
at /usr/share/rtlsa/taximqtt/node_modules/mosca/node_modules/mongodb/lib/cursor.js:660:20
at handleCallback (/usr/share/rtlsa/taximqtt/node_modules/mosca/node_modules/mongodb-core/lib/cursor.js:159:5)
at /usr/share/rtlsa/taximqtt/node_modules/mosca/node_modules/mongodb-core/lib/cursor.js:577:22
at queryCallback (/usr/share/rtlsa/taximqtt/node_modules/mosca/node_modules/mongodb-core/lib/cursor.js:200:16)
at Callbacks.emit (/usr/share/rtlsa/taximqtt/node_modules/mosca/node_modules/mongodb-core/lib/topologies/server.js:119:3)
at Connection.messageHandler (/usr/share/rtlsa/taximqtt/node_modules/mosca/node_modules/mongodb-core/lib/topologies/server.js:397:23)
at Socket.<anonymous> (/usr/share/rtlsa/taximqtt/node_modules/mosca/node_modules/mongodb-core/lib/connection/connection.js:302:22)
at emitOne (events.js:115:13)
at Socket.emit (events.js:210:7)
at addChunk (_stream_readable.js:266:12)
at readableAddChunk (_stream_readable.js:253:11)
at Socket.Readable.push (_stream_readable.js:211:10)
at TCP.onread (net.js:585:20)

persistence/abstract.js:129:22 has:

that.streamOfflinePackets(client, function(err, packet) {
      packet.offline = true;
      client.logger.debug({ packet: packet }, "Forwarding offline packet");
      client.forward(packet.topic, packet.payload, packet, packet.topic, packet.qos);
    }, done);

the err is not checked and packet is null !!!!

The problem is presented when 500 clients try to reconnect at same time after network failure, persistence is in mongodb.

juanenriqueescobar commented 6 years ago

in file lib/persistence/mongo.js line 350 has:

stream.on("error", cb);

so, if mongodb fail, the callback is invoked, but the error is not handled and crash..