diff --git a/src/platform/Darwin/DnssdImpl.cpp b/src/platform/Darwin/DnssdImpl.cpp index 7aa61acd2650d3..442a74cc0c7694 100644 --- a/src/platform/Darwin/DnssdImpl.cpp +++ b/src/platform/Darwin/DnssdImpl.cpp @@ -467,8 +467,21 @@ CHIP_ERROR ChipDnssdStopBrowse(intptr_t browseIdentifier) // Just treat this as a timeout error. Don't bother delivering the partial // results we have queued up in the BrowseContext, if any. In practice // there shouldn't be anything there long-term anyway. - auto browseCtx = static_cast(ctx); - browseCtx->Finalize(kDNSServiceErr_Timeout); + // + // Make sure to time out all the resolves first, before we time out the + // browse (because after the latter the serviceRefs on the resolves will not + // really be usable). + std::vector resolves; + MdnsContexts::GetInstance().FindAllMatchingPredicate([ctx] (GenericContext * item) { + return item->type == ContextType::Resolve && + item->serviceRef == ctx->serviceRef; + }, resolves); + + for (auto & resolve : resolves) { + resolve->Finalize(kDNSServiceErr_Timeout); + } + + ctx->Finalize(kDNSServiceErr_Timeout); return CHIP_NO_ERROR; } diff --git a/src/platform/Darwin/DnssdImpl.h b/src/platform/Darwin/DnssdImpl.h index 39df70e2b6a96c..6a20f6309dbdee 100644 --- a/src/platform/Darwin/DnssdImpl.h +++ b/src/platform/Darwin/DnssdImpl.h @@ -105,6 +105,21 @@ class MdnsContexts void Delete(GenericContext * context); + /** + * Fill the provided vector with all contexts for which the given predicate + * returns true. + */ + template + void FindAllMatchingPredicate(F predicate, std::vector & results) + { + results.clear(); + for (auto & ctx : mContexts) { + if (predicate(ctx)) { + results.push_back(ctx); + } + } + } + private: MdnsContexts(){}; static MdnsContexts sInstance;