Skip to content

Commit

Permalink
[Darwin] MTRDevice subscription estalished handler should change stat…
Browse files Browse the repository at this point in the history
…e before async
  • Loading branch information
jtung-apple committed Aug 5, 2024
1 parent b9c192b commit 413b47b
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/darwin/Framework/CHIP/MTRDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1193,23 +1193,20 @@ - (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 ececute "established" logic below
if (!HaveSubscriptionEstablishedRightNow(_internalDeviceState)) {
MTR_LOG("%@ _handleSubscriptionEstablished run with internal state %lu - skipping subscription establishment logic", self, static_cast<unsigned long>(_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];

_lastSubscriptionAttemptWait = 0;

auto initialSubscribeStart = _initialSubscribeStart;
// We no longer need to track subscribe latency for this device.
_initialSubscribeStart = nil;
Expand Down Expand Up @@ -2476,6 +2473,21 @@ - (void)_setupSubscriptionWithReason:(NSString *)reason
},
^(void) {
MTR_LOG("%@ got subscription established", self);
std::lock_guard lock(self->_lock);

// First synchronously change state

// reset subscription attempt wait time when subscription succeeds
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];
Expand Down

0 comments on commit 413b47b

Please sign in to comment.