From ea0cd59c1b33028e6985e6063dbd83171c58d5af Mon Sep 17 00:00:00 2001 From: Kiel Oleson Date: Wed, 7 Aug 2024 15:13:37 -0700 Subject: [PATCH] apply changes from PR #34797 to to concrete class (#34856) --- .../Framework/CHIP/MTRDevice_Concrete.mm | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm b/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm index c8b00ac622e311..a90b49d52b3139 100644 --- a/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm +++ b/src/darwin/Framework/CHIP/MTRDevice_Concrete.mm @@ -1101,23 +1101,21 @@ - (void)_handleSubscriptionEstablished { os_unfair_lock_lock(&self->_lock); - // We have completed the subscription work - remove from the subscription pool. - [self _clearSubscriptionPoolWork]; - - // reset subscription attempt wait time when subscription succeeds - _lastSubscriptionAttemptWait = 0; - if (HadSubscriptionEstablishedOnce(_internalDeviceState)) { - [self _changeInternalState:MTRInternalDeviceStateLaterSubscriptionEstablished]; - } else { - MATTER_LOG_METRIC_END(kMetricMTRDeviceInitialSubscriptionSetup, CHIP_NO_ERROR); - [self _changeInternalState:MTRInternalDeviceStateInitialSubscriptionEstablished]; + // If subscription had reset since this handler was scheduled, do not execute "established" logic below + if (!HaveSubscriptionEstablishedRightNow(_internalDeviceState)) { + MTR_LOG("%@ _handleSubscriptionEstablished run with internal state %lu - skipping subscription establishment logic", self, static_cast(_internalDeviceState)); + return; } - [self _changeState:MTRDeviceStateReachable]; + // We have completed the subscription work - remove from the subscription pool. + [self _clearSubscriptionPoolWork]; // No need to monitor connectivity after subscription establishment [self _stopConnectivityMonitoring]; + // reset subscription attempt wait time when subscription succeeds + _lastSubscriptionAttemptWait = 0; + auto initialSubscribeStart = _initialSubscribeStart; // We no longer need to track subscribe latency for this device. _initialSubscribeStart = nil; @@ -2384,6 +2382,19 @@ - (void)_setupSubscriptionWithReason:(NSString *)reason }, ^(void) { MTR_LOG("%@ got subscription established", self); + std::lock_guard lock(self->_lock); + + // First synchronously change state + if (HadSubscriptionEstablishedOnce(self->_internalDeviceState)) { + [self _changeInternalState:MTRInternalDeviceStateLaterSubscriptionEstablished]; + } else { + MATTER_LOG_METRIC_END(kMetricMTRDeviceInitialSubscriptionSetup, CHIP_NO_ERROR); + [self _changeInternalState:MTRInternalDeviceStateInitialSubscriptionEstablished]; + } + + [self _changeState:MTRDeviceStateReachable]; + + // Then async work that shouldn't be performed on the matter queue dispatch_async(self.queue, ^{ // OnSubscriptionEstablished [self _handleSubscriptionEstablished];