From bb0770ccd0096189ab80e7c0d2d77e24a0c90070 Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Wed, 26 Jun 2024 10:27:11 +0200 Subject: [PATCH 1/2] Clear subscriptions from the subscriber node This PR includes two changes. First we remove all subscriptions from the same peerNode when "keepSubscriptions=false" is used for a new subscription and not only subscriptions from the same session. Additionally, it fixes a bug where not all subscriptions from the sessions were really cleared. --- packages/matter.js/src/MatterDevice.ts | 4 ++++ .../src/protocol/interaction/InteractionServer.ts | 6 ++++-- packages/matter.js/src/session/SecureSession.ts | 3 ++- packages/matter.js/src/session/SessionManager.ts | 8 ++++++++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/matter.js/src/MatterDevice.ts b/packages/matter.js/src/MatterDevice.ts index 130e25e85a..5062905df6 100644 --- a/packages/matter.js/src/MatterDevice.ts +++ b/packages/matter.js/src/MatterDevice.ts @@ -504,6 +504,10 @@ export class MatterDevice { return { session, channel: await networkInterface.openChannel(device.addresses[0]) }; } + async clearSubscriptionsForNode(peerNodeId: NodeId, flushSubscriptions?: boolean) { + await this.#sessionManager.clearSubscriptionsForNode(peerNodeId, flushSubscriptions); + } + async close() { this.#isClosing = true; await this.endCommissioning(); diff --git a/packages/matter.js/src/protocol/interaction/InteractionServer.ts b/packages/matter.js/src/protocol/interaction/InteractionServer.ts index dadeab345a..174ab1cb2d 100644 --- a/packages/matter.js/src/protocol/interaction/InteractionServer.ts +++ b/packages/matter.js/src/protocol/interaction/InteractionServer.ts @@ -1061,8 +1061,10 @@ export class InteractionServer implements ProtocolHandler, Interac } if (!keepSubscriptions) { - logger.debug(`Clear subscriptions for Session ${session.name} because keepSubscriptions=false`); - await session.clearSubscriptions(true); + logger.debug( + `Clear subscriptions for Subscriber node ${session.peerNodeId} because keepSubscriptions=false`, + ); + await session.context.clearSubscriptionsForNode(session.peerNodeId, true); } const maxInterval = subscriptionHandler.getMaxInterval(); diff --git a/packages/matter.js/src/session/SecureSession.ts b/packages/matter.js/src/session/SecureSession.ts index ec77675a77..e4baf073c4 100644 --- a/packages/matter.js/src/session/SecureSession.ts +++ b/packages/matter.js/src/session/SecureSession.ts @@ -290,7 +290,8 @@ export class SecureSession extends Session { } async clearSubscriptions(flushSubscriptions = false) { - for (const subscription of this.#subscriptions) { + const subscriptions = [...this.#subscriptions]; // get all values because subscriptions will remove themselves when cancelled + for (const subscription of subscriptions) { await subscription.cancel(flushSubscriptions); } this.#subscriptions.length = 0; diff --git a/packages/matter.js/src/session/SessionManager.ts b/packages/matter.js/src/session/SessionManager.ts index 0fb80aecef..501e7a55f8 100644 --- a/packages/matter.js/src/session/SessionManager.ts +++ b/packages/matter.js/src/session/SessionManager.ts @@ -343,6 +343,14 @@ export class SessionManager { })); } + async clearSubscriptionsForNode(nodeId: NodeId, flushSubscriptions?: boolean) { + for (const session of this.#sessions) { + if (session.peerNodeId === nodeId) { + await session.clearSubscriptions(flushSubscriptions); + } + } + } + async close() { await this.storeResumptionRecords(); for (const session of this.#sessions) { From 106782662653164d5409605fda968c0aedcc757c Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Wed, 26 Jun 2024 19:53:09 +0200 Subject: [PATCH 2/2] Changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7fce3ca9a5..4e2844d4ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ The main work (all changes without a GitHub username in brackets in the below li ### __WORK IN PROGRESS__ --> +### __WORK IN PROGRESS__ +* Matter-Core functionality: + * Fix: Makes sure to clear all subscriptions from the subscriber noe and not only the current session when not keeping subscriptions + ### 0.9.2 (2024-06-20) * Matter-Core functionality: * Enhancement: Added some more certification relevant checks in Interaction server