Skip to content

Commit 968a0e6

Browse files
authored
apply changes from PR #34797 to to concrete class (#34856)
1 parent 599fd16 commit 968a0e6

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/darwin/Framework/CHIP/MTRDevice_Concrete.mm

+22-11
Original file line numberDiff line numberDiff line change
@@ -1101,23 +1101,21 @@ - (void)_handleSubscriptionEstablished
11011101
{
11021102
os_unfair_lock_lock(&self->_lock);
11031103

1104-
// We have completed the subscription work - remove from the subscription pool.
1105-
[self _clearSubscriptionPoolWork];
1106-
1107-
// reset subscription attempt wait time when subscription succeeds
1108-
_lastSubscriptionAttemptWait = 0;
1109-
if (HadSubscriptionEstablishedOnce(_internalDeviceState)) {
1110-
[self _changeInternalState:MTRInternalDeviceStateLaterSubscriptionEstablished];
1111-
} else {
1112-
MATTER_LOG_METRIC_END(kMetricMTRDeviceInitialSubscriptionSetup, CHIP_NO_ERROR);
1113-
[self _changeInternalState:MTRInternalDeviceStateInitialSubscriptionEstablished];
1104+
// If subscription had reset since this handler was scheduled, do not execute "established" logic below
1105+
if (!HaveSubscriptionEstablishedRightNow(_internalDeviceState)) {
1106+
MTR_LOG("%@ _handleSubscriptionEstablished run with internal state %lu - skipping subscription establishment logic", self, static_cast<unsigned long>(_internalDeviceState));
1107+
return;
11141108
}
11151109

1116-
[self _changeState:MTRDeviceStateReachable];
1110+
// We have completed the subscription work - remove from the subscription pool.
1111+
[self _clearSubscriptionPoolWork];
11171112

11181113
// No need to monitor connectivity after subscription establishment
11191114
[self _stopConnectivityMonitoring];
11201115

1116+
// reset subscription attempt wait time when subscription succeeds
1117+
_lastSubscriptionAttemptWait = 0;
1118+
11211119
auto initialSubscribeStart = _initialSubscribeStart;
11221120
// We no longer need to track subscribe latency for this device.
11231121
_initialSubscribeStart = nil;
@@ -2384,6 +2382,19 @@ - (void)_setupSubscriptionWithReason:(NSString *)reason
23842382
},
23852383
^(void) {
23862384
MTR_LOG("%@ got subscription established", self);
2385+
std::lock_guard lock(self->_lock);
2386+
2387+
// First synchronously change state
2388+
if (HadSubscriptionEstablishedOnce(self->_internalDeviceState)) {
2389+
[self _changeInternalState:MTRInternalDeviceStateLaterSubscriptionEstablished];
2390+
} else {
2391+
MATTER_LOG_METRIC_END(kMetricMTRDeviceInitialSubscriptionSetup, CHIP_NO_ERROR);
2392+
[self _changeInternalState:MTRInternalDeviceStateInitialSubscriptionEstablished];
2393+
}
2394+
2395+
[self _changeState:MTRDeviceStateReachable];
2396+
2397+
// Then async work that shouldn't be performed on the matter queue
23872398
dispatch_async(self.queue, ^{
23882399
// OnSubscriptionEstablished
23892400
[self _handleSubscriptionEstablished];

0 commit comments

Comments
 (0)