Skip to content

Commit ddca34c

Browse files
Improve the Darwin logging when we cancel dns-sd browse/resolve operations. (#26072)
Right now we are treating those as "timeout" and logging that, which really confuses people reading the logs. We should log them as cancellations instead. There's no obvious kDNSServiceErr_* value for "cancelled", so allow doing GenericContext::Finalize with a CHIP_ERROR.
1 parent a235160 commit ddca34c

File tree

3 files changed

+40
-25
lines changed

3 files changed

+40
-25
lines changed

src/platform/Darwin/DnssdContexts.cpp

+23-13
Original file line numberDiff line numberDiff line change
@@ -112,25 +112,35 @@ DNSServiceProtocol GetProtocol(const chip::Inet::IPAddressType & addressType)
112112
namespace chip {
113113
namespace Dnssd {
114114

115-
CHIP_ERROR GenericContext::Finalize(DNSServiceErrorType err)
115+
CHIP_ERROR GenericContext::FinalizeInternal(const char * errorStr, CHIP_ERROR err)
116116
{
117117
if (MdnsContexts::GetInstance().Has(this) == CHIP_NO_ERROR)
118118
{
119-
if (kDNSServiceErr_NoError == err)
119+
if (CHIP_NO_ERROR == err)
120120
{
121121
DispatchSuccess();
122122
}
123123
else
124124
{
125-
DispatchFailure(err);
125+
DispatchFailure(errorStr, err);
126126
}
127127
}
128128
else
129129
{
130130
chip::Platform::Delete(this);
131131
}
132132

133-
return Error::ToChipError(err);
133+
return err;
134+
}
135+
136+
CHIP_ERROR GenericContext::Finalize(CHIP_ERROR err)
137+
{
138+
return FinalizeInternal(err.AsString(), err);
139+
}
140+
141+
CHIP_ERROR GenericContext::Finalize(DNSServiceErrorType err)
142+
{
143+
return FinalizeInternal(Error::ToString(err), Error::ToChipError(err));
134144
}
135145

136146
MdnsContexts::~MdnsContexts()
@@ -289,10 +299,10 @@ RegisterContext::RegisterContext(const char * sType, const char * instanceName,
289299
mInstanceName = instanceName;
290300
}
291301

292-
void RegisterContext::DispatchFailure(DNSServiceErrorType err)
302+
void RegisterContext::DispatchFailure(const char * errorStr, CHIP_ERROR err)
293303
{
294-
ChipLogError(Discovery, "Mdns: Register failure (%s)", Error::ToString(err));
295-
callback(context, nullptr, nullptr, Error::ToChipError(err));
304+
ChipLogError(Discovery, "Mdns: Register failure (%s)", errorStr);
305+
callback(context, nullptr, nullptr, err);
296306
MdnsContexts::GetInstance().Remove(this);
297307
}
298308

@@ -316,10 +326,10 @@ BrowseContext::BrowseContext(void * cbContext, DnssdBrowseCallback cb, DnssdServ
316326
protocol = cbContextProtocol;
317327
}
318328

319-
void BrowseContext::DispatchFailure(DNSServiceErrorType err)
329+
void BrowseContext::DispatchFailure(const char * errorStr, CHIP_ERROR err)
320330
{
321-
ChipLogError(Discovery, "Mdns: Browse failure (%s)", Error::ToString(err));
322-
callback(context, nullptr, 0, true, Error::ToChipError(err));
331+
ChipLogError(Discovery, "Mdns: Browse failure (%s)", errorStr);
332+
callback(context, nullptr, 0, true, err);
323333
MdnsContexts::GetInstance().Remove(this);
324334
}
325335

@@ -353,14 +363,14 @@ ResolveContext::ResolveContext(void * cbContext, DnssdResolveCallback cb, chip::
353363

354364
ResolveContext::~ResolveContext() {}
355365

356-
void ResolveContext::DispatchFailure(DNSServiceErrorType err)
366+
void ResolveContext::DispatchFailure(const char * errorStr, CHIP_ERROR err)
357367
{
358-
ChipLogError(Discovery, "Mdns: Resolve failure (%s)", Error::ToString(err));
368+
ChipLogError(Discovery, "Mdns: Resolve failure (%s)", errorStr);
359369
// Remove before dispatching, so calls back into
360370
// ChipDnssdResolveNoLongerNeeded don't find us and try to also remove us.
361371
bool needDelete = MdnsContexts::GetInstance().RemoveWithoutDeleting(this);
362372

363-
callback(context, nullptr, Span<Inet::IPAddress>(), Error::ToChipError(err));
373+
callback(context, nullptr, Span<Inet::IPAddress>(), err);
364374

365375
if (needDelete)
366376
{

src/platform/Darwin/DnssdImpl.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,11 @@ CHIP_ERROR ChipDnssdStopBrowse(intptr_t browseIdentifier)
465465
return CHIP_ERROR_NOT_FOUND;
466466
}
467467

468-
// Just treat this as a timeout error. Don't bother delivering the partial
468+
// We have been canceled. Don't bother delivering the partial
469469
// results we have queued up in the BrowseContext, if any. In practice
470470
// there shouldn't be anything there long-term anyway.
471471
//
472-
// Make sure to time out all the resolves first, before we time out the
472+
// Make sure to cancel all the resolves first, before we cancel the
473473
// browse (just to avoid dangling pointers in the resolves, even though we
474474
// only use them for equality compares).
475475
std::vector<GenericContext *> resolves;
@@ -481,10 +481,10 @@ CHIP_ERROR ChipDnssdStopBrowse(intptr_t browseIdentifier)
481481

482482
for (auto & resolve : resolves)
483483
{
484-
resolve->Finalize(kDNSServiceErr_Timeout);
484+
resolve->Finalize(CHIP_ERROR_CANCELLED);
485485
}
486486

487-
ctx->Finalize(kDNSServiceErr_Timeout);
487+
ctx->Finalize(CHIP_ERROR_CANCELLED);
488488
return CHIP_NO_ERROR;
489489
}
490490

@@ -511,11 +511,11 @@ void ChipDnssdResolveNoLongerNeeded(const char * instanceName)
511511
if (*existingCtx->consumerCounter == 0)
512512
{
513513
// No more consumers; clear out all of these resolves so they don't
514-
// stick around. Dispatch timeout failure on all of them to make sure
515-
// whatever kicked them off cleans up resources as needed.
514+
// stick around. Dispatch a "cancelled" failure on all of them to make
515+
// sure whatever kicked them off cleans up resources as needed.
516516
do
517517
{
518-
existingCtx->Finalize(kDNSServiceErr_Timeout);
518+
existingCtx->Finalize(CHIP_ERROR_CANCELLED);
519519
existingCtx = MdnsContexts::GetInstance().GetExistingResolveForInstanceName(instanceName);
520520
} while (existingCtx != nullptr);
521521
}

src/platform/Darwin/DnssdImpl.h

+10-5
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,14 @@ struct GenericContext
4444

4545
virtual ~GenericContext() {}
4646

47+
CHIP_ERROR Finalize(CHIP_ERROR err);
4748
CHIP_ERROR Finalize(DNSServiceErrorType err = kDNSServiceErr_NoError);
48-
virtual void DispatchFailure(DNSServiceErrorType err) = 0;
49-
virtual void DispatchSuccess() = 0;
49+
50+
virtual void DispatchFailure(const char * errorStr, CHIP_ERROR err) = 0;
51+
virtual void DispatchSuccess() = 0;
52+
53+
private:
54+
CHIP_ERROR FinalizeInternal(const char * errorStr, CHIP_ERROR err);
5055
};
5156

5257
struct RegisterContext;
@@ -130,7 +135,7 @@ struct RegisterContext : public GenericContext
130135
RegisterContext(const char * sType, const char * instanceName, DnssdPublishCallback cb, void * cbContext);
131136
virtual ~RegisterContext() { mHostNameRegistrar.Unregister(); }
132137

133-
void DispatchFailure(DNSServiceErrorType err) override;
138+
void DispatchFailure(const char * errorStr, CHIP_ERROR err) override;
134139
void DispatchSuccess() override;
135140

136141
bool matches(const char * sType) { return mType.compare(sType) == 0; }
@@ -145,7 +150,7 @@ struct BrowseContext : public GenericContext
145150
BrowseContext(void * cbContext, DnssdBrowseCallback cb, DnssdServiceProtocol cbContextProtocol);
146151
virtual ~BrowseContext() {}
147152

148-
void DispatchFailure(DNSServiceErrorType err) override;
153+
void DispatchFailure(const char * errorStr, CHIP_ERROR err) override;
149154
void DispatchSuccess() override;
150155

151156
// Dispatch what we have found so far, but don't stop browsing.
@@ -195,7 +200,7 @@ struct ResolveContext : public GenericContext
195200
std::shared_ptr<uint32_t> && consumerCounterToUse);
196201
virtual ~ResolveContext();
197202

198-
void DispatchFailure(DNSServiceErrorType err) override;
203+
void DispatchFailure(const char * errorStr, CHIP_ERROR err) override;
199204
void DispatchSuccess() override;
200205

201206
CHIP_ERROR OnNewAddress(uint32_t interfaceId, const struct sockaddr * address);

0 commit comments

Comments
 (0)