kasm_agent crash-loops due to unhandled NotFound in clear_stale_mounts
Description
kasm_agent (v1.18.1) enters a restart loop when a ghost/phantom Docker container exists on the host. The agent crashes on every startup attempt during the clear_stale_mounts phase.
Environment
- Kasm Workspaces 1.18.1
- Docker Engine on Ubuntu 22.04 (API v1.52)
docker-py(Python Docker SDK) bundled with the agent image
Traceback
Traceback (most recent call last):
File "ProvisionAgent/__init__.py", line 396, in <module>
File "ProvisionAgent/__init__.py", line 366, in start
File "ProvisionAgent/__init__.py", line 354, in __init__
File "ProvisionAgent/provision.py", line 580, in clear_stale_mounts
File "/usr/local/lib/python3.12/site-packages/docker/models/containers.py", line 1018, in list
containers.append(self.get(r['Id']))
File "/usr/local/lib/python3.12/site-packages/docker/models/containers.py", line 954, in get
resp = self.client.api.inspect_container(container_id)
...
docker.errors.NotFound: 404 Client Error for http+docker://localhost/v1.52/containers/<id>/json:
Not Found ("No such container: <id>")
Root Cause
clear_stale_mounts calls docker.models.containers.list() which internally:
- Calls the Docker List API to get container IDs
- Calls
inspecton each container to build full Container objects
If a container is in a corrupted/ghost state (directory exists in /var/lib/docker/containers/ but Docker daemon cannot inspect it), step 2 throws docker.errors.NotFound and the unhandled exception kills the agent.
This is a well-documented race condition in the Docker Python SDK:
- https://github.com/docker/docker-py/issues/1485
- https://github.com/docker/docker-py/issues/2451
Ghost containers can arise from unclean shutdowns, Docker daemon crashes, or the known Docker engine issue https://github.com/moby/moby/issues/44512.
Suggested Fix
The Docker Python SDK provides the ignore_removed parameter (added in docker-py PR #2009) specifically for this scenario. In provision.py, the containers.list() call should use:
docker_client.containers.list(ignore_removed=True)
Alternatively, wrap the inspect call in a try/except for docker.errors.NotFound to skip ghost containers gracefully.
Current Workaround
Manually remove the ghost container directory and restart Docker:
sudo rm -rf /var/lib/docker/containers/<ghost_container_id>
sudo systemctl restart docker
This is disruptive on production systems as it restarts all containers.
Hi there, thanks for reporting this issue!
We’ve reviewed your report and have created an internal ticket to investigate further.
This GitHub issue will remain open until the internal ticket is resolved or we have additional updates to share.
Internal Ticket Reference: KASM-8192
We appreciate your feedback and patience while our team looks into this.