Skip to content

Commit

Permalink
Make sure we don't create MTRDevice/MTRDeviceController cycles we can…
Browse files Browse the repository at this point in the history
…'t break. (#25714)

Fixes #25710
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Feb 4, 2024
1 parent c79b062 commit 1103909
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/darwin/Framework/CHIP/MTRDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ - (void)shutdown
// Clean up from a state where startup was called.
- (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]) {
[device invalidate];
}
[self.nodeIDToDeviceMap removeAllObjects];

[_factory controllerShuttingDown:self];
}

Expand Down Expand Up @@ -203,11 +211,6 @@ - (void)cleanup
delete _deviceControllerDelegateBridge;
_deviceControllerDelegateBridge = nullptr;
}

for (MTRDevice * device in [self.nodeIDToDeviceMap allValues]) {
[device invalidate];
}
[self.nodeIDToDeviceMap removeAllObjects];
}

- (BOOL)startup:(MTRDeviceControllerStartupParamsInternal *)startupParams
Expand Down Expand Up @@ -534,7 +537,13 @@ - (MTRDevice *)deviceForNodeID:(NSNumber *)nodeID
MTRDevice * deviceToReturn = self.nodeIDToDeviceMap[nodeID];
if (!deviceToReturn) {
deviceToReturn = [[MTRDevice alloc] initWithNodeID:nodeID controller:self];
self.nodeIDToDeviceMap[nodeID] = deviceToReturn;
// If we're not running, don't add the device to our map. That would
// create a cycle that nothing would break. Just return the device,
// which will be in exactly the state it would be in if it were created
// while we were running and then we got shut down.
if ([self isRunning]) {
self.nodeIDToDeviceMap[nodeID] = deviceToReturn;
}
}
os_unfair_lock_unlock(&_deviceMapLock);

Expand Down

0 comments on commit 1103909

Please sign in to comment.