Skip to content

Commit 23563f6

Browse files
feasel0Copilot
andauthored
Linux/Dnssd: StopResolve detaches resolves before invoking cancel callbacks (#43393)
* Linux/Dnssd: StopResolve now synchronously delivers CHIP_ERROR_CANCELLED to matching pending resolve callbacks and removes their contexts to avoid later callbacks firing on stale/unused work. * Removing the entry from mAllocatedResolves first (capture the pointer, erase it, then invoke the callback and finally Delete) Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Refactored to detach contexts from mAllocatedResolves before invoking the cancelled callbacks and deleting. * simplified list removal with remove_if --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 743f157 commit 23563f6

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

src/platform/Linux/DnssdImpl.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -839,16 +839,25 @@ void MdnsAvahi::FreeResolveContext(size_t handle)
839839

840840
void MdnsAvahi::StopResolve(const char * name)
841841
{
842-
auto truncate_end = std::remove_if(mAllocatedResolves.begin(), mAllocatedResolves.end(),
843-
[name](ResolveContext * ctx) { return strcmp(ctx->mName, name) == 0; });
842+
// Cancel and delete any pending resolves for this instance name.
843+
//
844+
// Note: callbacks may re-enter into dnssd logic, so first detach matching contexts from
845+
// `mAllocatedResolves`, then invoke callbacks and delete outside of the list iteration.
846+
std::vector<ResolveContext *> contexts;
847+
mAllocatedResolves.remove_if([&contexts, name](ResolveContext * ctx) {
848+
if (strcmp(ctx->mName, name) == 0)
849+
{
850+
contexts.push_back(ctx);
851+
return true;
852+
}
853+
return false;
854+
});
844855

845-
for (auto it = truncate_end; it != mAllocatedResolves.end(); it++)
856+
for (auto * context : contexts)
846857
{
847-
(*it)->mCallback((*it)->mContext, nullptr, Span<Inet::IPAddress>(), CHIP_ERROR_CANCELLED);
848-
chip::Platform::Delete(*it);
858+
context->mCallback(context->mContext, nullptr, Span<Inet::IPAddress>(), CHIP_ERROR_CANCELLED);
859+
chip::Platform::Delete(context);
849860
}
850-
851-
mAllocatedResolves.erase(truncate_end, mAllocatedResolves.end());
852861
}
853862

854863
CHIP_ERROR MdnsAvahi::Resolve(const char * name, const char * type, DnssdServiceProtocol protocol, Inet::IPAddressType addressType,

0 commit comments

Comments
 (0)