diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm index 72cf8737f8aa7c..d7fb8e7b119cf0 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm @@ -641,9 +641,7 @@ - (MTRDeviceController * _Nullable)createControllerOnExistingFabric:(MTRDeviceCo return nil; } - os_unfair_lock_lock(&self->_controllersLock); - NSArray * controllersCopy = [self->_controllers copy]; - os_unfair_lock_unlock(&self->_controllersLock); + auto * controllersCopy = [self getRunningControllers]; for (MTRDeviceController * existing in controllersCopy) { BOOL isRunning = YES; // assume the worst @@ -880,13 +878,19 @@ - (void)controllerShuttingDown:(MTRDeviceController *)controller [controller deinitFromFactory]; } -- (nullable MTRDeviceController *)runningControllerForFabricIndex:(chip::FabricIndex)fabricIndex +- (NSArray *)getRunningControllers { - assertChipStackLockedByCurrentThread(); - os_unfair_lock_lock(&_controllersLock); NSArray * controllersCopy = [_controllers copy]; os_unfair_lock_unlock(&_controllersLock); + return controllersCopy; +} + +- (nullable MTRDeviceController *)runningControllerForFabricIndex:(chip::FabricIndex)fabricIndex +{ + assertChipStackLockedByCurrentThread(); + + auto * controllersCopy = [self getRunningControllers]; for (MTRDeviceController * existing in controllersCopy) { if ([existing fabricIndex] == fabricIndex) { @@ -901,9 +905,7 @@ - (void)operationalInstanceAdded:(chip::PeerId &)operationalID { assertChipStackLockedByCurrentThread(); - os_unfair_lock_lock(&_controllersLock); - NSArray * controllersCopy = [_controllers copy]; - os_unfair_lock_unlock(&_controllersLock); + auto * controllersCopy = [self getRunningControllers]; for (MTRDeviceController * controller in controllersCopy) { auto * compressedFabricId = controller.compressedFabricID; diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerFactory_Internal.h b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory_Internal.h index 9903eaa77e2cd2..dc0b968b427782 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceControllerFactory_Internal.h +++ b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory_Internal.h @@ -42,6 +42,12 @@ NS_ASSUME_NONNULL_BEGIN - (void)controllerShuttingDown:(MTRDeviceController *)controller; +/** + * Get the list of running controllers. This will include controllers that are + * in the middle of starting up or shutting down. + */ +- (NSArray *)getRunningControllers; + /** * Find a running controller, if any, for the given fabric index. */