Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

companion: verify proxy startup and reap timed-out run

Barack Sokullu21c0d51a97f4
diff --git a/webbrain-systray/internal/core/proxy.go b/webbrain-systray/internal/core/proxy.go
index 30c9b815d..92b955174 100644
--- a/webbrain-systray/internal/core/proxy.go
+++ b/webbrain-systray/internal/core/proxy.go
@@ -8,6 +8,7 @@ import (
"encoding/json"
"errors"
"fmt"
+ "io"
"net"
"net/http"
"os"
@@ -79,6 +80,19 @@ type Proxy struct {
func New() *Proxy { return &Proxy{} }
+func healthReady(client *http.Client, url, token string) bool {
+ resp, err := client.Get(url)
+ if err != nil {
+ return false
+ }
+ defer resp.Body.Close()
+ if resp.StatusCode != http.StatusOK {
+ return false
+ }
+ body, err := io.ReadAll(io.LimitReader(resp.Body, int64(len(token)+1)))
+ return err == nil && string(body) == token
+}
+
// Port is the port the proxy is listening on (0 when stopped).
func (p *Proxy) Port() int {
p.mu.Lock()
@@ -209,6 +223,7 @@ func (p *Proxy) Start(opts Options) error {
}
gin.SetMode(gin.ReleaseMode)
pass := settings.RandomSecret()
+ healthToken := settings.RandomSecret()
routes := opts.Routes
svc, err := cliproxy.NewBuilder().
WithConfig(cfg).
@@ -217,7 +232,7 @@ func (p *Proxy) Start(opts Options) error {
WithServerOptions(
api.WithMiddleware(opts.Middleware...),
api.WithRouterConfigurator(func(e *gin.Engine, _ *handlers.BaseAPIHandler, _ *sdkconfig.Config) {
- e.GET("/healthz-companion", func(c *gin.Context) { c.String(http.StatusOK, "ok") })
+ e.GET("/healthz-companion", func(c *gin.Context) { c.String(http.StatusOK, healthToken) })
if routes != nil {
routes(e)
}
@@ -241,7 +256,14 @@ func (p *Proxy) Start(opts Options) error {
p.mu.Unlock()
go func() {
- defer close(done)
+ defer func() {
+ p.mu.Lock()
+ if p.done == done {
+ p.port = 0
+ }
+ close(done)
+ p.mu.Unlock()
+ }()
err := svc.Run(ctx)
if err != nil && !errors.Is(err, context.Canceled) {
logx.Printf("proxy exited: %v", err)
@@ -263,14 +285,17 @@ func (p *Proxy) Start(opts Options) error {
return errors.New("proxy stopped during startup")
default:
}
- resp, err := client.Get(fmt.Sprintf("http://127.0.0.1:%d/healthz-companion", port))
- if err == nil {
- _ = resp.Body.Close()
+ if healthReady(client, fmt.Sprintf("http://127.0.0.1:%d/healthz-companion", port), healthToken) {
logx.Printf("proxy listening on %s:%d", opts.host(), port)
return nil
}
time.Sleep(150 * time.Millisecond)
}
+ cancel()
+ <-done
+ if err := p.LastError(); err != nil {
+ return err
+ }
return errors.New("proxy did not start listening in time")
}
diff --git a/webbrain-systray/internal/core/proxy_test.go b/webbrain-systray/internal/core/proxy_test.go
index 22396a176..7c38a77d7 100644
--- a/webbrain-systray/internal/core/proxy_test.go
+++ b/webbrain-systray/internal/core/proxy_test.go
@@ -1,10 +1,36 @@
package core
import (
+ "net/http"
+ "net/http/httptest"
"testing"
"time"
)
+func TestHealthProbeRequiresExpectedResponse(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ status int
+ body string
+ want bool
+ }{
+ {"error response", http.StatusNotFound, "wrong", false},
+ {"another service", http.StatusOK, "wrong", false},
+ {"expected service", http.StatusOK, "my-instance", true},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
+ w.WriteHeader(tc.status)
+ _, _ = w.Write([]byte(tc.body))
+ }))
+ defer server.Close()
+ if got := healthReady(&http.Client{Timeout: time.Second}, server.URL, "my-instance"); got != tc.want {
+ t.Fatalf("healthReady = %v, want %v", got, tc.want)
+ }
+ })
+ }
+}
+
func TestPickPortMaximum(t *testing.T) {
const host = "127.0.0.1"
const port = 65535