Skip to content

Commit

Permalink
fix: memory leak in connected clients counter (#979)
Browse files Browse the repository at this point in the history
Co-authored-by: Uladzimir Danko <uladzimirdanko@uladzimirsmbp14.home>
  • Loading branch information
uladzimir-danko and Uladzimir Danko authored Aug 28, 2024
1 parent 1a191b0 commit 9b9d524
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
10 changes: 7 additions & 3 deletions aedes.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function Aedes (opts) {
if (that.clients[clientId] && serverId !== that.id) {
if (that.clients[clientId].closed) {
// remove the client from the list if it is already closed
delete that.clients[clientId]
that.deleteClient(clientId)
done()
} else {
that.clients[clientId].close(done)
Expand Down Expand Up @@ -316,15 +316,19 @@ Aedes.prototype._finishRegisterClient = function (client) {
}

Aedes.prototype.unregisterClient = function (client) {
this.connectedClients--
delete this.clients[client.id]
this.deleteClient(client.id)
this.emit('clientDisconnect', client)
this.publish({
topic: $SYS_PREFIX + this.id + '/disconnect/clients',
payload: Buffer.from(client.id, 'utf8')
}, noop)
}

Aedes.prototype.deleteClient = function (clientId) {
this.connectedClients--
delete this.clients[clientId]
}

function closeClient (client, cb) {
this.clients[client].close(cb)
}
Expand Down
7 changes: 7 additions & 0 deletions lib/handlers/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ function addSubs (sub, done) {
func = blockDollarSignTopics(func)
}

if (client.closed || client.broker.closed) {
// a hack, sometimes client.close() or broker.close() happened
// before authenticate() comes back
// we don't continue subscription here
return
}

if (!client.subscriptions[topic]) {
client.subscriptions[topic] = new Subscription(qos, func, rh, rap, nl)
broker.subscribe(topic, func, done)
Expand Down
14 changes: 8 additions & 6 deletions test/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,20 @@ test('Test backpressure aedes published function', function (t) {
})

test('clear closed clients when the same clientId is managed by another broker', function (t) {
t.plan(1)
t.plan(2)

const clientId = 'closed-client'
const broker = aedes()
const aedesBroker = aedes()

// simulate a closed client on the broker
broker.clients[clientId] = { closed: true }
aedesBroker.clients[clientId] = { closed: true, broker: aedesBroker }
aedesBroker.connectedClients = 1

// simulate the creation of the same client on another broker of the cluster
broker.publish({ topic: '$SYS/anotherbroker/new/clients', payload: clientId }, () => {
t.equal(broker.clients[clientId], undefined) // check that the closed client was removed
aedesBroker.publish({ topic: '$SYS/anotherbroker/new/clients', payload: clientId }, () => {
t.equal(aedesBroker.clients[clientId], undefined) // check that the closed client was removed
t.equal(aedesBroker.connectedClients, 0)
})

t.teardown(broker.close.bind(broker))
t.teardown(aedesBroker.close.bind(aedesBroker))
})

0 comments on commit 9b9d524

Please sign in to comment.