Skip to content

perf(scheduler): switch FileCleanupTask to find -delete and add minimal path safety guards#967

Merged
jinbai340997 merged 6 commits into
alibaba:masterfrom
jinbai340997:feat/file-cleanup-perf-and-guards
Jun 3, 2026
Merged

perf(scheduler): switch FileCleanupTask to find -delete and add minimal path safety guards#967
jinbai340997 merged 6 commits into
alibaba:masterfrom
jinbai340997:feat/file-cleanup-perf-and-guards

Conversation

@jinbai340997

Copy link
Copy Markdown
Collaborator

Summary

1 个 commit,4 块改动:

  1. Perf-exec rm -f {} +-delete-exec rmdir {} +-depth -type d -empty -delete
  2. Path 校验TargetDirConfig.from_raw 拒绝空 / 相对路径 / 含 .. 的路径
  3. 黑名单_DANGEROUS_PATHS = ("/", "/tmp/miniforge") —— 仅 2 条真实事故源
  4. 测试:新增 tests/unit/admin/scheduler/test_file_cleanup_task.py 覆盖以上 3
    项 + 既有逻辑回归

Files (3, ≤8)

  • rock/admin/scheduler/tasks/file_cleanup_task.py (modify)
  • tests/unit/admin/scheduler/__init__.py (NEW, empty)
  • tests/unit/admin/scheduler/test_file_cleanup_task.py (NEW)

yaml 兼容

不破坏。同时支持 target_dirs: ["/path"]target_dirs: [{"path": "...", "exclude_files": [...]}]

Test plan

  • uv run pytest tests/unit/admin/scheduler/test_file_cleanup_task.py -v
  • uv run ruff check rock/admin/scheduler/tasks/file_cleanup_task.py tests/unit/admin/scheduler/test_file_cleanup_task.py
  • 关键回归:mock execute 验证 command-delete 出现、-exec rm 不出现

fixes #966

Comment thread rock/admin/scheduler/tasks/file_cleanup_task.py Outdated
jinbai340997 added a commit to jinbai340997/ROCK that referenced this pull request May 27, 2026
jinbai340997 added a commit to jinbai340997/ROCK that referenced this pull request May 27, 2026
@jinbai340997
jinbai340997 force-pushed the feat/file-cleanup-perf-and-guards branch from 33033de to 0c01918 Compare May 27, 2026 12:25
jinbai340997 added a commit to jinbai340997/ROCK that referenced this pull request May 27, 2026
jinbai340997 added a commit to jinbai340997/ROCK that referenced this pull request May 28, 2026
@jinbai340997
jinbai340997 force-pushed the feat/file-cleanup-perf-and-guards branch from 0c01918 to b0d0689 Compare May 28, 2026 03:29
jinbai340997 and others added 5 commits June 1, 2026 16:43
…al path safety guards

Performance:
- Replace `find ... -exec rm -f {} +` with `find ... -delete`
- Replace `find ... -exec rmdir {} +` with `find ... -depth -type d -empty -delete`
- `-delete` calls unlink(2) directly without forking per-batch rm/rmdir;
  on dirs with tens of thousands of files this is ~10x faster.

Safety guards:
- Add module-level `_DANGEROUS_PATHS` blacklist with 2 entries: "/" and
  "/tmp/miniforge". The list is intentionally minimal — both are observed
  incident sources (root wipe, uv runtime deletion). OS dirs like /etc /var
  are NOT included: they're hypothetical mistakes, and a longer blacklist
  raises the cost of changing the policy later.
- `FileCleanupTask.__init__` rejects target_dirs entries that match a
  blacklist entry (exact or subtree) -> ValueError at config-load time.
  Note: "/" is exact-match only — a naive subtree check would reject every
  absolute path.
- `TargetDirConfig.from_raw` rejects empty / relative / `..` paths. The ".."
  check happens BEFORE os.path.normpath, since normalize would collapse
  "/data/../etc" to "/etc" and silently accept the traversal.

Compat:
- yaml format unchanged (str + dict still both accepted).
- Behaviour for legitimate paths (/data/logs, /data/sandbox_logs etc.)
  unchanged — only the find action verb swapped.

Tests:
- New tests/unit/admin/scheduler/test_file_cleanup_task.py covers
  from_raw validation, blacklist guard (root exact-match, miniforge
  exact + subtree, /data/logs explicitly allowed), command generation
  (-delete present, -exec rm absent), from_config backward-compat,
  run_action happy + error paths. 31 tests, all passing.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@jinbai340997
jinbai340997 force-pushed the feat/file-cleanup-perf-and-guards branch from 5ee1550 to ad5359f Compare June 1, 2026 08:45
@jinbai340997
jinbai340997 merged commit a78e512 into alibaba:master Jun 3, 2026
6 of 7 checks passed
jinbai340997 added a commit that referenced this pull request Jun 3, 2026
…onsistency with blacklist terminology (PR #967 review)
jake11-oho pushed a commit to jake11-oho/ROCK that referenced this pull request Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] FileCleanupTask: switch to find -delete and add minimal path safety guards

2 participants