Skip to content

armgit / ax

Public Imported from GitHub
0

Agent Executor (AX) is Google's open agentic orchestration runtime

Go

feat: prevent sidecar startup when ReadyFunc indicates the endpoint is already in use

Jaana Dogan1abc003dca88
diff --git a/internal/pythonsidecar/sidecar.go b/internal/pythonsidecar/sidecar.go
index d02799b..9c8f6fe 100644
--- a/internal/pythonsidecar/sidecar.go
+++ b/internal/pythonsidecar/sidecar.go
@@ -80,6 +80,13 @@ func (s *Sidecar) Start(ctx context.Context, pythonPath string) error {
return fmt.Errorf("Module cannot be empty")
}
+ if s.cfg.ReadyFunc != nil {
+ if err := s.cfg.ReadyFunc(ctx); err == nil {
+ s.mu.Unlock()
+ return fmt.Errorf("cannot start sidecar: endpoint is already in use by another process")
+ }
+ }
+
// Prepare arguments: python -u -m module [args...]
// -u forces unbuffered stdout/stderr so logs stream to Go instantly
fullArgs := append([]string{"-u", "-m", s.cfg.Module}, s.cfg.Args...)
diff --git a/internal/pythonsidecar/sidecar_test.go b/internal/pythonsidecar/sidecar_test.go
index 3052f41..b594257 100644
--- a/internal/pythonsidecar/sidecar_test.go
+++ b/internal/pythonsidecar/sidecar_test.go
@@ -156,3 +156,30 @@ func TestSidecar_ReadinessFailureOnPrematureExit(t *testing.T) {
t.Fatalf("expected IsRunning() to be false")
}
}
+
+func TestSidecar_EndpointAlreadyInUse(t *testing.T) {
+ // Start a dummy TCP listener on a free port
+ l, err := net.Listen("tcp", "127.0.0.1:0")
+ if err != nil {
+ t.Fatalf("failed to listen: %v", err)
+ }
+ defer l.Close()
+ addr := l.Addr().String()
+
+ cfg := pythonsidecar.Config{
+ Module: "http.server",
+ ReadyFunc: pythonsidecar.TCPReady(addr),
+ }
+
+ s := pythonsidecar.New(cfg)
+ ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
+ defer cancel()
+
+ err = s.Start(ctx, "")
+ if err == nil {
+ t.Fatalf("expected Start() to fail when endpoint is already in use")
+ }
+ if !strings.Contains(err.Error(), "already in use") {
+ t.Fatalf("expected 'already in use' in error, got: %v", err)
+ }
+}