diff --git a/AGENT_INSTRUCTIONS.md b/AGENT_INSTRUCTIONS.md index 40497a6ca..3db983fff 100644 --- a/AGENT_INSTRUCTIONS.md +++ b/AGENT_INSTRUCTIONS.md @@ -63,6 +63,12 @@ into temp repos and produce flaky test behavior. **Warning:** bd will warn you when creating issues with "Test" prefix in the production database. Always use `BEADS_DB` for manual testing. +**Tmpfs hosts:** the `cmd/bd` test suite creates an isolated `$HOME` and several +test binaries under `$TMPDIR`. They are normally cleaned by the test process, +but a SIGKILLed or OOMed run can leave orphans behind. On hosts where `/tmp` +is tmpfs (e.g. Fedora Atomic / Bluefin), run `make clean-test-tmp` between +test runs if `du -sh /tmp/beads-* /tmp/bd-*` shows accumulation. See bd-3q2u. + ### Before Committing 1. **Run tests**: `make test` (or `./scripts/test.sh`) diff --git a/Makefile b/Makefile index 2676cc830..9ffe94a35 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ SHELL := $(subst cmd,bin,$(subst git.exe,bash.exe,$(GIT_BASH))) endif endif -.PHONY: all build test test-icu-path test-full-cgo test-regression test-upgrade test-cross-version test-migration bench bench-quick clean install install-force help check-up-to-date fmt fmt-check +.PHONY: all build test test-icu-path test-full-cgo test-regression test-upgrade test-cross-version test-migration bench bench-quick clean clean-test-tmp install install-force help check-up-to-date fmt fmt-check # Default target all: build @@ -197,6 +197,13 @@ clean: rm -f internal/storage/dolt/bench-cpu-*.prof rm -f beads-perf-*.prof +# Sweep orphaned cmd/bd test temp dirs (e.g. when a test run was SIGKILLed +# before its TestMain cleanup ran). Safe to run between test runs; will +# skip dirs in use by a live test process. See bd-3q2u. +clean-test-tmp: + @echo "Sweeping orphaned cmd/bd test temp dirs from $${TMPDIR:-/tmp}..." + @./scripts/clean-test-tmp.sh + # Show help help: @echo "Beads Makefile targets:" @@ -216,4 +223,5 @@ help: @echo " make fmt-check - Check Go formatting (for CI)" @echo " make check-docs - Validate docs against CLI flags" @echo " make clean - Remove build artifacts and profile files" + @echo " make clean-test-tmp - Sweep orphaned cmd/bd test temp dirs from \$$TMPDIR" @echo " make help - Show this help message" diff --git a/cmd/bd/explicit_db_nodb_test.go b/cmd/bd/explicit_db_nodb_test.go index ec71bfdd0..6336aee45 100644 --- a/cmd/bd/explicit_db_nodb_test.go +++ b/cmd/bd/explicit_db_nodb_test.go @@ -32,7 +32,7 @@ var ( func buildBDUnderTest(t *testing.T) string { t.Helper() buildBDOnce.Do(func() { - dir, err := os.MkdirTemp("", "bd-testbin-*") + dir, err := testTempDir("bd-testbin-*") if err != nil { buildBDErr = err return diff --git a/cmd/bd/init_embedded_test.go b/cmd/bd/init_embedded_test.go index 434006506..a11d1e8b1 100644 --- a/cmd/bd/init_embedded_test.go +++ b/cmd/bd/init_embedded_test.go @@ -42,7 +42,7 @@ func buildEmbeddedBD(t *testing.T) string { embeddedBD = prebuilt return } - tmpDir, err := os.MkdirTemp("", "bd-embedded-init-test-*") + tmpDir, err := testTempDir("bd-embedded-init-test-*") if err != nil { embeddedBDErr = fmt.Errorf("failed to create temp dir: %w", err) return diff --git a/cmd/bd/init_permissions_test.go b/cmd/bd/init_permissions_test.go index d6c94cc65..418a03f1c 100644 --- a/cmd/bd/init_permissions_test.go +++ b/cmd/bd/init_permissions_test.go @@ -22,7 +22,7 @@ var ( func buildBDForInitPermissionTests(t *testing.T) string { t.Helper() initPermissionTestBDOnce.Do(func() { - tmpDir, err := os.MkdirTemp("", "bd-init-permissions-test-*") + tmpDir, err := testTempDir("bd-init-permissions-test-*") if err != nil { initPermissionTestBDErr = fmt.Errorf("failed to create temp dir: %w", err) return diff --git a/cmd/bd/shared_server_integration_test.go b/cmd/bd/shared_server_integration_test.go index ffee1ca66..02505798c 100644 --- a/cmd/bd/shared_server_integration_test.go +++ b/cmd/bd/shared_server_integration_test.go @@ -613,7 +613,7 @@ func buildSharedServerTestBinary(t *testing.T) string { sharedServerBuildErr = fmt.Errorf("getwd: %w", err) return } - buildDir, err := os.MkdirTemp("", "beads-shared-server-bd-*") + buildDir, err := testTempDir("beads-shared-server-bd-*") if err != nil { sharedServerBuildErr = fmt.Errorf("mkdirtemp: %w", err) return diff --git a/cmd/bd/test_helpers_pure_test.go b/cmd/bd/test_helpers_pure_test.go index 0a24d44f7..bb2f40e51 100644 --- a/cmd/bd/test_helpers_pure_test.go +++ b/cmd/bd/test_helpers_pure_test.go @@ -279,7 +279,7 @@ func buildBDForInitTests(t *testing.T) string { return } // Fall back to building - tmpDir, err := os.MkdirTemp("", "bd-init-test-*") + tmpDir, err := testTempDir("bd-init-test-*") if err != nil { initTestBDErr = fmt.Errorf("failed to create temp dir: %w", err) return diff --git a/cmd/bd/test_repo_beads_guard_test.go b/cmd/bd/test_repo_beads_guard_test.go index dc7aef92c..ed376ba81 100644 --- a/cmd/bd/test_repo_beads_guard_test.go +++ b/cmd/bd/test_repo_beads_guard_test.go @@ -15,6 +15,26 @@ import ( // (e.g., starting a shared test Dolt server). Returns a cleanup function. var beforeTestsHook func() func() +// testTempRoot is the parent directory for per-process test temp dirs. +// It is set by testMainInner and used by the package-level sync.Once +// helpers (build binaries, isolated HOMEs) that previously called +// os.MkdirTemp("", ...) and leaked on every run. Anchoring those temp +// dirs under testTempRoot means the defer in testMainInner cleans them +// all up in one place (bd-3q2u / gastownhall/beads#4106). +// +// When tests run without TestMain (e.g. a single test invoked with the +// internal test binary directly), testTempRoot is empty and helpers +// fall back to os.TempDir(). +var testTempRoot string + +// testTempDir returns os.MkdirTemp under testTempRoot when it is set, +// otherwise it falls back to the system temp dir (os.MkdirTemp's +// default). Use this in package-level sync.Once builders so leaked +// directories get reaped by testMainInner's deferred cleanup. +func testTempDir(pattern string) (string, error) { + return os.MkdirTemp(testTempRoot, pattern) +} + // Guardrail: ensure the cmd/bd test suite does not touch the real repo .beads state. // Disable with BEADS_TEST_GUARD_DISABLE=1 (useful when running tests while actively using beads). func TestMain(m *testing.M) { @@ -36,6 +56,12 @@ func testMainInner(m *testing.M) int { } defer func() { _ = forceRemoveAll(tmp) }() + // Anchor package-level sync.Once builders (test binaries, isolated + // HOMEs) under this directory so the defer above sweeps them up too. + // Without this, those helpers leaked ~179MB-1.4GB per test run into + // /tmp and exhausted tmpfs over time (bd-3q2u). + testTempRoot = tmp + // Preserve Go build cache before changing HOME. // On macOS, GOCACHE defaults to $HOME/Library/Caches/go-build. // Changing HOME would cause tests that run `go build` (e.g., TestShow) diff --git a/scripts/clean-test-tmp.sh b/scripts/clean-test-tmp.sh new file mode 100755 index 000000000..02411938b --- /dev/null +++ b/scripts/clean-test-tmp.sh @@ -0,0 +1,59 @@ +#!/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"