From a26b90b5715154a6d44052e91424562cb8b0dd07 Mon Sep 17 00:00:00 2001 From: Brandon Everett Date: Fri, 7 Jun 2024 02:22:12 -0400 Subject: [PATCH] fix: delete will from persistence on disconnect (#957) Co-authored-by: Daniel Lando --- lib/client.js | 7 +++++++ test/will.js | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/client.js b/lib/client.js index 414d8e5f..e5ee42d0 100644 --- a/lib/client.js +++ b/lib/client.js @@ -313,7 +313,14 @@ Client.prototype.close = function (done) { }, noop) } }) + } else if (will) { + // delete the persisted will even on clean disconnect https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc385349232 + that.broker.persistence.delWill({ + id: that.id, + brokerId: that.broker.id + }, noop) } + that.will = null // this function might be called twice that._will = null diff --git a/test/will.js b/test/will.js index d2893d28..368544f6 100644 --- a/test/will.js +++ b/test/will.js @@ -420,6 +420,29 @@ test('does not deliver will when client sends a DISCONNECT', function (t) { }) }) +test('deletes from persistence on DISCONNECT', function (t) { + t.plan(2) + + const opts = { + clientId: 'abcde' + } + const broker = aedes() + t.teardown(broker.close.bind(broker)) + + const s = noError(willConnect(setup(broker), opts, function () { + s.inStream.end({ + cmd: 'disconnect' + }) + }), t) + + s.broker.persistence.getWill({ + id: opts.clientId + }, function (err, packet) { + t.error(err, 'no error') + t.notOk(packet) + }) +}) + test('does not store multiple will with same clientid', function (t) { t.plan(4)