Skip to content

Commit

Permalink
[Darwin] MTRDevice_XPC delegate callbacks need to hold lock before _c…
Browse files Browse the repository at this point in the history
…allDelegatesWithBlock
  • Loading branch information
jtung-apple committed Aug 20, 2024
1 parent 88b6644 commit 69a8eb9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/darwin/Framework/CHIP/MTRDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,12 @@ - (BOOL)_callDelegatesWithBlock:(void (^)(id<MTRDeviceDelegate> delegate))block
return (delegatesCalled > 0);
}

- (BOOL)_lockAndCallDelegatesWithBlock:(void (^)(id<MTRDeviceDelegate> delegate))block
{
std::lock_guard lock(self->_lock);
return [self _callDelegatesWithBlock:block];
}

#ifdef DEBUG
// Only used for unit test purposes - normal delegate should not expect or handle being called back synchronously
// Returns YES if a delegate is called
Expand Down
3 changes: 2 additions & 1 deletion src/darwin/Framework/CHIP/MTRDevice_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ MTR_TESTABLE
// false-positives, for example due to compressed fabric id collisions.
- (void)nodeMayBeAdvertisingOperational;

- (BOOL)_callDelegatesWithBlock:(void (^)(id<MTRDeviceDelegate> delegate))block;
// Called by MTRDevice_XPC to forward delegate callbacks
- (BOOL)_lockAndCallDelegatesWithBlock:(void (^)(id<MTRDeviceDelegate> delegate))block;

/**
* Like the public invokeCommandWithEndpointID but:
Expand Down
12 changes: 6 additions & 6 deletions src/darwin/Framework/CHIP/MTRDevice_XPC.mm
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,23 @@ @implementation MTRDevice_XPC
- (oneway void)device:(NSNumber *)nodeID stateChanged:(MTRDeviceState)state
{
MTR_LOG("%s", __PRETTY_FUNCTION__);
[self _callDelegatesWithBlock:^(id<MTRDeviceDelegate> delegate) {
[self _lockAndCallDelegatesWithBlock:^(id<MTRDeviceDelegate> delegate) {
[delegate device:self stateChanged:state];
}];
}

- (oneway void)device:(NSNumber *)nodeID receivedAttributeReport:(NSArray<NSDictionary<NSString *, id> *> *)attributeReport
{
MTR_LOG("%s", __PRETTY_FUNCTION__);
[self _callDelegatesWithBlock:^(id<MTRDeviceDelegate> delegate) {
[self _lockAndCallDelegatesWithBlock:^(id<MTRDeviceDelegate> delegate) {
[delegate device:self receivedAttributeReport:attributeReport];
}];
}

- (oneway void)device:(NSNumber *)nodeID receivedEventReport:(NSArray<NSDictionary<NSString *, id> *> *)eventReport
{
MTR_LOG("%s", __PRETTY_FUNCTION__);
[self _callDelegatesWithBlock:^(id<MTRDeviceDelegate> delegate) {
[self _lockAndCallDelegatesWithBlock:^(id<MTRDeviceDelegate> delegate) {
[delegate device:self receivedEventReport:eventReport];
}];
}
Expand All @@ -113,7 +113,7 @@ - (oneway void)device:(NSNumber *)nodeID receivedEventReport:(NSArray<NSDictiona
- (oneway void)deviceBecameActive:(NSNumber *)nodeID
{
MTR_LOG("%s", __PRETTY_FUNCTION__);
[self _callDelegatesWithBlock:^(id<MTRDeviceDelegate> delegate) {
[self _lockAndCallDelegatesWithBlock:^(id<MTRDeviceDelegate> delegate) {
if ([delegate respondsToSelector:@selector(deviceBecameActive:)]) {
[delegate deviceBecameActive:self];
}
Expand All @@ -122,7 +122,7 @@ - (oneway void)deviceBecameActive:(NSNumber *)nodeID

- (oneway void)deviceCachePrimed:(NSNumber *)nodeID
{
[self _callDelegatesWithBlock:^(id<MTRDeviceDelegate> delegate) {
[self _lockAndCallDelegatesWithBlock:^(id<MTRDeviceDelegate> delegate) {
if ([delegate respondsToSelector:@selector(deviceCachePrimed:)]) {
[delegate deviceCachePrimed:self];
}
Expand All @@ -131,7 +131,7 @@ - (oneway void)deviceCachePrimed:(NSNumber *)nodeID

- (oneway void)deviceConfigurationChanged:(NSNumber *)nodeID
{
[self _callDelegatesWithBlock:^(id<MTRDeviceDelegate> delegate) {
[self _lockAndCallDelegatesWithBlock:^(id<MTRDeviceDelegate> delegate) {
if ([delegate respondsToSelector:@selector(deviceConfigurationChanged:)]) {
[delegate deviceConfigurationChanged:self];
}
Expand Down

0 comments on commit 69a8eb9

Please sign in to comment.