Skip to content

Commit

Permalink
Added force closing of exchanges on shutdown of the node (#1660)
Browse files Browse the repository at this point in the history
* Added force closing of exchanges on shutdown of the node

* Fix typo

---------

Co-authored-by: lauckhart <[email protected]>
  • Loading branch information
Apollon77 and lauckhart authored Jan 25, 2025
1 parent cef7ec3 commit 51ff4db
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions packages/protocol/src/protocol/ExchangeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
18 changes: 15 additions & 3 deletions packages/protocol/src/protocol/MessageExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}
Expand Down

0 comments on commit 51ff4db

Please sign in to comment.