Skip to content

The agent that grows with you

fix(scratch): size a doomed entry before the final re-check, not between it and rmtree (review F6 on #139850)

Teknium33beaec5700c
diff --git a/hermes_constants_scratch.py b/hermes_constants_scratch.py
index f92e8072ab2..be599c0c5c0 100644
--- a/hermes_constants_scratch.py
+++ b/hermes_constants_scratch.py
@@ -306,6 +306,10 @@ def _prune_idle_entries(
# audit names the failed re-scan instead of inventing a touch that did not
# happen, so operators can tell an uninspectable candidate from verified
# recent activity (P3 on #134173).
+ # Size is collected before the re-check, not after it: a full-tree walk between
+ # the re-check and rmtree reopens the window for a write landing during that
+ # walk to be destroyed (review F6 on #139850).
+ size = _tree_bytes(entry)
state = subtree_touch_state(entry, cutoff)
if state != _IDLE:
# Only a missing path is a vanish: a stat that raises PermissionError means
@@ -331,7 +335,6 @@ def _prune_idle_entries(
os.fspath(entry),
)
continue
- size = _tree_bytes(entry)
try:
if entry.is_dir() and not entry.is_symlink():
shutil.rmtree(entry, ignore_errors=True)
diff --git a/tests/test_scratch_dir.py b/tests/test_scratch_dir.py
index cf70ed6cce9..120c6c881f8 100644
--- a/tests/test_scratch_dir.py
+++ b/tests/test_scratch_dir.py
@@ -393,6 +393,34 @@ def test_prune_releases_git_worktree_registration_of_idle_entry(tmp_path):
assert str(tree) not in listing and not tree.exists()
+
+def test_write_during_size_walk_is_rescued(tmp_path: Path) -> None:
+ """F6 on #139850: no full-tree walk may sit between the final re-check and rmtree.
+ A writer overwrites an old file while the prune is sizing the doomed entry; the
+ re-check must still see it. Red when the size walk runs after the re-check: the
+ entry is destroyed with the fresh write inside."""
+ import hermes_constants_scratch as scratch_mod
+
+ scratch = get_scratch_dir(tmp_path, prune=False)
+ entry = scratch / "sized-lane"
+ entry.mkdir()
+ payload = entry / "payload"
+ payload.write_text("OLD", encoding="utf-8")
+ ancient = time.time() - 48 * 3600
+ for path in (payload, entry):
+ os.utime(path, (ancient, ancient))
+ real_tree_bytes = scratch_mod._tree_bytes
+
+ def write_during_size_walk(path):
+ payload.write_text("FRESH", encoding="utf-8")
+ return real_tree_bytes(path)
+
+ with patch("hermes_constants_scratch._tree_bytes", write_during_size_walk):
+ assert prune_scratch_dir(scratch) == 0
+ assert payload.read_text(encoding="utf-8") == "FRESH", "write during size walk was destroyed"
+ log = (tmp_path / "logs" / "scratch-prune.log").read_text(encoding="utf-8-sig")
+ assert f"rescued {str(entry)!r} " in log, log
+
def test_midprune_resumed_write_is_rescued(tmp_path: Path) -> None:
"""F1 on #132401, ported from 3f72fd9c8e (#132455): a writer whose cwd is outside
scratch — invisible to the reap — resumes during the prune's own reap window and
diff --git a/website/docs/user-guide/configuration.md b/website/docs/user-guide/configuration.md
index 097ea77fbbc..3c9b7560eb9 100644
--- a/website/docs/user-guide/configuration.md
+++ b/website/docs/user-guide/configuration.md
@@ -263,7 +263,7 @@ with its working directory inside that entry (or inside a scratch path that no l
such as a headless browser left behind by a test run) and drops any `git worktree`
registration that pointed into it. Each entry is re-checked right before it is deleted, so
one written to while the prune was stopping processes is kept. Every removal, stopped
-process and kept entry is recorded in `~/.hermes/logs/scratch-prune.log`. `hermes doctor`
+process and entry kept by that re-check is recorded in `~/.hermes/logs/scratch-prune.log`. `hermes doctor`
reports the directory and its size, and
warns about directories over 1 GB elsewhere under `cache/` that no pruner covers.