Skip to content

Commit

Permalink
Make sure the MTRDevice lock is held when we touch _weakDelegate. (pr…
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple authored Dec 19, 2023
1 parent b38d140 commit 5cd1c52
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/darwin/Framework/CHIP/MTRDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,11 @@ - (void)nodeMayBeAdvertisingOperational
// Return YES if there's a valid delegate AND subscription is expected to report value
- (BOOL)_subscriptionAbleToReport
{
return (_weakDelegate.strongObject) && (_state == MTRDeviceStateReachable);
os_unfair_lock_lock(&self->_lock);
id<MTRDeviceDelegate> delegate = _weakDelegate.strongObject;
auto state = _state;
os_unfair_lock_unlock(&self->_lock);
return (delegate != nil) && (state == MTRDeviceStateReachable);
}

// assume lock is held
Expand Down Expand Up @@ -634,6 +638,8 @@ - (void)_handleEventReport:(NSArray<NSDictionary<NSString *, id> *> *)eventRepor
// assume lock is held
- (void)_setupSubscription
{
os_unfair_lock_assert_owner(&self->_lock);

#ifdef DEBUG
id delegate = _weakDelegate.strongObject;
Optional<System::Clock::Seconds32> maxIntervalOverride;
Expand All @@ -645,8 +651,6 @@ - (void)_setupSubscription
}
#endif

os_unfair_lock_assert_owner(&self->_lock);

// for now just subscribe once
if (_subscriptionActive) {
return;
Expand Down Expand Up @@ -1088,7 +1092,9 @@ - (void)writeAttributeWithEndpointID:(NSNumber *)endpointID

BOOL useValueAsExpectedValue = YES;
#ifdef DEBUG
os_unfair_lock_lock(&self->_lock);
id delegate = _weakDelegate.strongObject;
os_unfair_lock_unlock(&self->_lock);
if ([delegate respondsToSelector:@selector(unitTestShouldSkipExpectedValuesForWrite:)]) {
useValueAsExpectedValue = ![delegate unitTestShouldSkipExpectedValuesForWrite:self];
}
Expand Down

0 comments on commit 5cd1c52

Please sign in to comment.