fix(gc): recheck owner status and mounts before scratch deletes
CI / security-codeql (push) Canceled after 0s
CI / test-unit (push) Canceled after 0s
CI / test-integration (push) Canceled after 0s
CI / test-env-virtual (push) Canceled after 0s
CI / test-env-nix (push) Canceled after 0s
CI / test-e2e (push) Canceled after 0s
CI / test-virgin-user (push) Canceled after 0s
CI / test-virgin-root (push) Canceled after 0s
CI / lint-shell (push) Canceled after 0s
CI / lint-python (push) Canceled after 0s
CI / lint-docker (push) Canceled after 0s
CI / quality (push) Canceled after 0s

CodeRabbit on PR 21: the scratch delete trusted the owner status read at plan time, and walk skipped mounts below a scratch directory that rmtree would cross. The owner file is now read again before every scratch delete; walk counts entries on another filesystem and a counted mount keeps the item above low.

PR: https://github.com/kevinveenbirkenbach/package-manager/pull/21
Created-with: https://s.veen.world/skills
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kevin Veen-BirkenbachandClaude Opus 5.5 committed 2026-12-01 19:23:17 +00:00
1 parent 4c603a0087
commit babc977cfe
5 files changed
+102 -13

No files matched your search

@@ -3,6 +3,7 @@ from __future__ import annotations
import os
import tempfile
import unittest
from types import SimpleNamespace
from unittest.mock import patch
from pkgmgr.actions.gc.report import walk as walk_module
@@ -108,6 +109,28 @@ class TestWalk(unittest.TestCase):
self.assertEqual(stats.size_bytes, 0)
def test_child_on_another_filesystem_is_counted_not_walked(self) -> None:
self._write("mnt/inner", b"x" * 8192)
mnt = os.path.join(self.root, "mnt")
fields = list(os.lstat(mnt))
fields[2] += 1
moved = os.stat_result(fields)
real_children = walk_module._children
def children(path, stats):
return [
SimpleNamespace(path=e.path, stat=lambda follow_symlinks: moved)
if e.path == mnt
else e
for e in real_children(path, stats)
]
with patch.object(walk_module, "_children", side_effect=children):
stats = walk(self.root)
self.assertEqual(stats.mounts, 1)
self.assertEqual(stats.size_bytes, allocated(self.root))
if __name__ == "__main__":
unittest.main()
@@ -1,10 +1,11 @@
from __future__ import annotations
import json
import os
import unittest
from unittest.mock import patch
from pkgmgr.actions.gc.risk import git_items, plan
from pkgmgr.actions.gc.risk import git_items, plan, tmp_items
from tests.unit.pkgmgr.actions.gc.risk.risk_fixture import (
BARE_CONTAINER,
DANGLING_ID,
@@ -113,6 +114,59 @@ class TestApplyLow(RiskPlanFixture, unittest.TestCase):
self.assertTrue(os.path.isdir(self.wt["done"]))
self.assertTrue(os.path.isfile(self.t["old-done.log"]))
def _owners_after_plan(self, content: str | None) -> tuple[int, dict]:
real_apply = plan._apply
def rewrite_then_apply(*args):
if content is None:
os.unlink(self.owners)
else:
with open(self.owners, "w") as fh:
fh.write(content)
return real_apply(*args)
with patch.object(plan, "_apply", side_effect=rewrite_then_apply):
code, printed, _ = self.run_gc(level="low", apply=True)
return code, printed
def test_owner_status_changed_after_plan_keeps_the_scratch(self) -> None:
running = json.dumps({self.t["old-stopped"]: "running"})
code, printed = self._owners_after_plan(running)
stopped = self.entry(printed, self.t["old-stopped"])
self.assertEqual(code, 0)
self.assertEqual(
stopped["result"], "skipped: changed since plan (owner task is running)"
)
self.assertTrue(os.path.isdir(self.t["old-stopped"]))
def test_owner_file_gone_after_plan_fails_and_keeps_the_scratch(self) -> None:
code, printed = self._owners_after_plan(None)
self.assertEqual(code, 1)
for name in ("old-stopped", "old-done.log"):
self.assertTrue(
self.entry(printed, self.t[name])["result"].startswith("failed:"), name
)
self.assertTrue(os.path.lexists(self.t[name]), name)
def test_mount_below_scratch_is_never_low(self) -> None:
real_walk = tmp_items.walk
def mounted(path, *args):
stats = real_walk(path, *args)
if path == self.t["old-stopped"]:
stats.mounts = 1
return stats
with patch.object(tmp_items, "walk", side_effect=mounted):
code, printed, _ = self.run_gc(level="low", apply=True)
self.assertEqual(code, 0)
self.assertNotIn(self.t["old-stopped"], self.risks(printed))
self.assertTrue(os.path.isdir(self.t["old-stopped"]))
if __name__ == "__main__":
unittest.main()