ualex73 / monitor_docker

Monitor Docker containers from Home Assistant
Apache License 2.0
267 stars 34 forks source link

UnboundLocalError: cannot access local variable 'taskcreated' where it is not associated with a value #151

Closed RoboMagus closed 3 days ago

RoboMagus commented 3 weeks ago

I've recently started seeing the following log message:

Traceback (most recent call last):
  File "/config/custom_components/monitor_docker/helpers.py", line 394, in _run_docker_events
    if self._event_destroy and not taskcreated:
                                   ^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'taskcreated' where it is not associated with a value

Which on its own does not occur often (seems to be related to when containers are restarted / created / stopped), but it's followed by quite a lot (200+) of the following errors which appear to be caused by the incorrect handling of container creation / destruction indicated by the error above.

Traceback (most recent call last):
  File "/config/custom_components/monitor_docker/helpers.py", line 775, in _run
    await self._run_container_info()
  File "/config/custom_components/monitor_docker/helpers.py", line 818, in _run_container_info
    raw: dict = await self._container.show()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/aiodocker/containers.py", line 177, in show
    data = await self.docker._query_json(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/aiodocker/docker.py", line 300, in _query_json
    async with self._query(
  File "/usr/local/lib/python3.12/site-packages/aiodocker/utils.py", line 309, in __aenter__
    resp = await self._coro
           ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/aiodocker/docker.py", line 275, in _do_query
    raise DockerError(response.status, json.loads(what.decode("utf8")))
aiodocker.exceptions.DockerError: DockerError(404, 'No such container: ed72b505674547cb1387ed511082d16f564569c242c98bea216a0bdee5e56d56')

I'm running the latest version of monitor_docker, and 2024.5.5 of HomeAssistant.

RoboMagus commented 3 weeks ago

This appears to occur in the rename event case on helpers.py line 394.

Here the taskcreated variable is indeed referenced before assignment. This bug would appear to be easily fixed by rewriting this code section as shown in the snippet below, however I do not know if removal of this check would have unintended side-effects.


  if self._event_destroy:
      await self._container_create_destroy()

  # Second re-add the container with the original name
  taskcreated = (
      True
      if self._event_create or self._event_destroy
      else False
  )
ualex73 commented 3 weeks ago

Thanks for reporting it, and I am not 100% anymore what that "if self._event_destroy and not taskcreated:" was intended for. You are right, changing It like you said, fixes the problem. I will commit it like that in the master, and we monitor how it goes (hopefully stable).

ualex73 commented 4 days ago

I released it officially in 1.19, all seem to be good.