Skip to content

Commit 7ec4b8f

Browse files
authoredJan 18, 2022
fix(sendPacket): drain leak (#1401)
1 parent 4604b10 commit 7ec4b8f

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed
 

‎lib/client.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ function sendPacket (client, packet, cb) {
175175
debug('sendPacket :: writing to stream')
176176
const result = mqttPacket.writeToStream(packet, client.stream, client.options)
177177
debug('sendPacket :: writeToStream result %s', result)
178-
if (!result && cb) {
178+
if (!result && cb && cb !== nop) {
179179
debug('sendPacket :: handle events on `drain` once through callback.')
180180
client.stream.once('drain', cb)
181181
} else if (cb) {
@@ -190,6 +190,8 @@ function flush (queue) {
190190
Object.keys(queue).forEach(function (messageId) {
191191
if (typeof queue[messageId].cb === 'function') {
192192
queue[messageId].cb(new Error('Connection closed'))
193+
// This is suspicious. Why do we only delete this if we have a callbck?
194+
// If this is by-design, then adding no as callback would cause this to get deleted unintentionally.
193195
delete queue[messageId]
194196
}
195197
})
@@ -569,7 +571,7 @@ MqttClient.prototype._handlePacket = function (packet, done) {
569571

570572
MqttClient.prototype._checkDisconnecting = function (callback) {
571573
if (this.disconnecting) {
572-
if (callback) {
574+
if (callback && callback !== nop) {
573575
callback(new Error('client disconnecting'))
574576
} else {
575577
this.emit('error', new Error('client disconnecting'))
@@ -1581,7 +1583,7 @@ MqttClient.prototype._handleAck = function (packet) {
15811583
const that = this
15821584
let err
15831585

1584-
if (!cb) {
1586+
if (!cb || cb === nop) {
15851587
debug('_handleAck :: Server sent an ack in error. Ignoring.')
15861588
// Server sent an ack in error, ignore it.
15871589
return

0 commit comments

Comments
 (0)