Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Darwin] MTRDevice_XPC delegate callbacks need to hold lock before _callDelegatesWithBlock #35095

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading