Skip to content

Commit 413b47b

Browse files
committed
[Darwin] MTRDevice subscription estalished handler should change state before async
1 parent b9c192b commit 413b47b

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/darwin/Framework/CHIP/MTRDevice.mm

+23-11
Original file line numberDiff line numberDiff line change
@@ -1193,23 +1193,20 @@ - (void)_handleSubscriptionEstablished
11931193
{
11941194
os_unfair_lock_lock(&self->_lock);
11951195

1196-
// We have completed the subscription work - remove from the subscription pool.
1197-
[self _clearSubscriptionPoolWork];
1198-
1199-
// reset subscription attempt wait time when subscription succeeds
1200-
_lastSubscriptionAttemptWait = 0;
1201-
if (HadSubscriptionEstablishedOnce(_internalDeviceState)) {
1202-
[self _changeInternalState:MTRInternalDeviceStateLaterSubscriptionEstablished];
1203-
} else {
1204-
MATTER_LOG_METRIC_END(kMetricMTRDeviceInitialSubscriptionSetup, CHIP_NO_ERROR);
1205-
[self _changeInternalState:MTRInternalDeviceStateInitialSubscriptionEstablished];
1196+
// If subscription had reset since this handler was scheduled, do not ececute "established" logic below
1197+
if (!HaveSubscriptionEstablishedRightNow(_internalDeviceState)) {
1198+
MTR_LOG("%@ _handleSubscriptionEstablished run with internal state %lu - skipping subscription establishment logic", self, static_cast<unsigned long>(_internalDeviceState));
1199+
return;
12061200
}
12071201

1208-
[self _changeState:MTRDeviceStateReachable];
1202+
// We have completed the subscription work - remove from the subscription pool.
1203+
[self _clearSubscriptionPoolWork];
12091204

12101205
// No need to monitor connectivity after subscription establishment
12111206
[self _stopConnectivityMonitoring];
12121207

1208+
_lastSubscriptionAttemptWait = 0;
1209+
12131210
auto initialSubscribeStart = _initialSubscribeStart;
12141211
// We no longer need to track subscribe latency for this device.
12151212
_initialSubscribeStart = nil;
@@ -2476,6 +2473,21 @@ - (void)_setupSubscriptionWithReason:(NSString *)reason
24762473
},
24772474
^(void) {
24782475
MTR_LOG("%@ got subscription established", self);
2476+
std::lock_guard lock(self->_lock);
2477+
2478+
// First synchronously change state
2479+
2480+
// reset subscription attempt wait time when subscription succeeds
2481+
if (HadSubscriptionEstablishedOnce(self->_internalDeviceState)) {
2482+
[self _changeInternalState:MTRInternalDeviceStateLaterSubscriptionEstablished];
2483+
} else {
2484+
MATTER_LOG_METRIC_END(kMetricMTRDeviceInitialSubscriptionSetup, CHIP_NO_ERROR);
2485+
[self _changeInternalState:MTRInternalDeviceStateInitialSubscriptionEstablished];
2486+
}
2487+
2488+
[self _changeState:MTRDeviceStateReachable];
2489+
2490+
// Then async work that shouldn't be performed on the matter queue
24792491
dispatch_async(self.queue, ^{
24802492
// OnSubscriptionEstablished
24812493
[self _handleSubscriptionEstablished];

0 commit comments

Comments
 (0)