From 51ff4dbedfe42e224529ba13b9c80a92bc2ed28c Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Sat, 25 Jan 2025 02:43:53 +0100 Subject: [PATCH] Added force closing of exchanges on shutdown of the node (#1660) * Added force closing of exchanges on shutdown of the node * Fix typo --------- Co-authored-by: lauckhart --- CHANGELOG.md | 3 +++ .../protocol/src/protocol/ExchangeManager.ts | 7 ++++--- .../protocol/src/protocol/MessageExchange.ts | 18 +++++++++++++++--- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72a79ed5e..7bcac8fb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,9 @@ The main work (all changes without a GitHub username in brackets in the below li - @project-chip/matter.js - Fix: Allows more cases when checking if a device is battery powered to address real world devices +- @matter/protocol + - Fix: Added force closing of exchanges on shutdown of the node + ## 0.12.0 (2025-01-23) - @matter/general diff --git a/packages/protocol/src/protocol/ExchangeManager.ts b/packages/protocol/src/protocol/ExchangeManager.ts index 95bb99574..59075f1d9 100644 --- a/packages/protocol/src/protocol/ExchangeManager.ts +++ b/packages/protocol/src/protocol/ExchangeManager.ts @@ -185,9 +185,10 @@ export class ExchangeManager { await MatterAggregateError.allSettled(this.#closers, "Error closing exchanges").catch(error => logger.error(error), ); - await MatterAggregateError.allSettled(this.#exchanges.values(), "Error closing exchanges").catch(error => - logger.error(error), - ); + await MatterAggregateError.allSettled( + Array.from(this.#exchanges.values()).map(exchange => exchange.close(true)), + "Error closing exchanges", + ).catch(error => logger.error(error)); this.#exchanges.clear(); } diff --git a/packages/protocol/src/protocol/MessageExchange.ts b/packages/protocol/src/protocol/MessageExchange.ts index cc6352d2a..7365b37c3 100644 --- a/packages/protocol/src/protocol/MessageExchange.ts +++ b/packages/protocol/src/protocol/MessageExchange.ts @@ -646,8 +646,16 @@ export class MessageExchange { return this.#timedInteractionTimer !== undefined && !this.#timedInteractionTimer.isRunning; } - async close() { - if (this.#closeTimer !== undefined) return; // close was already called + async close(force = false) { + if (this.#closeTimer !== undefined) { + if (force) { + // Force close does not wait any longer + this.#closeTimer.stop(); + return this.#close(); + } + // close was already called, so let retries happen because close not forced + return; + } this.#isClosing = true; if (this.#receivedMessageToAck !== undefined) { @@ -659,7 +667,11 @@ export class MessageExchange { } catch (error) { logger.error("An error happened when closing the exchange", error); } - } else if (this.#sentMessageToAck === undefined) { + if (force) { + // We have sent the Ack, so close here, no retries because close is forced + return this.#close(); + } + } else if (this.#sentMessageToAck === undefined || force) { // No message left that we need to ack and no sent message left that waits for an ack, close directly return this.#close(); }