Downgrade stopServiceResolution listener-not-registered to a warning#6880
Conversation
On API 34+ the NSD framework auto-unregisters the resolve listener once onServiceResolved/onResolveFailed fires, so the stopServiceResolution call in awaitClose always threw "listener not registered" on the happy path and logged a stack trace per discovered instance. Only call it when the collector cancelled before resolution completed.
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adjusts NSD service resolution teardown to avoid calling stopServiceResolution after the platform has already auto-unregistered the resolve listener.
Changes:
- Track whether resolution has completed and conditionally call
stopServiceResolutiononly on early cancellation. - Extend
getResolvedListenerto accept anonDonecallback invoked on both success and failure.
|
I do remember this log while implementing and decided to keep it. If you look at the code of the API 37
It's cheap to call this even if it throw but it ensure without adding complex logic that in all supported version we properly unregistered the listener. I get that the log might be disturbing, we could probably capture the |
Well, "cheap" is probably relative. From what I've learned, exceptions are always computationally expensive. Not sure how bad it is in nowadays VMs, but this StackOverflow thread it was measured 60 times slower than no exception. This is without formatting/logging. But I'd guess just the formatting and printing of the stack trace is probably quite expensive already. So in performance critical code you usually don't want any exception to be raised in regular execution flow. Now this is not performance critical code, so doesn't really apply here, just saying.
Yeah and I think that is in my book more important: A unhandled exception showing in logs in regular operation makes me think this got missed. It also makes the logs harder to read. I'd prefer capturing it and warn over just let it raise. But I also think the current solution is not overly complicated, it really makes sure we don't knowingly run into the exception and generate computational overhead for the VM 🤷 . |
In this context it won't impact the usage of the app, vs a potential listener staying forever in the manager. In a more critical part of the app like startup I would argue that we should avoid triggering a raise of an exception that we can potentially avoid when we can. Today this would impact some devices not all, after testing on the API 37 where I got the documentation no exception are raised anymore.
The general handling of exception within the app is not good. I'm still trying to shape how I want it to evolve in a way I like. |
Oh wow, ok that is a good argument to keep the call ofc. Let's catch and warn as agreed in chat. |
Per review on PR home-assistant#6880: keep the unconditional stopServiceResolution call (still needed on devices with T SDK extension < 22) but catch IllegalArgumentException specifically and log it as a warning without a stack trace, since on newer devices the platform auto-unregisters the resolve listener after onServiceResolved/onResolveFailed and this is the expected case rather than a real failure.
Summary
On API 34+ the NSD framework auto-unregisters the resolve listener once
onServiceResolved/onResolveFailedfires, so the unconditionalstopServiceResolutioncall inawaitClosethrowsIllegalArgumentException: listener not registeredon the happy path and logs an error-level stack trace per discovered instance. This is noisy and misleading — it looks like a real failure but isn't.The call itself is still required: per the platform documentation, devices with T SDK extension < 22 do not auto-unregister, so skipping the call would leak listeners on those devices. Instead, catch
IllegalArgumentExceptionspecifically and log it at warning level without a stack trace, since on modern devices this branch is the expected outcome rather than an error. Other exceptions still log at error level with a stack.Checklist
Screenshots
Link to pull request in documentation repositories
User Documentation: home-assistant/companion.home-assistant#
Developer Documentation: home-assistant/developers.home-assistant#
Any other notes