Skip to content

fix: re-attach or drop stale monitor when a container is recreated (404) - #219

Closed
ciltocruz wants to merge 1 commit into
ualex73:masterfrom
ciltocruz:fix/issue-188-stale-container-monitor
Closed

ciltocruz wants to merge 1 commit into
ualex73:masterfrom
ciltocruz:fix/issue-188-stale-container-monitor

Conversation

@ciltocruz

Copy link
Copy Markdown

Fixes #188

Problem

When a container is recreated (Watchtower, docker-compose up -d, podman restart, ...), each monitor keeps the Docker API handle for the old container id. The new container has the same name but a new id, so:

  • The poll loop logs Container not available anymore (3a) (DockerError 404 ...) every retry, forever, and never recovers (worst case through docker-proxy where events can be missed).
  • If the destroy event is missed, the later create event hits Container already monitored and is dropped, so the stale monitor is never replaced.

Also seen with podman: a stopped then restarted container stays unknown until HA is restarted.

Fix (custom_components/monitor_docker/helpers.py)

  1. On DockerError 404 in the container poll loop, try to re-attach by name (_handle_container_gone). If the recreated container is found, monitoring continues transparently with the fresh handle (Re-attached to recreated container).
  2. If the container name no longer exists (2 consecutive 404 re-attach failures), the container monitor asks the DockerAPI instance to drop its own monitor (on_container_gone), so entities are removed and a later create event re-adds everything cleanly.
  3. _container_add now detects the name is monitored, checks the new container's id, and replaces the stale monitor instead of bailing out with Container already monitored.

Behavior notes

  • Generic (transport/timeout) errors keep the old 40s retry behavior unchanged.
  • No config or UI changes; sensors/switches/buttons are recreated normally by the existing load_platform flow when a monitor is re-added.

When a container is recreated (Watchtower, docker-compose up -d, ...), the
monitor keeps a handle for the old container id, so every poll fails with
'DockerError 404' forever and retries every 60s. If the destroy event is
missed (docker proxy, races), a later create event bails out with
'Container already monitored' without replacing the stale monitor.

- On 404, try to re-attach to a container with the same name; on success
  the monitor continues with the fresh handle.
- If the container does not exist anymore (2 consecutive 404s), ask the
  DockerAPI instance to drop this monitor so a later create event can
  re-add it.
- _container_add now replaces a stale monitor instead of returning early.

Fixes ualex73#188
@ciltocruz ciltocruz closed this Oct 19, 2026
ak111pro added a commit to ak111pro/monitor_docker that referenced this pull request Oct 19, 2026
A failed attach in _container_add left the monitor in the container table,
so every later container of that name was skipped as "Container already
monitored" and never monitored (ualex73#166). The monitor is dropped now, and a
container that vanished before it could be attached is logged as a warning
instead of an error.

When a monitored container was recreated and the destroy event was missed,
the poll loop retried the old container id forever (ualex73#188). On a 404 the
monitor now re-attaches to the container that has its name; if there is
none twice in a row it asks DockerAPI to drop the monitor and its entities,
and the next create event adds it back. A create event for a name whose
monitor points to an old container replaces that monitor.

Approach from ualex73#219 by @ciltocruz, adapted to the
selection-aware monitors (callback instead of a parent reference, monitor
identity checks, counters reset after re-attaching).

Co-authored-by: ciltocruz <19903919+ciltocruz@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015BTTm7J9zaAQxmzuzViueF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Restart needed when any docker container is re-created (reopening #143)

1 participant