Fix issue #1476 in lockcodemanager: shared lock providers are not torn down when a…
- User
Fix issue #1476 in lock_code_manager: shared lock providers are not torn down when a sibling config entry is present-but-unloaded, which lets a re-setup create a second provider instance and fire duplicate events.
Work in a new git worktree off
main(there's a.worktrees/dir, already gitignored). Suggested branch:shared-lock-unload.The defect
_lock_managed_by_other_entry(custom_components/lock_code_manager/__init__.py:787) answers "does another LCM entry manage this lock?" by scanninghass.config_entries.async_entries(DOMAIN, include_disabled=False, include_ignore=False)— that tests config-entry presence, not whether the entry is LOADED.async_unload_lock(__init__.py:829, guard at:844) uses that predicate to decide whether to calllock.async_unload(). So when a second entry merely exists — unloaded, in SETUP_RETRY, mid-reload — the provider is never torn down even though no loaded entry is using it.Three consequences, worst last:
- The provider's listeners survive with no loaded entry owning them.
- Duplicate events.
_find_shared_lock_instance(__init__.py:802) correctly requires LOADED, so on the next setup it finds nothing and builds a second provider instance with a second subscription. Both fire. Reproduced during review: reload entry A while entry B is unloaded-but-present, then trigger a code-slot event → two events for one physical use. Reachable via an options change on A while B is in SETUP_RETRY, or a reload-all. - The skip also leaves the provider's config-entry-state listener registered —
which the comment in
providers/virtual.py(async_unload) documents as precisely the dangerous path: a later provider reload re-runsasync_setupagainst an unloaded LCM entry.
The fix, and the constraint that makes it non-trivial
Do not just change the predicate — it has three call sites and they don't all want the same semantics:
__init__.py:844(async_unload_lock) — wants loaded. This is the bug.__init__.py:970(async_remove_entry) — repair-issue cleanup on entry removal; presence is correct here.__init__.py:1645(options-flow lock removal) — repair-issue cleanup; presence is arguably correct here too. Decide deliberately and say why.
Assistant
I'll start by getting oriented, then work through this with TDD.
Assistant
I'm using the using-git-worktrees skill to set up an isolated workspace.
Assistant
★ Insight ─────────────────────────────────────- Choosing the observable matters more than the assertion. Instance identity alone won't catch this bug — a fresh instance is created either way. What distinguishes fixed from broken is whether the old instance was torn down.
BaseLock.async_unloadclears_config_entry_state_unsubas its first real action, so that field is a faithful "did teardown run?" witness that also happens to be consequence #3 (the leaked config-entry-state listener). - HA tolerates double-unload (
ConfigEntry.async_unloadreturns early when state isNOT_LOADED), so a test can unload a fixture-owned entry explicitly and still let fixture teardown run.─────────────────────────────────────────────────
Writing the failing tests first:
- Choosing the observable matters more than the assertion. Instance identity alone won't catch this bug — a fresh instance is created either way. What distinguishes fixed from broken is whether the old instance was torn down.
Assistant
Now watch them fail:
Assistant
RED confirmed for the right reason — the state-change unsub is still live because
async_unloadnever ran. Now the fix:Assistant
GREEN. Now the full suite plus coverage:
Assistant
Now the mutation check — strip the
LOADEDclause from the new predicate and confirm the tests catch it:Assistant
Now the commit and PR:
Assistant
- User
run code review but note its already merged so amny follow up would be in a new P
Assistant
Confirmed merged (
4905c748). Running the review against the merged diff:Assistant
Let me verify each finding myself rather than take them at face value.
Assistant
All three verified. Let me check what else
remove_permanentlygates before designing fix 2:Assistant
Good — direct
async_unload_lockcalls are established. Writing both failing tests:Assistant
★ Insight ─────────────────────────────────────- Both findings reproduce against real production paths, not just the reviewer's reading. Finding 1's route is
__init__.py:1483—_async_setup_new_lockspops a lock whose provider failed to resolve while the entry itself still reachesLOADED. That's a loaded entry with the lock in config and no instance, which my config-based predicate treated as an owner. - The root cause is the same one the merged PR fixed, one layer down: two functions that must agree were written to ask different questions. The durable fix isn't a third careful predicate — it's making release defined as "acquire would find nothing," so they cannot drift again.
─────────────────────────────────────────────────
- Both findings reproduce against real production paths, not just the reviewer's reading. Finding 1's route is
Assistant
Now the inaccurate comment the reviewer flagged — HA does not unload entries on shutdown (verified:
_async_shutdownonly callsasync_cancel_retry_setup):Assistant
Now the mutation check on both fixes:
Assistant
Each mutation kills exactly its own test. Committing:
Assistant