Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure we don't create MTRDevice/MTRDeviceController cycles we can't break. #25714

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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