From f42882c9e20713b450249d60386334b8127c5060 Mon Sep 17 00:00:00 2001 From: Gianluca-Casagrande-Stiga <81612076+Gianluca-Casagrande-Stiga@users.noreply.github.com> Date: Fri, 12 Jan 2024 15:58:02 +0100 Subject: [PATCH] fix: delete already closed client on closeSameClients (#929) Co-authored-by: Daniel Lando --- aedes.js | 8 +++++++- test/events.js | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/aedes.js b/aedes.js index fd4f9b72..956b7409 100644 --- a/aedes.js +++ b/aedes.js @@ -170,7 +170,13 @@ function Aedes (opts) { const clientId = packet.payload.toString() if (that.clients[clientId] && serverId !== that.id) { - that.clients[clientId].close(done) + if (that.clients[clientId].closed) { + // remove the client from the list if it is already closed + delete that.clients[clientId] + done() + } else { + that.clients[clientId].close(done) + } } else { done() } diff --git a/test/events.js b/test/events.js index 40191395..cd2137a1 100644 --- a/test/events.js +++ b/test/events.js @@ -221,3 +221,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) + + const clientId = 'closed-client' + const broker = aedes() + + // simulate a closed client on the broker + broker.clients[clientId] = { closed: true } + + // 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 + }) + + t.teardown(broker.close.bind(broker)) +})