@matter/protocol 0.17.8 → 0.17.9-alpha.0-20260805-a5d633a78

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/dist/cjs/bdx/BdxMessenger.d.ts.map +1 -1
  2. package/dist/cjs/bdx/BdxMessenger.js +1 -2
  3. package/dist/cjs/bdx/BdxMessenger.js.map +1 -1
  4. package/dist/cjs/bdx/BdxSession.js +1 -1
  5. package/dist/cjs/bdx/BdxSession.js.map +1 -1
  6. package/dist/cjs/peer/ControllerCommissioner.js +1 -1
  7. package/dist/cjs/peer/ControllerCommissioner.js.map +1 -1
  8. package/dist/cjs/peer/Peer.d.ts.map +1 -1
  9. package/dist/cjs/peer/Peer.js +1 -0
  10. package/dist/cjs/peer/Peer.js.map +1 -1
  11. package/dist/cjs/peer/PeerConnection.d.ts.map +1 -1
  12. package/dist/cjs/peer/PeerConnection.js +4 -0
  13. package/dist/cjs/peer/PeerConnection.js.map +1 -1
  14. package/dist/cjs/peer/PeerSet.d.ts +3 -3
  15. package/dist/cjs/peer/PeerSet.d.ts.map +1 -1
  16. package/dist/cjs/peer/PeerSet.js +17 -17
  17. package/dist/cjs/peer/PeerSet.js.map +1 -1
  18. package/dist/cjs/peer/PeerTimingParameters.d.ts +3 -3
  19. package/dist/cjs/peer/PeerTimingParameters.d.ts.map +1 -1
  20. package/dist/cjs/peer/PeerTimingParameters.js +12 -9
  21. package/dist/cjs/peer/PeerTimingParameters.js.map +2 -2
  22. package/dist/cjs/protocol/ExchangeManager.d.ts.map +1 -1
  23. package/dist/cjs/protocol/ExchangeManager.js +178 -162
  24. package/dist/cjs/protocol/ExchangeManager.js.map +1 -1
  25. package/dist/cjs/protocol/ExchangeProvider.d.ts.map +1 -1
  26. package/dist/cjs/protocol/ExchangeProvider.js.map +1 -1
  27. package/dist/cjs/protocol/MessageChannel.d.ts +6 -0
  28. package/dist/cjs/protocol/MessageChannel.d.ts.map +1 -1
  29. package/dist/cjs/protocol/MessageChannel.js +20 -4
  30. package/dist/cjs/protocol/MessageChannel.js.map +1 -1
  31. package/dist/cjs/protocol/MessageExchange.d.ts +11 -2
  32. package/dist/cjs/protocol/MessageExchange.d.ts.map +1 -1
  33. package/dist/cjs/protocol/MessageExchange.js +102 -21
  34. package/dist/cjs/protocol/MessageExchange.js.map +1 -1
  35. package/dist/cjs/securechannel/SecureChannelMessenger.js +4 -4
  36. package/dist/cjs/securechannel/SecureChannelMessenger.js.map +1 -1
  37. package/dist/cjs/session/NodeSession.d.ts.map +1 -1
  38. package/dist/cjs/session/NodeSession.js +1 -2
  39. package/dist/cjs/session/NodeSession.js.map +1 -1
  40. package/dist/cjs/session/Session.d.ts +11 -0
  41. package/dist/cjs/session/Session.d.ts.map +1 -1
  42. package/dist/cjs/session/Session.js +23 -2
  43. package/dist/cjs/session/Session.js.map +1 -1
  44. package/dist/cjs/session/case/CaseServer.d.ts.map +1 -1
  45. package/dist/cjs/session/case/CaseServer.js +1 -0
  46. package/dist/cjs/session/case/CaseServer.js.map +1 -1
  47. package/dist/cjs/session/pase/PaseServer.js +1 -1
  48. package/dist/cjs/session/pase/PaseServer.js.map +1 -1
  49. package/dist/esm/bdx/BdxMessenger.d.ts.map +1 -1
  50. package/dist/esm/bdx/BdxMessenger.js +1 -2
  51. package/dist/esm/bdx/BdxMessenger.js.map +1 -1
  52. package/dist/esm/bdx/BdxSession.js +1 -1
  53. package/dist/esm/bdx/BdxSession.js.map +1 -1
  54. package/dist/esm/peer/ControllerCommissioner.js +1 -1
  55. package/dist/esm/peer/ControllerCommissioner.js.map +1 -1
  56. package/dist/esm/peer/Peer.d.ts.map +1 -1
  57. package/dist/esm/peer/Peer.js +1 -0
  58. package/dist/esm/peer/Peer.js.map +1 -1
  59. package/dist/esm/peer/PeerConnection.d.ts.map +1 -1
  60. package/dist/esm/peer/PeerConnection.js +4 -0
  61. package/dist/esm/peer/PeerConnection.js.map +1 -1
  62. package/dist/esm/peer/PeerSet.d.ts +3 -3
  63. package/dist/esm/peer/PeerSet.d.ts.map +1 -1
  64. package/dist/esm/peer/PeerSet.js +17 -17
  65. package/dist/esm/peer/PeerSet.js.map +1 -1
  66. package/dist/esm/peer/PeerTimingParameters.d.ts +3 -3
  67. package/dist/esm/peer/PeerTimingParameters.d.ts.map +1 -1
  68. package/dist/esm/peer/PeerTimingParameters.js +20 -10
  69. package/dist/esm/peer/PeerTimingParameters.js.map +1 -1
  70. package/dist/esm/protocol/ExchangeManager.d.ts.map +1 -1
  71. package/dist/esm/protocol/ExchangeManager.js +178 -162
  72. package/dist/esm/protocol/ExchangeManager.js.map +1 -1
  73. package/dist/esm/protocol/ExchangeProvider.d.ts.map +1 -1
  74. package/dist/esm/protocol/ExchangeProvider.js.map +1 -1
  75. package/dist/esm/protocol/MessageChannel.d.ts +6 -0
  76. package/dist/esm/protocol/MessageChannel.d.ts.map +1 -1
  77. package/dist/esm/protocol/MessageChannel.js +20 -4
  78. package/dist/esm/protocol/MessageChannel.js.map +1 -1
  79. package/dist/esm/protocol/MessageExchange.d.ts +11 -2
  80. package/dist/esm/protocol/MessageExchange.d.ts.map +1 -1
  81. package/dist/esm/protocol/MessageExchange.js +104 -21
  82. package/dist/esm/protocol/MessageExchange.js.map +1 -1
  83. package/dist/esm/securechannel/SecureChannelMessenger.js +4 -4
  84. package/dist/esm/securechannel/SecureChannelMessenger.js.map +1 -1
  85. package/dist/esm/session/NodeSession.d.ts.map +1 -1
  86. package/dist/esm/session/NodeSession.js +1 -2
  87. package/dist/esm/session/NodeSession.js.map +1 -1
  88. package/dist/esm/session/Session.d.ts +11 -0
  89. package/dist/esm/session/Session.d.ts.map +1 -1
  90. package/dist/esm/session/Session.js +23 -2
  91. package/dist/esm/session/Session.js.map +1 -1
  92. package/dist/esm/session/case/CaseServer.d.ts.map +1 -1
  93. package/dist/esm/session/case/CaseServer.js +1 -0
  94. package/dist/esm/session/case/CaseServer.js.map +1 -1
  95. package/dist/esm/session/pase/PaseServer.js +1 -1
  96. package/dist/esm/session/pase/PaseServer.js.map +1 -1
  97. package/package.json +5 -5
  98. package/src/bdx/BdxMessenger.ts +1 -2
  99. package/src/bdx/BdxSession.ts +1 -1
  100. package/src/peer/ControllerCommissioner.ts +1 -1
  101. package/src/peer/Peer.ts +5 -0
  102. package/src/peer/PeerConnection.ts +7 -0
  103. package/src/peer/PeerSet.ts +23 -18
  104. package/src/peer/PeerTimingParameters.ts +32 -12
  105. package/src/protocol/ExchangeManager.ts +27 -2
  106. package/src/protocol/ExchangeProvider.ts +0 -2
  107. package/src/protocol/MessageChannel.ts +24 -5
  108. package/src/protocol/MessageExchange.ts +152 -24
  109. package/src/securechannel/SecureChannelMessenger.ts +4 -4
  110. package/src/session/NodeSession.ts +1 -2
  111. package/src/session/Session.ts +26 -2
  112. package/src/session/case/CaseServer.ts +1 -0
  113. package/src/session/pase/PaseServer.ts +1 -1
