From 8306353fa62ed719ef9ce435c06efec954ec2dd9 Mon Sep 17 00:00:00 2001 From: Karsten Sperling <113487422+ksperling-apple@users.noreply.github.com> Date: Wed, 24 Jul 2024 15:31:47 +1200 Subject: [PATCH] Darwin: Avoid calling DnssdServer::StartServer() when not initialized (#34465) * Darwin: Avoid calling DnssdServer::StartServer() when not initialized Look at the SystemState directly instead of relying on _controllers. * Return error from AdvertiseOperational when fabric table is null --- src/app/server/Dnssd.cpp | 2 +- src/controller/CHIPDeviceControllerSystemState.h | 2 +- src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm | 9 ++++----- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/app/server/Dnssd.cpp b/src/app/server/Dnssd.cpp index 2fc56614e3ab9e..d3de7044bbda8b 100644 --- a/src/app/server/Dnssd.cpp +++ b/src/app/server/Dnssd.cpp @@ -188,7 +188,7 @@ void DnssdServer::GetPrimaryOrFallbackMACAddress(chip::MutableByteSpan mac) /// Set MDNS operational advertisement CHIP_ERROR DnssdServer::AdvertiseOperational() { - VerifyOrDie(mFabricTable != nullptr); + VerifyOrReturnError(mFabricTable != nullptr, CHIP_ERROR_INCORRECT_STATE); for (const FabricInfo & fabricInfo : *mFabricTable) { diff --git a/src/controller/CHIPDeviceControllerSystemState.h b/src/controller/CHIPDeviceControllerSystemState.h index 1ea0593d594993..da8f25a75d2541 100644 --- a/src/controller/CHIPDeviceControllerSystemState.h +++ b/src/controller/CHIPDeviceControllerSystemState.h @@ -208,7 +208,7 @@ class DeviceControllerSystemState mGroupDataProvider != nullptr && mReportScheduler != nullptr && mTimerDelegate != nullptr && mSessionKeystore != nullptr && mSessionResumptionStorage != nullptr && mBDXTransferServer != nullptr; }; - bool IsShutDown() { return mHaveShutDown; } + bool IsShutDown() const { return mHaveShutDown; } System::Layer * SystemLayer() const { return mSystemLayer; }; Inet::EndPointManager * TCPEndPointManager() const { return mTCPEndPointManager; }; diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm index 662a228bdd994e..18128249376e21 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm @@ -889,11 +889,10 @@ - (void)resetOperationalAdvertising // If we're not advertising, then there's no need to reset anything. VerifyOrReturn(_advertiseOperational); - // If there are no running controllers there will be no advertisements to reset. - { - std::lock_guard lock(_controllersLock); - VerifyOrReturn(_controllers.count > 0); - } + // Ensure the stack is running. We can't look at _controllers to determine this + // reliably because it gets updated early during controller startup from off-queue. + auto systemState = _controllerFactory->GetSystemState(); + VerifyOrReturn(systemState != nullptr && !systemState->IsShutDown()); // StartServer() is the only API we have for resetting DNS-SD advertising. // It sure would be nice if there were a "restart" that was a no-op if the