test(hooks): bound the long-running bd stub so a killed run cannot leak a spinner (wy-nzeocg) (#6612)
The real-GNU-timeout and real-Perl-alarm expiry subtests stand in a bd that never returns with a bare `while :; do :; done`. On a normal run the helper reaps it at 1s. When the go test process is killed mid-subtest, though, the helper has already setpgid()d itself and the stub out of the harness's group, the tree-kill reaches the helper by pid before its child, and the orphan is reparented to 1 and spins at 100% CPU forever: one such stub was found 2.5h old on a shared build box after a session teardown. The stub now paces itself with sleep and exits 97 on its own after 60 ticks, so a killed run leaks at most a minute of idle sleep. The generated hook runs under a controlled PATH that carries only the fixtures and the helper under test, so the test resolves sleep once (findHookProcessSleep, the same shape as the Perl probe) and hands it to the stub as HOOK_TEST_SLEEP by absolute path. The sleep's stdio is detached because Perl's alarm signals only the exec'd stub, not a group, and the orphaned sleep must not hold the test's output pipe open past exec.Cmd.WaitDelay. Exit 97 is outside every status the hook normalizes, so a stub that outlives the helper still fails the expiry assertions loudly. Tests: go test ./cmd/bd -run TestGeneratedHookTimeoutProcessBoundary -count=1 (twice, darwin/arm64, bash 5 + coreutils gtimeout + perl); go vet ./cmd/bd. Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
9a91f0629e
commit
f6d9245624
1 changed files with 53 additions and 8 deletions
|
|
@ -24,9 +24,24 @@ done
|
|||
exit "${HOOK_TEST_BD_EXIT:-0}"
|
||||
`
|
||||
|
||||
// hookProcessLongRunningBDStub stands in for a bd that never returns, so the
|
||||
// expiry subtests can prove the timeout helper reaps it. It sleeps instead of
|
||||
// busy-looping and bounds itself: when a test run is killed before the helper
|
||||
// fires (the harness reaches the helper by pid before its child), the orphan
|
||||
// idles for at most a minute and exits on its own instead of spinning at 100%
|
||||
// CPU forever with ppid 1. Exit 97 is outside every code the hook normalizes,
|
||||
// so a run that outlives the helper still fails the test. The sleep runs with
|
||||
// its stdio detached: when the helper signals only the stub (Perl's alarm hits
|
||||
// the exec'd process, not a group), the orphaned sleep must not keep the test's
|
||||
// output pipe open past exec.Cmd.WaitDelay.
|
||||
const hookProcessLongRunningBDStub = `#!/bin/sh
|
||||
printf 'long-running-bd-started\n'
|
||||
while :; do :; done
|
||||
_bd_test_tick=0
|
||||
while [ "$_bd_test_tick" -lt 60 ]; do
|
||||
"${HOOK_TEST_SLEEP:?}" 1 </dev/null >/dev/null 2>&1
|
||||
_bd_test_tick=$((_bd_test_tick + 1))
|
||||
done
|
||||
exit 97
|
||||
`
|
||||
|
||||
const hookProcessGNUTimeoutStub = `#!/bin/sh
|
||||
|
|
@ -113,6 +128,7 @@ type hookProcessCase struct {
|
|||
usePOSIXSh bool
|
||||
timeout *string
|
||||
pathTail string
|
||||
sleepPath string
|
||||
args []string
|
||||
}
|
||||
|
||||
|
|
@ -343,9 +359,10 @@ func testHookProcessRealTimeoutExpiry(t *testing.T) {
|
|||
helperName, helperDir := findHookProcessCoreutilsTimeout(t)
|
||||
timeout := "1"
|
||||
result := runGeneratedHookProcess(t, hookProcessCase{
|
||||
bdBody: hookProcessLongRunningBDStub,
|
||||
timeout: &timeout,
|
||||
pathTail: helperDir,
|
||||
bdBody: hookProcessLongRunningBDStub,
|
||||
timeout: &timeout,
|
||||
pathTail: helperDir,
|
||||
sleepPath: findHookProcessSleep(t),
|
||||
})
|
||||
if result.exitCode != 0 {
|
||||
t.Fatalf("generated hook exit = %d, want normalized timeout success\n%s", result.exitCode, result.output)
|
||||
|
|
@ -370,9 +387,10 @@ func testHookProcessRealPerlExpiry(t *testing.T) {
|
|||
{name: "timeout", body: hookProcessIncompatibleTimeoutStub},
|
||||
{name: "gtimeout", body: hookProcessIncompatibleGtimeoutStub},
|
||||
},
|
||||
bdBody: hookProcessLongRunningBDStub,
|
||||
timeout: &timeout,
|
||||
pathTail: perlDir,
|
||||
bdBody: hookProcessLongRunningBDStub,
|
||||
timeout: &timeout,
|
||||
pathTail: perlDir,
|
||||
sleepPath: findHookProcessSleep(t),
|
||||
})
|
||||
if result.exitCode != 0 {
|
||||
t.Fatalf("generated hook exit = %d, want normalized Perl alarm success\n%s", result.exitCode, result.output)
|
||||
|
|
@ -514,6 +532,9 @@ shift
|
|||
if tc.timeout != nil {
|
||||
cmd.Env = append(cmd.Env, "BEADS_HOOK_TIMEOUT="+*tc.timeout)
|
||||
}
|
||||
if tc.sleepPath != "" {
|
||||
cmd.Env = append(cmd.Env, "HOOK_TEST_SLEEP="+tc.sleepPath)
|
||||
}
|
||||
started := time.Now()
|
||||
output, err := cmd.CombinedOutput()
|
||||
elapsed := time.Since(started)
|
||||
|
|
@ -585,6 +606,30 @@ printf '%s\n' "${_bd_test_path%/*}"
|
|||
return dir
|
||||
}
|
||||
|
||||
// findHookProcessSleep resolves the sleep the long-running bd stub paces itself
|
||||
// with. The generated hook runs under a controlled PATH that carries only the
|
||||
// fixtures and the helper under test, so the stub is handed sleep by absolute
|
||||
// path (in the shell's own spelling, which on Git Bash is the MSYS form).
|
||||
func findHookProcessSleep(t *testing.T) string {
|
||||
t.Helper()
|
||||
probe := `
|
||||
_bd_test_path=$(command -v sleep) || exit 1
|
||||
"$_bd_test_path" 0 || exit 1
|
||||
printf '%s\n' "$_bd_test_path"
|
||||
`
|
||||
cmd := exec.Command(hookProcessShell(t), "--noprofile", "--norc", "-c", probe)
|
||||
cmd.Env = hookProcessEnv()
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("sleep is required to pace the long-running bd stub: %s", strings.TrimSpace(string(output)))
|
||||
}
|
||||
path := strings.TrimSpace(string(output))
|
||||
if path == "" {
|
||||
t.Fatalf("unexpected sleep probe output: %q", string(output))
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func writeHookProcessFixture(t *testing.T, dir, name, body string) {
|
||||
t.Helper()
|
||||
path := filepath.Join(dir, name)
|
||||
|
|
@ -686,7 +731,7 @@ func hookProcessEnv() []string {
|
|||
}
|
||||
upper := strings.ToUpper(key)
|
||||
if upper == "BASH_ENV" || upper == "BASHOPTS" || upper == "ENV" || upper == "SHELLOPTS" ||
|
||||
upper == "BEADS_HOOK_TIMEOUT" || upper == "HOOK_TEST_BD_EXIT" {
|
||||
upper == "BEADS_HOOK_TIMEOUT" || upper == "HOOK_TEST_BD_EXIT" || upper == "HOOK_TEST_SLEEP" {
|
||||
continue
|
||||
}
|
||||
env = append(env, entry)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue