From 9186e08911709b08a9cfdc5aca055237f37b14a0 Mon Sep 17 00:00:00 2001 From: Jeff Tung <100387939+jtung-apple@users.noreply.github.com> Date: Mon, 17 Feb 2025 15:03:38 -0800 Subject: [PATCH 1/2] [Darwin] MTRDevice resub logic should reset backoff as needed --- src/darwin/Framework/CHIP/MTRDevice_Concrete.mm | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm b/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm index 0cb81e6b3f56dc..cf21168768d080 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm +++ b/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm @@ -1031,8 +1031,9 @@ - (void)_triggerResubscribeWithReason:(NSString *)reason nodeLikelyReachable:(BO // in fact be reachable yet; we won't know until we have managed to // establish a CASE session. And at that point, our subscription will // trigger the state change as needed. + BOOL shouldReattemptSubscription = NO; if (self.reattemptingSubscription) { - [self _reattemptSubscriptionNowIfNeededWithReason:reason]; + shouldReattemptSubscription = YES; } else { readClientToResubscribe = self.matterCPPObjectsHolder.readClient; subscriptionCallback = self.matterCPPObjectsHolder.subscriptionCallback; @@ -1048,9 +1049,15 @@ - (void)_triggerResubscribeWithReason:(NSString *)reason nodeLikelyReachable:(BO subscriptionCallback->ResetResubscriptionBackoff(); } readClientToResubscribe->TriggerResubscribeIfScheduled(reason.UTF8String); - } else if (_internalDeviceState == MTRInternalDeviceStateSubscribing && nodeLikelyReachable) { + } else if (((_internalDeviceState == MTRInternalDeviceStateSubscribing) || shouldReattemptSubscription) && nodeLikelyReachable) { // If we have reason to suspect that the node is now reachable and we haven't established a // CASE session yet, let's consider it to be stalled and invalidate the pairing session. + + // Reset back off for framework resubscription + os_unfair_lock_lock(&self->_lock); + [self _setLastSubscriptionAttemptWait:0]; + os_unfair_lock_unlock(&self->_lock); + mtr_weakify(self); [self._concreteController asyncGetCommissionerOnMatterQueue:^(Controller::DeviceCommissioner * commissioner) { mtr_strongify(self); @@ -1061,6 +1068,11 @@ - (void)_triggerResubscribeWithReason:(NSString *)reason nodeLikelyReachable:(BO caseSessionMgr->ReleaseSession(commissioner->GetPeerScopedId(self->_nodeID.unsignedLongLongValue)); } errorHandler:nil /* not much we can do */]; } + + // Reattempt subscription after the above ReleaseSession call to avoid churn + if (shouldReattemptSubscription) { + [self _reattemptSubscriptionNowIfNeededWithReason:reason]; + } } // Return YES if we are in a state where, apart from communication issues with From 14a0c6fc64c5bf241d3da40ec9ca9934571b2367 Mon Sep 17 00:00:00 2001 From: Jeff Tung <100387939+jtung-apple@users.noreply.github.com> Date: Mon, 17 Feb 2025 15:30:35 -0800 Subject: [PATCH 2/2] Improved code comment, per PR review --- src/darwin/Framework/CHIP/MTRDevice_Concrete.mm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm b/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm index cf21168768d080..b7feddc74c8262 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm +++ b/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm @@ -1069,7 +1069,8 @@ - (void)_triggerResubscribeWithReason:(NSString *)reason nodeLikelyReachable:(BO } errorHandler:nil /* not much we can do */]; } - // Reattempt subscription after the above ReleaseSession call to avoid churn + // The subscription reattempt here eventually asyncs onto the matter queue for session, + // and should be called after the above ReleaseSession call, to avoid churn. if (shouldReattemptSubscription) { [self _reattemptSubscriptionNowIfNeededWithReason:reason]; }