Files
gastownhall__beads/scripts/clean-test-tmp.sh
matt wilkie 458cf899c2 test(cmd/bd): route sync.Once test temp dirs through TestMain cleanup (#4108)
Five package-level sync.Once builders in cmd/bd/ called
os.MkdirTemp("", ...) with no corresponding cleanup, leaking
~179MB-1.4GB of test artifacts per run into $TMPDIR. On tmpfs
hosts (e.g. Fedora Atomic /tmp = 50% of RAM) this exhausted /tmp
after a few runs. Leaking sites:

  - test_repo_beads_guard_test.go   beads-bd-tests-*
  - test_helpers_pure_test.go       bd-init-test-*
  - init_embedded_test.go           bd-embedded-init-test-*
  - shared_server_integration_test  beads-shared-server-bd-*
  - init_permissions_test.go        bd-init-permissions-test-*
  - explicit_db_nodb_test.go        bd-testbin-*

Fix: anchor all six temp dirs under the existing TestMain tmp
parent via a new testTempDir helper. The defer in testMainInner
already calls forceRemoveAll on that parent (chmod-tolerant for
Go modcache 0444 files), so all six get cleaned in one place.

Also adds 'make clean-test-tmp' (scripts/clean-test-tmp.sh) for
sweeping orphans left by SIGKILLed test runs, and documents the
tmpfs caveat in AGENT_INSTRUCTIONS.md.

Fixes gastownhall/beads#4106 (bd-3q2u)

_amp-unknown-model-unknown-reasoning on behalf of matt wilkie_

Amp-Thread-ID: https://ampcode.com/threads/T-019e512d-edf1-72e3-a252-6328e63bff3f

Co-authored-by: Amp <amp@ampcode.com>
2026-05-22 16:48:22 -07:00

60 lines
2.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# clean-test-tmp.sh — sweep orphaned cmd/bd test temp dirs.
#
# The cmd/bd test suite creates several MkdirTemp parents under $TMPDIR
# (HOME isolation, built test binaries, etc.). They are normally cleaned
# by testMainInner's defer, but a SIGKILLed / OOMed test run leaves them
# behind. On tmpfs hosts (e.g. Bluefin's 20GB /tmp) these can grow to
# several GB and exhaust /tmp.
#
# This script removes orphaned dirs matching the known patterns. It is
# safe to run between test invocations. Dirs whose lockfile / mtime
# suggests an in-progress run are skipped.
#
# See bd-3q2u / gastownhall/beads#4106.
set -euo pipefail
tmpdir="${TMPDIR:-/tmp}"
# Patterns created by cmd/bd test helpers (see test_repo_beads_guard_test.go
# and the package-level sync.Once builders).
patterns=(
"beads-bd-tests-*"
"beads-shared-server-bd-*"
"bd-testbin-*"
"bd-init-test-*"
"bd-init-permissions-test-*"
"bd-embedded-init-test-*"
)
# Only sweep dirs older than this many minutes, so we don't blow away
# a test run that's mid-flight in another worktree.
min_age_min="${BEADS_CLEAN_TEST_TMP_MIN_AGE:-30}"
removed=0
skipped=0
total_bytes=0
for pat in "${patterns[@]}"; do
# -mindepth/-maxdepth 1 so we only match top-level entries.
# -mmin +N skips dirs newer than N minutes.
while IFS= read -r -d '' dir; do
# Size for reporting (du in KB; cheap on tmpfs).
sz_kb=$(du -sk "$dir" 2>/dev/null | awk '{print $1}' || echo 0)
# chmod -R u+w so we can remove read-only Go modcache files.
chmod -R u+w "$dir" 2>/dev/null || true
if rm -rf "$dir" 2>/dev/null; then
removed=$((removed + 1))
total_bytes=$((total_bytes + sz_kb))
printf ' removed %s (%s KB)\n' "$dir" "$sz_kb"
else
skipped=$((skipped + 1))
printf ' skipped %s (rm failed; may be in use)\n' "$dir" >&2
fi
done < <(find "$tmpdir" -mindepth 1 -maxdepth 1 -type d -name "$pat" -mmin "+$min_age_min" -print0 2>/dev/null)
done
printf 'Removed %d orphaned dir(s), reclaimed ~%d MB. Skipped %d.\n' \
"$removed" "$((total_bytes / 1024))" "$skipped"