Skip to content

Commit eb2c6b7

Browse files
authoredMar 4, 2025
Ensure threadsafe access to mutable MTRDeviceController_XPC state. (project-chip#37875)
The node ID and compressed fabric ID can mutate in controllerConfigurationUpdated:, and that can race with the property getters; ensure we don't have a data race in that situation.
1 parent 037f26c commit eb2c6b7

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed
 

‎src/darwin/Framework/CHIP/MTRDeviceController_XPC.mm

+19-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ @interface MTRDeviceController_XPC ()
5454

5555
// #define MTR_HAVE_MACH_SERVICE_NAME_CONSTRUCTOR
5656

57-
@implementation MTRDeviceController_XPC
57+
@implementation MTRDeviceController_XPC {
58+
// Protects access to the data set in controllerConfigurationUpdated:
59+
os_unfair_lock _configurationLock;
60+
}
5861

5962
#pragma mark - Node ID Management
6063

@@ -119,8 +122,20 @@ - (void)forgetDeviceWithNodeID:(NSNumber *)nodeID
119122
}
120123

121124
#pragma mark - XPC
125+
122126
@synthesize controllerNodeID = _controllerNodeID;
127+
- (nullable NSNumber *)controllerNodeID
128+
{
129+
std::lock_guard lock(_configurationLock);
130+
return _controllerNodeID;
131+
}
132+
123133
@synthesize compressedFabricID = _compressedFabricID;
134+
- (nullable NSNumber *)compressedFabricID
135+
{
136+
std::lock_guard lock(_configurationLock);
137+
return _compressedFabricID;
138+
}
124139

125140
+ (NSMutableSet *)_allowedClasses
126141
{
@@ -345,6 +360,7 @@ - (nullable instancetype)initWithParameters:(MTRDeviceControllerAbstractParamete
345360
self.uniqueIdentifier = UUID;
346361
self.xpcParameters = xpcParameters;
347362
_workQueue = dispatch_queue_create("MTRDeviceController_XPC_queue", DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
363+
_configurationLock = OS_UNFAIR_LOCK_INIT;
348364

349365
if (![self _setupXPCConnection]) {
350366
return nil;
@@ -490,6 +506,8 @@ - (oneway void)controller:(NSUUID *)controller controllerConfigurationUpdated:(N
490506

491507
NSDictionary * controllerContext = MTR_SAFE_CAST(configuration[MTRDeviceControllerRegistrationControllerContextKey], NSDictionary);
492508
if (controllerContext) {
509+
std::lock_guard lock(_configurationLock);
510+
493511
NSNumber * controllerNodeID = MTR_SAFE_CAST(controllerContext[MTRDeviceControllerRegistrationControllerNodeIDKey], NSNumber);
494512
if (controllerNodeID) {
495513
_controllerNodeID = controllerNodeID;

0 commit comments

Comments
 (0)