ualex73 / monitor_docker

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

HA logs thread safety errors when Docker container is deleted and recreated #87

Closed airdrummingfool closed 1 month ago

airdrummingfool commented 2 years ago

Hello and thanks for this great component!

I've been seeing "Error doing job: Task was destroyed but it is pending!" logged in my Home Assistant logs for a long time now. I recently learned that I could get detailed logs by enabling the debugpy integration in the HA config:

debugpy:

After doing so, I found a series of logs related to monitor_docker that I figured I should report. I think I was working on the tailscale image around this time, so the container was probably deleted and recreated a few times.

2021-12-22 10:38:14 ERROR (Thread-10) [custom_components.monitor_docker.helpers] [Docker]: run_docker_events (Non-thread-safe operation invoked on an event loop other than the current one)
Traceback (most recent call last):
File "/config/custom_components/monitor_docker/helpers.py", line 383, in _run_docker_events
self._containers[cname].remove_entities()
File "/config/custom_components/monitor_docker/helpers.py", line 1185, in remove_entities
callback(remove=True)
File "/config/custom_components/monitor_docker/switch.py", line 201, in event_callback
self._loop.create_task(self.async_remove())
File "/usr/local/lib/python3.9/asyncio/base_events.py", line 433, in create_task
task = tasks.Task(coro, loop=self, name=name)
File "/usr/local/lib/python3.9/asyncio/base_events.py", line 748, in call_soon
self._check_thread()
File "/usr/local/lib/python3.9/asyncio/base_events.py", line 785, in _check_thread
raise RuntimeError(
RuntimeError: Non-thread-safe operation invoked on an event loop other than the current one
2021-12-22 10:38:15 ERROR (Thread-10) [custom_components.monitor_docker.helpers] [Docker] 311c8f5400b0_config-tailscale-1: Container not available anymore (3) (DockerError(404, 'No such container: 311c8f5400b0e2d6263b514e9bba65e7e52e9773db9e4246186d66c75ed74c05'))
Traceback (most recent call last):
File "/config/custom_components/monitor_docker/helpers.py", line 751, in _run
await self._run_container_info()
File "/config/custom_components/monitor_docker/helpers.py", line 786, in _run_container_info
raw = await self._container.show()
File "/usr/local/lib/python3.9/site-packages/aiodocker/containers.py", line 177, in show
data = await self.docker._query_json(
File "/usr/local/lib/python3.9/site-packages/aiodocker/docker.py", line 300, in _query_json
async with self._query(
File "/usr/local/lib/python3.9/site-packages/aiodocker/utils.py", line 309, in __aenter__
resp = await self._coro
File "/usr/local/lib/python3.9/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: 311c8f5400b0e2d6263b514e9bba65e7e52e9773db9e4246186d66c75ed74c05')
2021-12-22 10:38:15 ERROR (Thread-2) [homeassistant] Error doing job: Task was destroyed but it is pending!: File "/usr/local/lib/python3.9/site-packages/debugpy/_vendored/pydevd/_pydev_bundle/pydev_monkey.py", line 1054, in __call__
ret = self.original_func(*self.args, **self.kwargs)
File "/usr/local/lib/python3.9/threading.py", line 930, in _bootstrap
self._bootstrap_inner()
File "/usr/local/lib/python3.9/threading.py", line 973, in _bootstrap_inner
self.run()
File "/usr/local/lib/python3.9/threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "/config/custom_components/monitor_docker/__init__.py", line 128, in RunDocker
loop.run_forever()
File "/usr/local/lib/python3.9/asyncio/base_events.py", line 596, in run_forever
self._run_once()
File "/usr/local/lib/python3.9/asyncio/base_events.py", line 1890, in _run_once
handle._run()
File "/usr/local/lib/python3.9/asyncio/events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "/config/custom_components/monitor_docker/helpers.py", line 383, in _run_docker_events
self._containers[cname].remove_entities()
File "/config/custom_components/monitor_docker/helpers.py", line 1185, in remove_entities
callback(remove=True)
File "/config/custom_components/monitor_docker/switch.py", line 201, in event_callback
self._loop.create_task(self.async_remove())
File "/usr/local/lib/python3.9/asyncio/base_events.py", line 433, in create_task
task = tasks.Task(coro, loop=self, name=name)
ualex73 commented 1 month ago

I will close this old issue. If still persists, please open a new issue.