Entity discovery silently stops at the first failing entity: the defensive loop in _discover_new_entities is defeated by generator semantics

Open
#886 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
Half a day
Newbie friendliness
66/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
python
Domain
backend

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from zigpy/zha

All issues in zigpy/zha

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.