Files
magpie/internal/proc/tree_other_test.go
T
yetoneandXIQIXIQIXIQI b789bcc45d gateway, proc: magpie ends a Claude Code run with SIGTERM and a 5s grace before the kill, so its OAuth refresh lock is let go (#2195, XIQIXIQIXIQI)
subscriptionRun.abort killed Claude Code's process group with SIGKILL at
once. A Claude Code holding ~/.claude/.oauth_refresh.lock at that moment
can't run its exit cleanup, and the lock was left for the next requests to
fail on ("another Claude Code process is refreshing it or exited
mid-refresh").

proc.Tree.Stop(grace) sends SIGTERM to the leader alone, then ends the
tree once it has exited (Wait sweeps the group) or grace has passed
(Kill). A second Stop shares the first's grace. Windows has no such
signal and kills the job at once. abort uses it with stopGrace (5s); the
run still counts as ended at once, and abortAll, as the gateway stops,
waits for every tree. Other agents' trees keep Kill.

Run (macOS, sandbox HOME, pinned caches):
- the real Claude Code 2.1.299 (-p, stream-json in/out, as the bridge
  starts it) under a sandbox HOME/CLAUDE_CONFIG_DIR, no credentials:
  SIGTERM after 4s, it exited in 0.497s with 143.
- go test -race -count=20 -run TestTree ./internal/proc: ok
- go test -race -count=5 -run TestClaudeAbortAllowsLockCleanup
  ./internal/gateway (the reporter's test, trap set before the lock is
  made): ok
- broken by hand: abort back to t.Kill() -> TestClaudeAbortAllowsLockCleanup
  FAIL "Claude abort left refresh lock: <nil>"; Stop killing at once ->
  TestTreeStopLetsItCleanUp FAIL "the command's lock is left after Stop".
- on b9c589ebb plus these commits: gofmt -l (nothing), go vet ./... on
  darwin, GOOS=linux -tags nogui and GOOS=windows, go test -tags nogui ./...:
  every package ok but two filed load flakes, TestMaxRPMWaitsForRoomInTheMinute
  (#1791) and TestCopilotSecretToolUsername (#1627), on a machine at load
  ~200; each passed -count=3 alone.
- rebased onto e98440ae6 (27 more commits on main) and
  run again there: gofmt -l, go vet ./... (darwin, linux nogui, windows),
  go test -tags nogui ./internal/gateway ./internal/proc ./internal/agent.
- then onto af27b70b4 (9 commits; gateway routing.go/gateway.go and agent
  codex.go touched, not these lines): gofmt -l, go vet on the three OSes,
  and the tests named above plus the new remote-magpie and Codex ones.
Not tried: a real OAuth refresh interrupted mid-way (no Claude request was
made); Linux and Windows at runtime.

Co-authored-by: XIQIXIQIXIQI <75725040+XIQIXIQIXIQI@users.noreply.github.com>
2026-10-15 01:22:13 +08:00

120 lines
3.4 KiB
Go

//go:build !windows
package proc
import (
"bufio"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
"testing"
"time"
)
// startSh runs script in a tree and gives the pid its first line names.
func startSh(t *testing.T, script string) (*Tree, int) {
t.Helper()
cmd := Command("/bin/sh", "-c", script)
out, err := cmd.StdoutPipe()
if err != nil {
t.Fatal(err)
}
tree, err := StartTree(cmd)
if err != nil {
t.Fatal(err)
}
line, _ := bufio.NewReader(out).ReadString('\n')
pid, err := strconv.Atoi(strings.TrimSpace(line))
if err != nil {
t.Fatalf("no pid from the script: %q", line)
}
t.Cleanup(func() { _ = syscall.Kill(pid, syscall.SIGKILL) })
return tree, pid
}
func gone(pid int) bool {
for end := time.Now().Add(3 * time.Second); time.Now().Before(end); time.Sleep(20 * time.Millisecond) {
if syscall.Kill(pid, 0) != nil {
return true
}
}
return false
}
// Killing a command ends what it started, not only the command.
func TestTreeKillEndsChildren(t *testing.T) {
tree, child := startSh(t, "sleep 600 >/dev/null 2>&1 & echo $!; wait")
done := make(chan struct{})
go func() { _ = tree.Wait(); close(done) }()
tree.Kill()
select {
case <-done:
case <-time.After(3 * time.Second):
t.Fatal("the command outlived Kill")
}
if !gone(child) {
t.Fatalf("its child %d outlived Kill", child)
}
tree.Kill() // once waited for, a no-op
}
// A command that ends by itself takes what it left running with it.
func TestTreeWaitEndsWhatWasLeft(t *testing.T) {
tree, child := startSh(t, "sleep 600 >/dev/null 2>&1 & echo $!")
if err := tree.Wait(); err != nil {
t.Fatal(err)
}
if !gone(child) {
t.Fatalf("its child %d outlived it", child)
}
}
// Stop lets the command clean up before its tree ends: Claude Code removes
// the OAuth refresh lock it holds on SIGTERM, which a SIGKILL left behind
// (#2195). What it started still ends with it.
func TestTreeStopLetsItCleanUp(t *testing.T) {
lock := filepath.Join(t.TempDir(), "refresh.lock")
tree, child := startSh(t, `sleep 600 >/dev/null 2>&1 & echo $!; trap 'rmdir "`+lock+`"; exit 0' TERM; mkdir "`+lock+`"; while :; do sleep 0.02; done`)
for end := time.Now().Add(3 * time.Second); ; time.Sleep(10 * time.Millisecond) {
if _, err := os.Stat(lock); err == nil {
break
} else if time.Now().After(end) {
t.Fatal("the lock was never made")
}
}
go func() { _ = tree.Wait() }()
select {
case <-tree.Stop(time.Minute):
case <-time.After(10 * time.Second):
t.Fatal("Stop never ended a command that exits on SIGTERM")
}
if _, err := os.Stat(lock); !os.IsNotExist(err) {
t.Fatalf("the command's lock is left after Stop: %v", err)
}
if !gone(child) {
t.Fatalf("its child %d outlived Stop", child)
}
}
// A command that doesn't end when asked is killed, tree and all, once the
// grace has passed; a second Stop shares the first's.
func TestTreeStopKillsOneThatStays(t *testing.T) {
tree, child := startSh(t, `sleep 600 >/dev/null 2>&1 & echo $!; trap '' TERM; while :; do sleep 0.02; done`)
go func() { _ = tree.Wait() }()
first := tree.Stop(50 * time.Millisecond)
if second := tree.Stop(time.Hour); second != first {
t.Fatal("a second Stop started a grace of its own")
}
select {
case <-first:
case <-time.After(10 * time.Second):
t.Fatal("Stop never killed a command that ignores SIGTERM")
}
if !gone(child) {
t.Fatalf("its child %d outlived Stop", child)
}
tree.Kill() // ended already: a no-op
}