@@ -13,7 +13,6 @@ import {
13
13
  TransientPeerCommunicationError,
14
14
  } from "#peer/PeerCommunicationError.js";
15
15
  import { GroupSession } from "#session/GroupSession.js";
16
- import type { NodeSession } from "#session/NodeSession.js";
17
16
  import { Session } from "#session/Session.js";
18
17
  import { SessionParameters } from "#session/SessionParameters.js";
19
18
  import {
@@ -38,6 +37,7 @@ import {
38
37
  MatterFlowError,
39
38
  Millis,
40
39
  NetworkError,
40
+ Seconds,
41
41
  ServerAddress,
42
42
  ServerAddressUdp,
43
43
  Time,
@@ -46,6 +46,7 @@ import {
46
46
  Timestamp,
47
47
  } from "@matter/general";
48
48
  import {
49
+ BDX_PROTOCOL_ID,
49
50
  GroupId,
50
51
  NodeId,
51
52
  SECURE_CHANNEL_PROTOCOL_ID,
@@ -59,6 +60,23 @@ import { MRP } from "./MRP.js";
59
60
 
60
61
  const logger = Logger.get("MessageExchange");
61
62
 
63
+ /**
64
+ * How long a BDX peer waits for the next BDX message before it abandons the transfer.
65
+ *
66
+ * BDX peers bound a transfer by progress at the BDX layer, not by MRP: CHIP's OTA requestor aborts after this long
67
+ * awaiting a response, and neither our acknowledgement nor the peer's own MRP retransmissions reset its clock. So an
68
+ * MRP schedule that outlives this budget turns a recoverable block loss into a dead transfer.
69
+ */
70
+ const BDX_PEER_RESPONSE_BUDGET = Seconds(30);
71
+
72
+ /** Leaves the final attempt time to reach the peer while the budget is still open. */
73
+ const BDX_RESPONSE_BUDGET_MARGIN = Millis(100);
74
+
75
+ /** Retransmission interval that still fits every attempt inside {@link BDX_PEER_RESPONSE_BUDGET}. */
76
+ const BDX_MAX_RETRANSMISSION_INTERVAL = Millis(
77
+ BDX_PEER_RESPONSE_BUDGET / (MRP.MAX_TRANSMISSIONS - 1) - BDX_RESPONSE_BUDGET_MARGIN,
78
+ );
79
+
62
80
  export type ExchangeLogContext = Record<string, unknown>;
63
81
 
64
82
  export interface ExchangeSendOptions {
@@ -80,8 +98,14 @@ export interface ExchangeSendOptions {
80
98
  */
81
99
  expectedProcessingTime?: Duration;
82
100
 
83
- /** Allows to specify if the send message requires to be acknowledged by the receiver or not. */
84
- requiresAck?: boolean;
101
+ /**
102
+ * Sends the message without requesting acknowledgement, for a message whose delivery we deliberately do not
103
+ * track — a Busy or CloseSession status report, or an ICD check-in on a session we are about to drop.
104
+ *
105
+ * MRP requests an ack for every message on an MRP session, so there is nothing to opt into: only suppression is
106
+ * expressible.
107
+ */
108
+ suppressAck?: true;
85
109
 
86
110
  /**
87
111
  * Disables the MRP logic which means that no retransmissions are done and receiving an ack is not awaited.
@@ -241,6 +265,7 @@ export class MessageExchange {
241
265
  #messageSendCounter = 0;
242
266
  #messageReceivedCounter = 0;
243
267
  #retransmissionTimer?: Timer;
268
+ #lastTransmissionAt?: Timestamp;
244
269
  #lastActive = Time.nowMs;
245
270
 
246
271
  constructor(config: MessageExchange.Config) {
@@ -435,7 +460,11 @@ export class MessageExchange {
435
460
  if (duplicate) {
436
461
  // Received a message retransmission, but the reply is not ready yet, ignoring
437
462
  if (requiresAck) {
438
- await this.sendStandaloneAckForMessage(message);
463
+ // A retransmission of our pending message acknowledges this duplicate by definition, so it stands in
464
+ // for the standalone ack. Only a transmission that actually reached the channel counts.
465
+ if (!this.#kickRetransmissionForLiveness(message)) {
466
+ await this.sendStandaloneAckForMessage(message);
467
+ }
439
468
  }
440
469
  return;
441
470
  }
@@ -444,6 +473,7 @@ export class MessageExchange {
444
473
  // Resending the previous reply message which contains the ack
445
474
  using _acking = this.join("resending ack");
446
475
  this.#messageSendCounter++;
476
+ this.#lastTransmissionAt = Time.nowMs;
447
477
  await this.channel.send(this.#sentMessageToAck, { exchange: this, addressOverride: this.#addressOverride });
448
478
  return;
449
479
  }
@@ -491,6 +521,76 @@ export class MessageExchange {
491
521
  this.#messagesQueue.write(message);
492
522
  }
493
523
 
524
+ /**
525
+ * Retransmits the pending message early when a duplicate proves both that the peer is awake right now and that our
526
+ * pending reply never reached it. A backoff sized for an idle peer would otherwise spend a window we know is open,
527
+ * which for a sleepy peer mid-transfer may not reopen for a long time.
528
+ *
529
+ * This departs from the retransmission schedule of {@link MatterSpecification.v16.Core} § 4.12.2.1 and, when it
530
+ * transmits, from the standalone ack of § 4.12.5.2.2 — the retransmission carries that ack instead. Both are
531
+ * confined to BDX, where transfers are long-running and lock-step makes the predicate unambiguous.
532
+ *
533
+ * Returns true only if a transmission actually reached the channel, in which case the caller must not send a
534
+ * standalone ack: the transmitted message already acknowledges the duplicate that triggered it.
535
+ *
536
+ * {@link BDX_MAX_RETRANSMISSION_INTERVAL} currently bounds what this can save. It earns its keep again for any
537
+ * peer whose response budget is larger than CHIP's, or if that cap is ever relaxed.
538
+ */
539
+ #kickRetransmissionForLiveness(duplicate: Message) {
540
+ if (this.#protocolId !== BDX_PROTOCOL_ID || this.considerClosed) {
541
+ return false;
542
+ }
543
+
544
+ // A running timer also tells us no send is in flight, so we cannot transmit on top of one
545
+ const pending = this.#sentMessageToAck;
546
+ const timer = this.#retransmissionTimer;
547
+ if (pending === undefined || timer === undefined || !timer.isRunning) {
548
+ return false;
549
+ }
550
+
551
+ if (duplicate.packetHeader.messageId !== pending.payloadHeader.ackedMessageId) {
552
+ return false;
553
+ }
554
+
555
+ // Taking the last slot early would leave the scheduled attempt with none, and MRP would report the peer as
556
+ // unresponsive without ever having retried inside the window we are trying to hit
557
+ if (this.#retransmissionCounter + 1 >= (this.#sendOptions.maxRetransmissions ?? MRP.MAX_TRANSMISSIONS)) {
558
+ return false;
559
+ }
560
+
561
+ const { activeInterval, activeThreshold } = this.session.parameters;
562
+
563
+ // Never transmit faster than the cadence the schedule itself uses, medium margin included
564
+ const cadence = Millis(activeInterval + this.#sendAdditionalDelay);
565
+ const elapsed = this.#lastTransmissionAt === undefined ? Forever : Timestamp.delta(this.#lastTransmissionAt);
566
+ if (elapsed < cadence) {
567
+ return false;
568
+ }
569
+
570
+ // Only worth a slot if the peer would fall asleep again before the scheduled attempt; a wait it can stay
571
+ // awake through costs nothing to honour
572
+ const remaining = Millis(Math.max(0, timer.interval - elapsed));
573
+ if (remaining <= activeThreshold) {
574
+ return false;
575
+ }
576
+
577
+ logger.debug(
578
+ this.via,
579
+ "Kicking retransmission because peer proved liveness",
580
+ Diagnostic.dict({
581
+ waited: Duration.format(elapsed),
582
+ saved: Duration.format(remaining),
583
+ "retrans#": this.#retransmissionCounter,
584
+ }),
585
+ );
586
+
587
+ timer.stop();
588
+ return this.#retransmitMessage(
589
+ pending,
590
+ this.#sendOptions.expectedProcessingTime ?? MRP.DEFAULT_EXPECTED_PROCESSING_TIME,
591
+ );
592
+ }
593
+
494
594
  async send(messageType: number, payload: Bytes, options: ExchangeSendOptions = {}) {
495
595
  if (this.#closeCause) {
496
596
  throw new ClosedError("Exchange is closed", { cause: this.#closeCause });
@@ -541,22 +641,14 @@ export class MessageExchange {
541
641
  }
542
642
  }
543
643
 
544
- let { requiresAck } = this.#sendOptions;
545
- if (requiresAck && !(this.session.usesMrp || (this.session as NodeSession).isPeerLost)) {
546
- requiresAck = false;
547
- }
644
+ const requiresAck = !this.#sendOptions.suppressAck && this.session.usesMrp && !isStandaloneAck;
548
645
 
549
646
  // StandaloneAck and StatusReport are SecureChannel messages and are always sent via SECURE_CHANNEL_PROTOCOL_ID
550
647
  // regardless of the exchange's protocol (Matter spec 4.10).
551
648
  const isStatusReport = messageType === SecureMessageType.StatusReport;
552
649
  const protocolId = isStandaloneAck || isStatusReport ? SECURE_CHANNEL_PROTOCOL_ID : this.#protocolId;
553
- if (isStandaloneAck) {
554
- if (!this.session.usesMrp) {
555
- return;
556
- }
557
- if (requiresAck) {
558
- throw new MatterFlowError("A standalone ack may not require acknowledgement");
559
- }
650
+ if (isStandaloneAck && !this.session.usesMrp) {
651
+ return;
560
652
  }
561
653
  if (this.#sentMessageToAck !== undefined && !isStandaloneAck) {
562
654
  throw new MatterFlowError(
@@ -633,7 +725,7 @@ export class MessageExchange {
633
725
  protocolId,
634
726
  messageType,
635
727
  isInitiatorMessage: this.isInitiator,
636
- requiresAck: requiresAck ?? (this.session.usesMrp && !isStandaloneAck),
728
+ requiresAck,
637
729
  ackedMessageId,
638
730
  hasSecuredExtension: false,
639
731
  },
@@ -670,6 +762,10 @@ export class MessageExchange {
670
762
  return;
671
763
  }
672
764
 
765
+ if (!isStandaloneAck) {
766
+ this.#lastTransmissionAt = Time.nowMs;
767
+ }
768
+
673
769
  if (ackPromise !== undefined) {
674
770
  this.#retransmissionCounter = 0;
675
771
  this.#retransmissionTimer?.start();
@@ -759,7 +855,18 @@ export class MessageExchange {
759
855
  await this.#send(SecureMessageType.StandaloneAck, new Uint8Array(0), messageId);
760
856
  }
761
857
 
858
+ /**
859
+ * Transmits the pending message again. Returns whether a transmission was actually started: the budget may be
860
+ * spent, in which case this only schedules the final wait, and a caller that treats "attempted" as "sent" would
861
+ * drop whatever the transmission was meant to convey.
862
+ */
762
863
  #retransmitMessage(message: Message, expectedProcessingTime?: Duration) {
864
+ // The message may have been acked while an earlier transmission of it was still in flight. Its timers must
865
+ // neither outlive it nor spend the retransmission budget of whatever is pending now.
866
+ if (this.#sentMessageToAck !== message) {
867
+ return false;
868
+ }
869
+
763
870
  this.#retransmissionCounter++;
764
871
  this.#totalRetransmissionCounter++;
765
872
  if (
@@ -791,7 +898,7 @@ export class MessageExchange {
791
898
  finalWaitTime,
792
899
  () => this.#retransmitMessage(message),
793
900
  ).start();
794
- return;
901
+ return false;
795
902
  }
796
903
  }
797
904
 
@@ -807,11 +914,12 @@ export class MessageExchange {
807
914
  // TODO await
808
915
  this.#close().catch(error => logger.warn("Error closing exchange", error));
809
916
  }
810
- return;
917
+ return false;
811
918
  }
812
919
 
813
920
  this.#messageSendCounter++;
814
921
  this.#notifyActivity(false);
922
+ this.#lastTransmissionAt = Time.nowMs;
815
923
 
816
924
  this.context.retry(this.#retransmissionCounter);
817
925
  const resubmissionBackoffTime = this.#mrpResubmissionBackOffTime;
@@ -837,9 +945,17 @@ export class MessageExchange {
837
945
  this.#initializeResubmission(message, resubmissionBackoffTime, expectedProcessingTime);
838
946
  }
839
947
  });
948
+
949
+ return true;
840
950
  }
841
951
 
842
952
  #initializeResubmission(message: Message, resubmissionBackoffTime: Duration, expectedProcessingTimeMs?: Duration) {
953
+ // An ack may have landed while the transmission we are following up on was in flight; arming a timer for a
954
+ // message that is no longer pending leaves it running unreferenced, so nothing can ever stop it
955
+ if (this.#sentMessageToAck !== message) {
956
+ return;
957
+ }
958
+
843
959
  this.#retransmissionTimer = Time.getTimer("Message retransmission", resubmissionBackoffTime, () =>
844
960
  this.#retransmitMessage(message, expectedProcessingTimeMs),
845
961
  ).start();
@@ -1044,9 +1160,13 @@ export class MessageExchange {
1044
1160
  return this.#backOffFor(this.#retransmissionCounter);
1045
1161
  }
1046
1162
 
1047
- /** Amplified backoff addition applied to our sends: the larger of our own and the peer's network-profile delay. */
1163
+ /**
1164
+ * Amplified backoff addition applied to our sends: the larger of our own and the peer's network-profile delay for
1165
+ * this exchange's traffic class.
1166
+ */
1048
1167
  get #sendAdditionalDelay() {
1049
- return Duration.max(this.#context.localAdditionalMrpDelay, this.#peerAdditionalMrpDelay ?? Millis(0));
1168
+ const peerMargin = this.#peerAdditionalMrpDelay ?? this.session.peerAdditionalMrpDelay;
1169
+ return Duration.max(this.#context.localAdditionalMrpDelay, peerMargin ?? Millis(0));
1050
1170
  }
1051
1171
 
1052
1172
  /**
@@ -1054,6 +1174,10 @@ export class MessageExchange {
1054
1174
  *
1055
1175
  * The cap never drops below the peer's idle interval: a peer whose idle base backoff already exceeds
1056
1176
  * maxRetransmissionTime must not be retried faster than its own idle cadence.
1177
+ *
1178
+ * BDX overrides even that floor. Its schedule has to fit the peer's response budget, and the idle cadence does not
1179
+ * apply mid-transfer: per {@link MatterSpecification.v16.Core} § 4.12.2.1 a peer awaiting an acknowledgement is in
1180
+ * active mode, because it holds an open exchange.
1057
1181
  */
1058
1182
  #backOffFor(retransmissionCount: number) {
1059
1183
  let backOff = this.channel.getMrpResubmissionBackOffTime(
@@ -1066,10 +1190,11 @@ export class MessageExchange {
1066
1190
  if (this.#sendOptions.initialRetransmissionTime !== undefined) {
1067
1191
  backOff = Millis(backOff + this.#sendOptions.initialRetransmissionTime);
1068
1192
  }
1069
- const cap = Duration.max(
1070
- this.#sendOptions.maxRetransmissionTime ?? Forever,
1071
- this.session.parameters.idleInterval ?? Instant,
1072
- );
1193
+ const idleInterval = this.session.parameters.idleInterval;
1194
+ let cap = Duration.max(this.#sendOptions.maxRetransmissionTime ?? Forever, idleInterval);
1195
+ if (this.#protocolId === BDX_PROTOCOL_ID) {
1196
+ cap = Duration.min(cap, BDX_MAX_RETRANSMISSION_INTERVAL);
1197
+ }
1073
1198
  return Duration.min(backOff, cap);
1074
1199
  }
1075
1200
 
@@ -1111,6 +1236,9 @@ export namespace MessageExchange {
1111
1236
  /**
1112
1237
  * Additive MRP retransmission margin for the peer's network medium. Sourced independently of
1113
1238
  * {@link network} so concurrency overrides cannot strip the medium-correct margin (e.g. thread's).
1239
+ *
1240
+ * Overrides {@link Session.peerAdditionalMrpDelay}, which supplies the margin for exchanges created without
1241
+ * this option (notably peer-initiated ones).
1114
1242
  */
1115
1243
  peerAdditionalMrpDelay?: Duration;
1116
1244
 
@@ -148,13 +148,13 @@ export class SecureChannelMessenger {
148
148
  GeneralStatusCode.Busy,
149
149
  SecureChannelStatusCode.Busy,
150
150
  abort,
151
- false,
151
+ true,
152
152
  writer.toByteArray(),
153
153
  );
154
154
  }
155
155
 
156
156
  sendCloseSession(abort?: AbortSignal) {
157
- return this.#sendStatusReport(GeneralStatusCode.Success, SecureChannelStatusCode.CloseSession, abort, false);
157
+ return this.#sendStatusReport(GeneralStatusCode.Success, SecureChannelStatusCode.CloseSession, abort, true);
158
158
  }
159
159
 
160
160
  get channelName() {
@@ -173,7 +173,7 @@ export class SecureChannelMessenger {
173
173
  generalStatus: GeneralStatusCode,
174
174
  protocolStatus: SecureChannelStatusCode,
175
175
  abort?: AbortSignal,
176
- requiresAck?: boolean,
176
+ suppressAck?: true,
177
177
  protocolData?: Bytes,
178
178
  ) {
179
179
  await this.exchange.send(
@@ -184,7 +184,7 @@ export class SecureChannelMessenger {
184
184
  protocolData,
185
185
  }),
186
186
  {
187
- requiresAck,
187
+ suppressAck,
188
188
  logContext: {
189
189
  generalStatus: GeneralStatusCode[generalStatus] ?? Diagnostic.hex(generalStatus),
190
190
  protocolStatus: SecureChannelStatusCode[protocolStatus] ?? Diagnostic.hex(protocolStatus),
@@ -328,8 +328,7 @@ export class NodeSession extends SecureSession {
328
328
 
329
329
  override addExchange(exchange: MessageExchange) {
330
330
  super.addExchange(exchange);
331
- exchange.closed.on(async () => {
332
- this.exchanges.delete(exchange);
331
+ exchange.closed.once(async () => {
333
332
  if (this.deferredClose && !this.hasActiveExchanges) {
334
333
  this.deferredClose = false;
335
334
  await this.close();
@@ -55,7 +55,9 @@ export abstract class Session {
55
55
  activeTimestamp: Timestamp = 0;
56
56
  abstract type: SessionType;
57
57
 
58
+ #peerAdditionalMrpDelay?: () => Duration | undefined;
58
59
  #closing = ObservableValue();
60
+ #ended = false;
59
61
  #gracefulClose = AsyncObservable<[]>();
60
62
  readonly #exchanges = new Set<MessageExchange>();
61
63
  protected deferredClose = false;
@@ -114,6 +116,7 @@ export abstract class Session {
114
116
 
115
117
  addExchange(exchange: MessageExchange) {
116
118
  this.#exchanges.add(exchange);
119
+ exchange.closed.once(() => this.deleteExchange(exchange));
117
120
  }
118
121
 
119
122
  deleteExchange(exchange: MessageExchange) {
@@ -199,6 +202,23 @@ export abstract class Session {
199
202
  };
200
203
  }
201
204
 
205
+ /**
206
+ * Additive MRP retransmission margin for the peer's network medium, or undefined if the medium is unknown.
207
+ *
208
+ * Applies to every exchange on the session, including those the peer initiates.
209
+ */
210
+ get peerAdditionalMrpDelay(): Duration | undefined {
211
+ return this.#peerAdditionalMrpDelay?.();
212
+ }
213
+
214
+ /**
215
+ * Installs the resolver for {@link peerAdditionalMrpDelay}. A resolver rather than a value because the peer's
216
+ * medium often becomes known (or changes, e.g. a new thread channel) only after the session exists.
217
+ */
218
+ set peerAdditionalMrpDelayResolver(resolver: () => Duration | undefined) {
219
+ this.#peerAdditionalMrpDelay = resolver;
220
+ }
221
+
202
222
  /**
203
223
  * Allows updating the Session timing parameters based on received information from the peer during PASE/CASE initialization
204
224
  */
@@ -276,7 +296,7 @@ export abstract class Session {
276
296
  if (!context.keepSubscriptions) {
277
297
  await this.closeSubscriptions(false, context.currentExchange);
278
298
  }
279
- for (const exchange of this.#exchanges) {
299
+ for (const exchange of [...this.#exchanges]) {
280
300
  if (exchange === context.currentExchange) {
281
301
  this.deferredClose = true;
282
302
  continue;
@@ -324,7 +344,7 @@ export abstract class Session {
324
344
  detachChannel() {
325
345
  const channel = this.#channel;
326
346
  this.#channel = undefined;
327
- logger.info(this.via, "Channel detached");
347
+ logger.debug(this.via, "Channel detached");
328
348
  return channel;
329
349
  }
330
350
 
@@ -334,6 +354,10 @@ export abstract class Session {
334
354
  if (this.#channel) {
335
355
  await this.#channel.close();
336
356
  this.#channel = undefined;
357
+ }
358
+
359
+ if (!this.#ended) {
360
+ this.#ended = true;
337
361
  logger.info(this.via, "Session ended");
338
362
  }
339
363
  }
@@ -94,6 +94,7 @@ export class CaseServer implements ProtocolHandler {
94
94
  }
95
95
  } finally {
96
96
  // Destroy the unsecure session used to establish the secure Case session
97
+ await exchange.session.detachChannel()?.release();
97
98
  await exchange.session.initiateClose();
98
99
  }
99
100
  }
@@ -113,7 +113,7 @@ export class PaseServer implements ProtocolHandler {
113
113
  this.#pairingTimer = undefined;
114
114
  this.#pairingMessenger = undefined;
115
115
  // Detach and Destroy the unsecure session used to establish the Pase session
116
- exchange.session.detachChannel();
116
+ await exchange.session.detachChannel()?.release();
117
117
  await exchange.session.initiateClose();
118
118
  }
119
119
  }