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>
This commit is contained in:
matt wilkie 2026-05-22 16:48:22 -07:00 • committed by GitHub
commit 458cf899c2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 105 additions and 6 deletions

View file

@ -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`)

View file

@ -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"

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)

59
scripts/clean-test-tmp.sh Executable file
View file

@ -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"