Entity discovery silently stops at the first failing entity: the defensive loop in _discover_new_entities is defeated by generator semantics
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- Half a day
- Newbie friendliness
- 66/100
Research direction
Start with _discover_new_entities in zha/zigbee/device.py, where the try/except wraps next() on a generator that closes after raising. Then look at discover_entities_for_endpoint in zha/application/discovery.py to find where each entity is constructed. Run the four-entity reproduction from the issue to confirm the dropped tail. Done means the try/except sits inside the generator, entities after a failing one still get discovered, and a regression test uses a generator that raises mid-stream.
Written by the indexing model from the issue text.
Description
Split out of the review discussion on #884 (https://github.com/zigpy/zha/pull/884#discussion_r3910746398). Not caused by that PR — it is pre-existing, introduced with the defensive loop in #762 — but #884 adds the first raise that can reach it from config data, which is what made it worth writing down.
What happens
Device._discover_new_entities iterates entity discovery defensively so that one bad entity does not cost the whole device (zha/zigbee/device.py:1081-1097 on dev at 66603431):
# Iterate defensively so a failure in any single entity construction
# does not abort discovery for the rest of the device.
iterator = iter(self.discover_entities())
while True:
try:
entity = next(iterator)
except StopIteration:
break
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Failed to create entity during discovery")
continue
...
That does not do what the comment says. discover_entities is a generator, and a generator that propagates an exception is closed. The continue therefore lands on a dead iterator: the next next() raises StopIteration, the loop breaks, and every entity after the failing one is silently dropped.
So the real behaviour is "discovery stops at the first failing entity", not "the failing entity is skipped" — and the log line says Failed to create entity during discovery, singular, which reads exactly like the intended behaviour.
Reproduced
The loop shape in isolation, four entities with a raise on the second:
def inner(items):
for i in items:
if i == 2:
raise ValueError("bad entity 2")
yield i
def discover_entities():
yield from inner([1, 2, 3, 4])
discovered = []
iterator = iter(discover_entities())
while True:
try:
entity = next(iterator)
except StopIteration:
break
except Exception as e:
print(f" caught: {e!r} -> continue")
continue
discovered.append(entity)
print("discovered:", discovered)
caught: ValueError('bad entity 2') -> continue
discovered: [1] # the guard's intent would be [1, 3, 4]
Why it is not biting today, and when it would
Every built-in _server_cluster_config is a class-level literal, so a malformed AttrConfig fails at import time rather than during discovery. The zhaquirks follow-up described in #884 only sets reporting_override=True inside the reporting_config is not None branch, which cannot violate the new invariant either.
It becomes reachable the moment entity config is built from quirk-supplied metadata rather than from literals — at which point the failure mode a user sees is "device came up missing an arbitrary tail of its entities, with one exception in the log", which is considerably harder to diagnose than "one entity missing".
Fix shape
The try/except has to sit inside the generator, around the per-entity construction, so the generator survives and keeps yielding — e.g. in discover_entities_for_endpoint (zha/application/discovery.py:162) at the point each entity is instantiated, or in Device.discover_entities wrapping each endpoint's sub-iterator. Whatever the placement, a regression test with a generator that raises mid-stream is the thing worth having, since the current shape looks correct at the call site.
- Dominant language
- Python
- Stars
- 96
- Forks
- 84
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 30
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from zigpy/zha
-
Device override creates the generic entity instead of the vendor-specific one (Light instead of HueLight, ForceOnLight, MinTransitionLight)Possibly taken @zigpy-review-bot claimed this today. Openbot: code audit
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
Each time a device becomes available again, ZHA adds another firmware update listener that is never releasedPossibly taken @zigpy-review-bot claimed this 1 day ago. Openbot: code audit
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
Maintainers usually reply within 1 day
-
Devices that left the network are re-initialized on every availability check, reading attributes from a device that is gonePossibly taken @zigpy-review-bot claimed this 1 day ago. Openbot: code audit
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 1 day
-
device specific quirks
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
pimutils/vdirsyncer#1242 ·
-
priority:P2
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
QuantConnect/lean-cli#792 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
open-webui/open-webui#35360 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Suggest link: Destiny Verify (MIT zero-dependency pre-commit security, citation & link hygiene CLI)Open
Difficulty 1/5 Under an hour Newbie friendliness 68/100