Skip to content

Agent Mesh

node: reap the stdio bridge backend after killing it

Antonio Ojea108d902f47b8
diff --git a/internal/node/service.go b/internal/node/service.go
index 5969d510..d167579d 100644
--- a/internal/node/service.go
+++ b/internal/node/service.go
@@ -16,9 +16,11 @@ package node
import (
"context"
+ "errors"
"fmt"
"net/http"
"net/http/httputil"
+ "os"
"os/exec"
"strings"
@@ -101,12 +103,17 @@ func (b *baseService) Init(ctx context.Context) error {
return nil
}
-// Teardown kills the command backend's process, if any.
+// Teardown kills the command backend's process, if any. The bridge's stdout
+// reader reaps it, and has already done so for a backend that exited on its
+// own, so an already-finished process is not an error.
func (b *baseService) Teardown() error {
if b.cmd == nil || b.cmd.Process == nil {
return nil
}
- return b.cmd.Process.Kill()
+ if err := b.cmd.Process.Kill(); err != nil && !errors.Is(err, os.ErrProcessDone) {
+ return err
+ }
+ return nil
}
func NewServiceFromRequest(req *api.RegisterServiceRequest) (Service, error) {
diff --git a/internal/node/service_test.go b/internal/node/service_test.go
index f3692b43..c02d1898 100644
--- a/internal/node/service_test.go
+++ b/internal/node/service_test.go
@@ -16,10 +16,14 @@ package node
import (
"context"
+ "errors"
"net/http"
"net/http/httptest"
"net/http/httputil"
+ "os"
+ "syscall"
"testing"
+ "time"
"github.com/google/sam/api"
)
@@ -187,6 +191,39 @@ func TestBaseService_TeardownIsNilSafe(t *testing.T) {
}
}
+// A killed backend must also be reaped: kill(pid, 0) keeps succeeding on a
+// zombie, and Process.Signal only reports ErrProcessDone once Wait has run.
+func TestBaseService_TeardownReapsCommandBackend(t *testing.T) {
+ b := &baseService{
+ info: &api.ServiceInfo{Type: api.ServiceType_SERVICE_TYPE_MCP, Name: "demo"},
+ backend: &api.RegisterServiceRequest_Command{
+ Command: &api.CommandBackend{Command: []string{"/bin/cat"}},
+ },
+ }
+ if err := b.Init(context.Background()); err != nil {
+ t.Fatalf("Init: %v", err)
+ }
+ if err := b.Teardown(); err != nil {
+ t.Fatalf("Teardown: %v", err)
+ }
+
+ deadline := time.Now().Add(5 * time.Second)
+ for {
+ err := b.cmd.Process.Signal(syscall.Signal(0))
+ if errors.Is(err, os.ErrProcessDone) {
+ break
+ }
+ if time.Now().After(deadline) {
+ t.Fatalf("backend pid %d still not reaped after Teardown (Signal(0) = %v)", b.cmd.Process.Pid, err)
+ }
+ time.Sleep(10 * time.Millisecond)
+ }
+
+ if err := b.Teardown(); err != nil {
+ t.Errorf("Teardown on an already-reaped backend: %v", err)
+ }
+}
+
func TestNewServiceFromRequest_ReturnsMCPService(t *testing.T) {
req := &api.RegisterServiceRequest{
Service: &api.ServiceInfo{Type: api.ServiceType_SERVICE_TYPE_MCP, Name: "x"},
diff --git a/internal/node/stdio_bridge.go b/internal/node/stdio_bridge.go
index 5fd7fb8c..f89f7360 100644
--- a/internal/node/stdio_bridge.go
+++ b/internal/node/stdio_bridge.go
@@ -75,10 +75,9 @@ func (b *StdioBridge) Start() {
} else {
logger.Warnf("[StdioBridge] backend closed stdout, refusing further requests")
}
- if b.cmd != nil && b.cmd.Process != nil {
- _ = b.cmd.Process.Kill()
- }
+ // Refuse requests and release waiters before reaping: Wait blocks for
+ // as long as the killed process takes to die, and callers must not.
b.mu.Lock()
b.closed = true
for id, call := range b.calls {
@@ -86,6 +85,13 @@ func (b *StdioBridge) Start() {
delete(b.calls, id)
}
b.mu.Unlock()
+
+ if b.cmd != nil && b.cmd.Process != nil {
+ _ = b.cmd.Process.Kill()
+ // Reap here, the only stdout reader, so Wait cannot race a read on
+ // the pipe it closes; skipping it leaves a zombie until the node exits.
+ _ = b.cmd.Wait()
+ }
}()
}