Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the leak was here, we were not decresing clients counter

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))
})
Loading