Skip to content

Add clean_workdirs.sh script with dry-run mode and tests - #30

Open
jhshreya wants to merge 3 commits into
mission-kit:mainfrom
jhshreya:feat/clean-workdirs
Open

jhshreya wants to merge 3 commits into
mission-kit:mainfrom
jhshreya:feat/clean-workdirs

Conversation

@jhshreya

@jhshreya jhshreya commented Dec 4, 2026

Copy link
Copy Markdown

Fixes #29

Adds scripts/clean_workdirs.sh to prune old GitHub Actions (actions-runner-*/_work) and GitLab (builds) runner work directories safely:

  • Defaults to dry-run mode (printing candidate paths and sizes without deleting) and requires --apply to remove directories.
  • Supports configurable retention via --days N or CLEAN_WORKDIRS_DAYS (default 7 days).
  • Checks active runner pidfiles and locks before running so active jobs/runners are never disrupted.
  • Adds fake-rootfs unit tests in board/tests/test_clean_workdirs.py and updates README.md and docs/hosts.md.

@agent-kit-startup agent-kit-startup left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, the dry-run default and the integer check on --days look good. Deletion is limited to the direct children of actions-runner-*/_work and builds, find -type d does not follow symlinks, and a symlinked base yields nothing. Two things I'd fix before merging:

1. The deletion loop is unsafe with unusual directory names.

IFS='
'
for entry in $(find "$base" -mindepth 1 -maxdepth 1 -type d -mtime +"$DAYS" ...); do

The unquoted command substitution is split on newlines and also goes through pathname expansion. A directory named x<newline>/some/other/path becomes two words, and the second is passed to rm -rf. A directory named * expands to the contents of the current directory. Jobs run as the same user and can create directories under _work, so with --apply this can remove paths outside the work dirs. It is not a privilege escalation, but it is a wrong delete. Suggested shape:

export APPLY
find "$base" -mindepth 1 -maxdepth 1 -type d -mtime +"$DAYS" -exec sh -c '
  for e; do
    s=$(du -sh "$e" 2>/dev/null | cut -f1)
    if [ "$APPLY" -eq 1 ]; then rm -rf -- "$e" && echo "removed ${s:-0} $e"
    else echo "would remove ${s:-0} $e"; fi
  done' sh {} +

(Alternatively set -f and skip any entry containing a newline.) A test with a newline and a * in a directory name would cover it.

2. The active-job check runs only once.

check_no_job_running runs before the loop, and no lock is held while deleting. If a runner picks up a job during the prune, its workspace can be removed under it. Holding runner.lock / gh-runners.lock for the duration, or re-checking before each rm, would close that window.

Minor:

  • -mtime on a top-level directory such as _work/<repo> only changes when its direct entries change, so a repo built daily can still look older than N days and be pruned. It is harmless (it gets cloned again), but the README wording "older than N days" overstates the guarantee.
  • Without a proot rootfs, the defaults target /root/actions-runner-*/_work and /root/builds on the host. Probably intended, but worth a line in docs/hosts.md.

@agent-kit-startup

Copy link
Copy Markdown
Contributor

Two corrections to my examples in point 1, the conclusion stands:

  • A directory name cannot contain "/", so the newline case yields a bare second word (e.g. "rest"), which rm then resolves against the current directory.
  • A directory named "" makes the word "/", which expands to every child of the work dir, recent ones included. So the real damage is deleting fresh work dirs.

Also: the issue asked for scripts/clean-workdirs.sh (hyphen, like the other scripts), and the snippet I suggested drops the "nothing older than N days" line your test checks, so keep that count.

@jhshreya

jhshreya commented Dec 5, 2026

Copy link
Copy Markdown
Author

Thanks for the detailed review! I've pushed commit aa97567 to switch the pruning loop to find ... -exec (avoiding word-splitting and pathname expansion on unusual directory names) and re-check active runner pidfiles and locks before each removal while keeping the "nothing older than N days" output.

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.

Script to clean old runner work directories, with a dry run

2 participants