From c07070f114564c8ece72f25b0a1951ff83f956a7 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Tue, 22 Aug 2023 10:18:11 +0300 Subject: [PATCH] Add missing locks around nodeIDToDeviceMap access. (#28675) Those accesses are all supposed to happen while holding _deviceMapLock, but we had some that were not locking. --- src/darwin/Framework/CHIP/MTRDeviceController.mm | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRDeviceController.mm b/src/darwin/Framework/CHIP/MTRDeviceController.mm index cf4ab4c068d465..21ec9e56f737e9 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceController.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceController.mm @@ -163,11 +163,17 @@ - (void)cleanupAfterStartup { // Invalidate our MTRDevice instances before we shut down our secure // sessions and whatnot, so they don't start trying to resubscribe when we - // do the secure session shutdowns. - for (MTRDevice * device in [self.nodeIDToDeviceMap allValues]) { + // do the secure session shutdowns. Since we don't want to hold the lock + // while calling out into arbitrary invalidation code, snapshot the list of + // devices before we start invalidating. + os_unfair_lock_lock(&_deviceMapLock); + NSArray * devices = [self.nodeIDToDeviceMap allValues]; + [self.nodeIDToDeviceMap removeAllObjects]; + os_unfair_lock_unlock(&_deviceMapLock); + + for (MTRDevice * device in devices) { [device invalidate]; } - [self.nodeIDToDeviceMap removeAllObjects]; [self stopBrowseForCommissionables]; [_factory controllerShuttingDown:self];