Skip to content

Commit

Permalink
Fix memory leak in subscription topic mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
bverhoeven committed Sep 9, 2022
1 parent 00bf657 commit df7b8d1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,7 @@ MqttClient.prototype._handleAck = function (packet) {
}
}
}
delete this.messageIdToTopic[messageId]
this._invokeStoreProcessingQueue()
cb(null, packet)
break
Expand Down
39 changes: 39 additions & 0 deletions test/abstract_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3199,4 +3199,43 @@ module.exports = function (server, config) {
})
})
})

describe('message id to subscription topic mapping', () => {
it('should not create a mapping if resubscribe is disabled', function (done) {
var client = connect({ resubscribe: false })
client.subscribe('test1')
client.subscribe('test2')
assert.strictEqual(Object.keys(client.messageIdToTopic).length, 0)
client.end(true, done)
})

it('should create a mapping for each subscribe call', function (done) {
var client = connect()
client.subscribe('test1')
assert.strictEqual(Object.keys(client.messageIdToTopic).length, 1)
client.subscribe('test2')
assert.strictEqual(Object.keys(client.messageIdToTopic).length, 2)

client.subscribe(['test3', 'test4'])
assert.strictEqual(Object.keys(client.messageIdToTopic).length, 3)
client.subscribe(['test5', 'test6'])
assert.strictEqual(Object.keys(client.messageIdToTopic).length, 4)

client.end(true, done)
})

it('should remove the mapping after suback', function (done) {
var client = connect()
client.once('connect', function () {
client.subscribe('test1', { qos: 2 }, function () {
assert.strictEqual(Object.keys(client.messageIdToTopic).length, 0)

client.subscribe(['test2', 'test3'], { qos: 2 }, function () {
assert.strictEqual(Object.keys(client.messageIdToTopic).length, 0)
client.end(done)
})
})
})
})
})
}

0 comments on commit df7b8d1

Please sign in to comment.