Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
MUL-5241 fix(agent): bound the hermes shutdown so an escaped pipe holder can't wedge a turn (#5878)
Bohan Jiangbf7e6e50e732
diff --git a/server/pkg/agent/hermes.go b/server/pkg/agent/hermes.goindex 82de0fb1e6..a9f824804e 100644--- a/server/pkg/agent/hermes.go+++ b/server/pkg/agent/hermes.go@@ -290,6 +290,14 @@ func (b *hermesBackend) Execute(ctx context.Context, prompt string, opts ExecOpthermesArgs := hermesCLIArgs(opts.CustomArgs, b.cfg.Logger)cmd := b.cfg.commandAt(execPath).exec(runCtx, hermesArgs...)hideAgentWindow(cmd)+ // What makes the shutdown below bounded. Wait waits on the direct child, and+ // a child that ignores the cancel would otherwise hold it forever; with a+ // WaitDelay, a cancelled context makes Wait kill and reap within it. Wait+ // returning is also what closes the parent ends of the pipes, which is the+ // step that frees a reader an escaped descendant is holding — so bounding+ // Wait is what lets the forced shutdown join its readers at all. Same 10s+ // the claude, codearts and antigravity backends use.+ cmd.WaitDelay = 10 * time.Secondb.cfg.logAgentCommand(cmd, newAgentCommandLogArgs(hermesArgs, trustAgentCommandPositional(0, hermesACPSubcommand)))agentsMDPresent := falseif opts.Cwd != "" {@@ -427,6 +435,23 @@ func (b *hermesBackend) Execute(ctx context.Context, prompt string, opts ExecOptc.closeAllPending(fmt.Errorf("hermes process exited"))}()+ // reapProcess runs cmd.Wait() — which may only be called once — and returns+ // when it has. Wait is what closes the parent ends of the stdout and stderr+ // pipes, so it is also the only way to free a reader blocked on a pipe that+ // a descendant outside the process group is still holding. Both the forced+ // shutdown below and the deferred cleanup need it, in that order.+ var waitOnce sync.Once+ waitDone := make(chan struct{})+ reapProcess := func() {+ waitOnce.Do(func() {+ go func() {+ defer close(waitDone)+ _ = cmd.Wait()+ }()+ })+ <-waitDone+ }+// Drive the ACP session lifecycle in a goroutine.go func() {defer close(msgCh)@@ -438,7 +463,13 @@ func (b *hermesBackend) Execute(ctx context.Context, prompt string, opts ExecOpt// process alive; waiting first would then block until the overall// task timeout and make a later deferred cancel ineffective.cancel()- _ = cmd.Wait()+ reapProcess()+ // Wait has closed the pipes, so both readers are now guaranteed to+ // reach EOF and return. Join them before the enclosing goroutine+ // returns and closes msgCh: a reader that outlived that close would+ // panic sending on it.+ <-readerDone+ <-stderrDonereleaseProcessGroup(cmd)}()@@ -736,7 +767,23 @@ func (b *hermesBackend) Execute(ctx context.Context, prompt string, opts ExecOpt"pid", cmd.Process.Pid,"grace", hermesReaderDrainGrace.String(),)+ // Cancel kills the owned process tree, so every descendant in it+ // releases the pipes and both readers reach EOF. A descendant+ // outside that tree does not get the signal: on POSIX because it+ // called setsid and left the process group, on Windows because+ // startOwnedProcessTree failed open and the child runs unowned, so+ // the kill reaches the leader alone. Joining the readers is then an+ // unbounded wait — the turn hangs with no result until the user+ // cancels by hand, which is the MUL-5241 report.cancel()+ // Reap here rather than leaving it to the deferred cleanup. Wait+ // closes the pipes, which is what frees a reader the kill could not+ // reach, and cmd.WaitDelay bounds Wait itself now that the context+ // is cancelled. Both joins below therefore terminate, and they still+ // run before the buffers are read: providerErr.Finalize requires a+ // drained stderr pipe, and it is not safe to call while the copier+ // can still write.+ reapProcess()<-readerDone<-stderrDone}diff --git a/server/pkg/agent/hermes_escaped_holder_unix_test.go b/server/pkg/agent/hermes_escaped_holder_unix_test.gonew file mode 100644index 0000000000..597d4c6bfa--- /dev/null+++ b/server/pkg/agent/hermes_escaped_holder_unix_test.go@@ -0,0 +1,248 @@+//go:build unix++package agent++import (+ "context"+ "fmt"+ "log/slog"+ "os"+ "path/filepath"+ "strings"+ "syscall"+ "testing"+ "time"+)++// hermesEscapedHolderEnv carries the path the re-executed helper records its+// identity in. Empty means this process is the ordinary test run.+const hermesEscapedHolderEnv = "MULTICA_FAKE_HERMES_ESCAPED_HOLDER"++// hermesEscapedHolderBound is what the daemon's shutdown may cost: the drain+// grace, then a forced shutdown whose own cost is one cmd.Wait() on an+// already-exited process, plus slack for a loaded CI runner.+var hermesEscapedHolderBound = hermesReaderDrainGrace + 6*time.Second++// hermesEscapedHolderReady covers everything between Execute returning and the+// holder's identity being readable: the protocol advancing to the case that+// starts it, then a process spawn and a Go runtime start, since the holder is a+// re-executed test binary. On a loaded runner that is seconds rather than+// milliseconds. The fake agent and the test wait on the same budget on purpose:+// with the test's the shorter of the two, it can give up while the agent is+// still legitimately waiting, and the run fails with a missing state file+// instead of an assertion about the daemon.+var hermesEscapedHolderReady = hermesEscapedHolderBound++// hermesEscapedHolderLifetime outlives every wait these tests make — the ready+// budget and then the shutdown bound, twice over — so a passing run can only+// mean the daemon stopped waiting for the holder, never that the holder+// happened to exit first. The fixture kills it long before this elapses.+var hermesEscapedHolderLifetime = 2 * (hermesEscapedHolderReady + hermesEscapedHolderBound)++// TestHermesEscapedPipeHolderHelper is the fixture's pipe holder, not a test of+// its own: the tests below re-execute this binary so the child can call+// setsid(2) and leave the process group the daemon kills, the one thing a shell+// fixture cannot express portably. It inherits the fake agent's stdout and+// stderr — the daemon's read pipes — and keeps them open while it sleeps.+func TestHermesEscapedPipeHolderHelper(t *testing.T) {+ statePath := os.Getenv(hermesEscapedHolderEnv)+ if statePath == "" {+ t.Skip("pipe holder for the hermes escaped-descendant tests")+ }+ // Without its own session this process would die with the group and the+ // tests would pass for the wrong reason, so leave no state file behind:+ // they fail on its absence.+ if _, err := syscall.Setsid(); err != nil {+ t.Fatalf("setsid: %v", err)+ }+ if err := os.WriteFile(statePath, []byte(fmt.Sprintf("%d %d", os.Getpid(), syscall.Getpgrp())), 0o600); err != nil {+ t.Fatalf("record holder identity: %v", err)+ }+ time.Sleep(hermesEscapedHolderLifetime)+}++// TestHermesBackendReportsTurnWhenEscapedDescendantHoldsPipes covers the case+// owning the process tree cannot: the agent leaves a descendant that inherited+// stdout/stderr and then left the process group, so the group-wide kill in+// cmd.Cancel never reaches it and the daemon's readers never see EOF. Joining+// them unconditionally wedged the turn until the user cancelled it by hand,+// which is the MUL-5241 report. This fixture is the POSIX form of that — a+// setsid descendant; on Windows the equivalent is a child running unowned+// because startOwnedProcessTree failed open, since the Job Object this daemon+// creates does not permit breakaway.+//+// The turn must reach a terminal Result, and the session's channels must close,+// within the test's shutdown bound — itself well inside the 10s cmd.WaitDelay+// that makes the forced shutdown terminate at all. The escaped process itself+// is not reaped — that is what escaping means — which is why the fixture kills+// the holder itself.+//+// The sibling TestHermesBackendCancelsBeforeWaitingForLingeringProcess does not+// reach this: its lingering process closes the inherited descriptors first, so+// the readers get their EOF.+func TestHermesBackendReportsTurnWhenEscapedDescendantHoldsPipes(t *testing.T) {+ // The turn completes normally; only the shutdown has to cope with the+ // holder.+ session, messagesClosed := startHermesWithEscapedHolder(t, `+ *'"method":"initialize"'*)+ printf '{"jsonrpc":"2.0","id":%%s,"result":{"protocolVersion":1,"agentCapabilities":{}}}\n' "$id"+ ;;+ *'"method":"session/new"'*)+ printf '{"jsonrpc":"2.0","id":%%s,"result":{"sessionId":"ses_escaped_holder"}}\n' "$id"+ ;;+ *'"method":"session/prompt"'*)+ %s+ printf '{"jsonrpc":"2.0","id":%%s,"result":{"stopReason":"end_turn"}}\n' "$id"+ exit 0+ ;;`)++ result := awaitHermesResult(t, session)+ if result.Status != "completed" {+ t.Fatalf("status: got %q (error %q), want %q", result.Status, result.Error, "completed")+ }+ awaitHermesMessagesClosed(t, messagesClosed)+}++// TestHermesBackendClosesSessionWhenEarlyFailureLeavesEscapedHolder covers the+// other side of that shutdown: a handshake failure returns from the lifecycle+// goroutine before the drain runs at all, so the deferred cleanup is the only+// thing that closes the pipes and joins the readers.+//+// This is a contract test, not a regression: it passes on the unfixed backend+// too, because Messages gets closed either way. What it pins is that the early+// path still reaches a terminal Result and closes the session within the same+// bound. The join it sits next to guards something a test cannot observe+// reliably — a reader still live when the goroutine closes msgCh panics in+// trySend — so the assertion here is the closure, not the join.+func TestHermesBackendClosesSessionWhenEarlyFailureLeavesEscapedHolder(t *testing.T) {+ session, messagesClosed := startHermesWithEscapedHolder(t, `+ *'"method":"initialize"'*)+ %s+ printf '{"jsonrpc":"2.0","id":%%s,"error":{"code":-32603,"message":"initialize refused"}}\n' "$id"+ exit 0+ ;;`)++ result := awaitHermesResult(t, session)+ if result.Status != "failed" {+ t.Fatalf("status: got %q (error %q), want %q", result.Status, result.Error, "failed")+ }+ awaitHermesMessagesClosed(t, messagesClosed)+}++// startHermesWithEscapedHolder runs the hermes backend against a fake agent+// built from cases — the bodies of a `case "$line" in` over incoming JSON-RPC,+// with one %s where the holder is started. It returns the session and a channel+// that closes when Messages does, having already confirmed the holder really+// left the agent's process group.+func startHermesWithEscapedHolder(t *testing.T, cases string) (*Session, <-chan struct{}) {+ t.Helper()++ testBinary, err := os.Executable()+ if err != nil {+ t.Fatalf("locate test binary: %v", err)+ }+ dir := t.TempDir()+ statePath := filepath.Join(dir, "holder-state")+ fakePath := filepath.Join(dir, "hermes")+ // The holder is started without redirections on purpose: inheriting fd 1+ // and 2 is what keeps the daemon's pipes open after their writer is gone.+ //+ // The agent waits for it to record its identity — which it does once it has+ // left the process group — before answering at all. Without that wait the+ // daemon can kill the group first, as it does the moment a handshake fails,+ // and the holder dies as an ordinary member before it ever escapes. The wait+ // is bounded by hermesEscapedHolderReady, the same budget the test gives it,+ // so a broken holder fails the test rather than hanging it.+ spawnHolder := fmt.Sprintf(`%s=%q %q -test.run='^TestHermesEscapedPipeHolderHelper$' &+ waited=0+ while [ ! -f %[2]q ] && [ $waited -lt %[4]d ]; do sleep 0.05; waited=$((waited+1)); done`,+ hermesEscapedHolderEnv, statePath, testBinary, hermesEscapedHolderReady/(50*time.Millisecond))+ script := fmt.Sprintf(`#!/bin/sh+while IFS= read -r line; do+ id=$(printf '%%s' "$line" | sed -n 's/.*"id":\([0-9]*\).*/\1/p')+ case "$line" in`+cases+`+ esac+done+`, spawnHolder)+ writeTestExecutable(t, fakePath, []byte(script))++ backend, err := New("hermes", Config{ExecutablePath: fakePath, Logger: slog.Default()})+ if err != nil {+ t.Fatalf("new hermes backend: %v", err)+ }+ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)+ t.Cleanup(cancel)+ session, err := backend.Execute(ctx, "prompt", ExecOptions{Timeout: 25 * time.Second})+ if err != nil {+ t.Fatalf("execute: %v", err)+ }+ messagesClosed := make(chan struct{})+ go func() {+ defer close(messagesClosed)+ for range session.Messages {+ }+ }()++ // Read the holder before waiting on anything it can outlast, so a failing+ // run kills it on the way out instead of leaving it to time out on its own.+ pid, pgid := readEscapedHolderIdentity(t, statePath)+ t.Cleanup(func() { _ = syscall.Kill(pid, syscall.SIGKILL) })+ // A session leader's group id is its own pid. Anything else means the+ // holder stayed in the group cmd.Cancel kills, and the test would pass+ // without ever exercising the shutdown path it is here for.+ if pid != pgid {+ t.Fatalf("holder pid %d is in group %d; it never left the agent's process group", pid, pgid)+ }+ return session, messagesClosed+}++// awaitHermesResult returns the session's terminal Result, failing if the+// shutdown waited on the escaped holder instead of bounding itself.+func awaitHermesResult(t *testing.T, session *Session) Result {+ t.Helper()+ select {+ case result, ok := <-session.Result:+ if !ok {+ t.Fatal("result channel closed without a value")+ }+ return result+ case <-time.After(hermesEscapedHolderBound):+ t.Fatalf("no result within %s: the escaped pipe holder wedged the shutdown", hermesEscapedHolderBound)+ return Result{}+ }+}++// awaitHermesMessagesClosed fails if the lifecycle goroutine never closed+// Messages. The Result alone does not prove it: the readers are joined after+// the Result is sent, and Messages closes only once they are.+func awaitHermesMessagesClosed(t *testing.T, messagesClosed <-chan struct{}) {+ t.Helper()+ select {+ case <-messagesClosed:+ case <-time.After(hermesEscapedHolderBound):+ t.Fatalf("Messages still open %s after the result; the readers were never joined", hermesEscapedHolderBound)+ }+}++// readEscapedHolderIdentity returns the holder's pid and process group, waiting+// hermesEscapedHolderReady for it to record them — the same budget the fake+// agent allows, since this wait starts as soon as Execute returns, before the+// handshake the agent is holding up. Missing at the end of it means the holder+// never started or setsid(2) failed, so it never held the pipes.+func readEscapedHolderIdentity(t *testing.T, path string) (pid, pgid int) {+ t.Helper()+ deadline := time.Now().Add(hermesEscapedHolderReady)+ for {+ raw, err := os.ReadFile(path)+ if err == nil {+ if _, scanErr := fmt.Sscan(strings.TrimSpace(string(raw)), &pid, &pgid); scanErr != nil {+ t.Fatalf("holder state %q: %v", raw, scanErr)+ }+ return pid, pgid+ }+ if time.Now().After(deadline) {+ t.Fatalf("holder never recorded its identity at %s: %v", path, err)+ }+ time.Sleep(10 * time.Millisecond)+ }+}