From 0871a51e4c65c6f3912ddd32525db21fd278e222 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sawicz?= Date: Tue, 22 Oct 2024 12:09:32 +0200 Subject: [PATCH] cgroups: fall back to parent cgroup if no child found This is a change in snapd 2.65+, not all snap apps get a child cgroup any more. --- mir-ci/mir_ci/lib/cgroups.py | 4 +++- mir-ci/mir_ci/tests/test_tests.py | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/mir-ci/mir_ci/lib/cgroups.py b/mir-ci/mir_ci/lib/cgroups.py index fc175962..5e4ccbae 100644 --- a/mir-ci/mir_ci/lib/cgroups.py +++ b/mir-ci/mir_ci/lib/cgroups.py @@ -1,6 +1,7 @@ import asyncio import os import pathlib +import warnings from typing import Iterator @@ -29,7 +30,8 @@ async def get_cgroup_dir(pid: int) -> pathlib.Path: if path != parent_path: return path else: - raise RuntimeError(f"Unable to read cgroup directory for pid: {pid}") + warnings.warn(f"Unable to find child cgroup for pid: {pid}") + return parent_path @staticmethod def _get_cgroup_dir_internal(pid: int) -> pathlib.Path: diff --git a/mir-ci/mir_ci/tests/test_tests.py b/mir-ci/mir_ci/tests/test_tests.py index 021190f4..4f635f22 100644 --- a/mir-ci/mir_ci/tests/test_tests.py +++ b/mir-ci/mir_ci/tests/test_tests.py @@ -4,6 +4,7 @@ import time from collections import OrderedDict from contextlib import suppress +from pathlib import Path from unittest import IsolatedAsyncioTestCase from unittest.mock import MagicMock, Mock, call, mock_open, patch @@ -309,6 +310,11 @@ async def test_cgroup_path_raises_runtime_error_when_contents_are_none(self, moc with pytest.raises(RuntimeError, match=f"Unable to find path for process with pid: {os.getpid()}"): await Cgroup.get_cgroup_dir(12345) + @patch("builtins.open", new_callable=mock_open, read_data="0::path") + async def test_group_path_warns_when_no_child_found(self, mock_open): + with pytest.warns(UserWarning, match="Unable to find child cgroup"): + assert await Cgroup.get_cgroup_dir(12345) == Path("/sys/fs/cgroup/path") + @pytest.mark.self class TestDisplayServer: