Fix TypeError when Docker returns null labels in container inspect

When a container has no labels, Docker returns {"Labels": null} in the
inspect response. The code was using .get("Labels", {}) which only
returns the default when the key is missing, not when it's null.

This caused _has_webterm_label() to raise TypeError, which was silently
caught by the event watcher's exception handler, causing it to reconnect
and miss the container start event.

Fixed by using .get("Labels") or {} pattern in:
- _handle_event() when processing start events
- _get_container_command() when extracting command label
- _get_container_theme() when extracting theme label

Added test for null labels case.
This commit is contained in:
GitHub Copilot
2026-01-31 08:40:39 +00:00
parent 924905eace
commit 84b4cee353
2 changed files with 36 additions and 4 deletions
+30
View File
@@ -263,6 +263,36 @@ class TestDockerWatcherIntegration:
session_manager.add_app.assert_called_once()
@pytest.mark.asyncio
async def test_handle_start_event_null_labels(self, session_manager):
"""Container with null labels in Docker inspect response is handled gracefully."""
watcher = DockerWatcher(session_manager)
async def mock_request(method, path):
if "/containers/" in path and "/json" in path:
# Docker returns null for Labels when container has none
return (
200,
'{"Name": "/no-labels-container", "Config": {"Labels": null}}',
)
return 404, ""
watcher._docker_request = mock_request
event = {
"Action": "start",
"Actor": {
"ID": "container456",
"Attributes": {},
},
}
# Should not raise an exception
await watcher._handle_event(event)
# Should not add container (no webterm labels)
session_manager.add_app.assert_not_called()
@pytest.mark.parametrize(
("labels", "expected"),