Skip to content

Commit

Permalink
Fix thread race in MTRXPCListenerSampleTests. (#26862)
Browse files Browse the repository at this point in the history
WARNING: ThreadSanitizer: race on NSMutableDictionary (pid=72050)
      Modifying access of NSMutableDictionary at 0x7b0800149020 by thread T7:
        #0 -[__NSDictionaryM setObject:forKey:] <null>:2 (CoreFoundation:x86_64+0x40d4e)
        #1 -[MTRXPCListenerSample listener:shouldAcceptNewConnection:] MTRXPCListenerSampleTests.m:116 (MatterTests:x86_64+0x48437b)
        #2 service_connection_handler_make_connection <null>:2 (Foundation:x86_64+0xdea93)

      Previous modifying access of NSMutableDictionary at 0x7b0800149020 by thread T4:
        #0 -[__NSDictionaryM removeObjectForKey:] <null>:2 (CoreFoundation:x86_64+0x6cd6d)
        #1 __59-[MTRXPCListenerSample listener:shouldAcceptNewConnection:]_block_invoke MTRXPCListenerSampleTests.m:119 (MatterTests:x86_64+0x484630)
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Mar 18, 2024
1 parent 6a35294 commit 1071692
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/darwin/Framework/CHIPTests/MTRXPCListenerSampleTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

// system dependencies
#import <XCTest/XCTest.h>
#import <os/lock.h>

static uint16_t kTestVendorId = 0xFFF1u;

Expand Down Expand Up @@ -73,6 +74,8 @@ @interface MTRXPCListenerSample ()
@property (nonatomic, readonly, strong)
NSMutableDictionary<NSNumber *, MTRClusterStateCacheContainer *> * clusterStateCacheDictionary;

// serversLock controls access to _servers.
@property (nonatomic, readonly) os_unfair_lock serversLock;
@end

@implementation MTRXPCListenerSample
Expand All @@ -86,6 +89,7 @@ - (instancetype)init
_clusterStateCacheDictionary = [NSMutableDictionary dictionary];
_xpcListener = [NSXPCListener anonymousListener];
[_xpcListener setDelegate:(id<NSXPCListenerDelegate>) self];
_serversLock = OS_UNFAIR_LOCK_INIT;
}
return self;
}
Expand Down Expand Up @@ -113,10 +117,16 @@ - (BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConne
__auto_type newServer = [[MTRDeviceControllerServerSample alloc] initWithClientProxy:[newConnection remoteObjectProxy]
clusterStateCacheDictionary:_clusterStateCacheDictionary];
newConnection.exportedObject = newServer;

os_unfair_lock_lock(&_serversLock);
[_servers setObject:newServer forKey:newServer.identifier];
os_unfair_lock_unlock(&_serversLock);

newConnection.invalidationHandler = ^{
NSLog(@"XPC connection disconnected");
os_unfair_lock_lock(&self->_serversLock);
[self.servers removeObjectForKey:newServer.identifier];
os_unfair_lock_unlock(&self->_serversLock);
};
[newConnection resume];
return YES;
Expand Down

0 comments on commit 1071692

Please sign in to comment.