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>
90 lines
2.2 KiB
Go
90 lines
2.2 KiB
Go
package proc
|
|
|
|
import (
|
|
"os/exec"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// A Tree is a started command and the processes it starts in turn: an
|
|
// agent's CLI with the MCP servers, shells and helpers it runs. Killing the
|
|
// CLI alone leaves those running, so they are ended with it.
|
|
type Tree struct {
|
|
cmd *exec.Cmd
|
|
|
|
mu sync.Mutex
|
|
done bool // waited for: its pid may be another process's now
|
|
waited chan struct{} // closed once Wait has
|
|
stopped chan struct{} // Stop's, closed once it has ended the tree
|
|
group
|
|
}
|
|
|
|
// StartTree starts cmd in a group of its own: a session on Unix, a job on
|
|
// Windows. It has no terminal then, and a Ctrl+C there doesn't reach it.
|
|
func StartTree(cmd *exec.Cmd) (*Tree, error) {
|
|
ownGroup(cmd)
|
|
if err := cmd.Start(); err != nil {
|
|
return nil, err
|
|
}
|
|
t := &Tree{cmd: cmd, waited: make(chan struct{})}
|
|
t.join(cmd)
|
|
return t, nil
|
|
}
|
|
|
|
// Wait waits for the command, then ends what it left running.
|
|
func (t *Tree) Wait() error {
|
|
err := t.cmd.Wait()
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
if !t.done {
|
|
t.done = true
|
|
close(t.waited)
|
|
}
|
|
t.sweep(t.cmd)
|
|
return err
|
|
}
|
|
|
|
// Kill ends the command and every process of its tree.
|
|
func (t *Tree) Kill() {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
if t.done {
|
|
return
|
|
}
|
|
_ = t.kill(t.cmd)
|
|
_ = t.cmd.Process.Kill()
|
|
}
|
|
|
|
// Stop asks the command to end, so it can clean up first, and ends its tree
|
|
// once it has (Wait sweeps what it left) or grace has passed (Kill). Claude
|
|
// Code holding its OAuth refresh lock removes it as it exits on SIGTERM, and
|
|
// a SIGKILL left ~/.claude/.oauth_refresh.lock behind for every later
|
|
// request to wait on (#2195). Windows has no such signal: the tree is killed
|
|
// at once there. It returns at once; the channel closes once the tree is
|
|
// ended. A tree no one Waits for is killed after grace.
|
|
func (t *Tree) Stop(grace time.Duration) <-chan struct{} {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
if t.stopped != nil {
|
|
return t.stopped
|
|
}
|
|
t.stopped = make(chan struct{})
|
|
if t.done || t.term(t.cmd) != nil {
|
|
if !t.done {
|
|
_ = t.kill(t.cmd)
|
|
_ = t.cmd.Process.Kill()
|
|
}
|
|
close(t.stopped)
|
|
return t.stopped
|
|
}
|
|
go func() {
|
|
defer close(t.stopped)
|
|
select {
|
|
case <-t.waited:
|
|
case <-time.After(grace):
|
|
t.Kill()
|
|
}
|
|
}()
|
|
return t.stopped
|
|
}
|