Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix: prevent server crash when closing last tab (#66)

luigiagent3a5adeb397c2
diff --git a/internal/bridge/bridge_test.go b/internal/bridge/bridge_test.go
index 2961198d..88c3d5fb 100644
--- a/internal/bridge/bridge_test.go
+++ b/internal/bridge/bridge_test.go
@@ -2,6 +2,7 @@ package bridge
import (
"context"
+ "strings"
"sync"
"testing"
@@ -166,3 +167,21 @@ func TestTabContext_AcceptsRegisteredHashID(t *testing.T) {
t.Errorf("resolvedID should be raw CDP ID, got %s", resolvedID)
}
}
+
+func TestCloseTab_PreventsLastTabClose(t *testing.T) {
+ // CloseTab should fail when attempting to close the last remaining tab
+ // This prevents Chrome from exiting and crashing the server
+ tm := NewTabManager(context.Background(), &config.RuntimeConfig{}, nil, nil)
+
+ // Without a valid browser context, ListTargets will fail
+ // which triggers the guard at the start of CloseTab
+ err := tm.CloseTab("tab_fake1234")
+ if err == nil {
+ t.Error("CloseTab should fail when ListTargets fails")
+ }
+
+ // The error should mention listing targets
+ if err != nil && !strings.Contains(err.Error(), "list targets") {
+ t.Errorf("expected error about list targets, got: %s", err.Error())
+ }
+}
diff --git a/internal/bridge/tab_manager.go b/internal/bridge/tab_manager.go
index 50afaf1a..977d782f 100644
--- a/internal/bridge/tab_manager.go
+++ b/internal/bridge/tab_manager.go
@@ -176,6 +176,15 @@ func (tm *TabManager) CreateTab(url string) (string, context.Context, context.Ca
}
func (tm *TabManager) CloseTab(tabID string) error {
+ // Guard against closing the last tab to prevent Chrome from exiting
+ targets, err := tm.ListTargets()
+ if err != nil {
+ return fmt.Errorf("list targets: %w", err)
+ }
+ if len(targets) <= 1 {
+ return fmt.Errorf("cannot close the last tab — at least one tab must remain")
+ }
+
tm.mu.Lock()
entry, tracked := tm.tabs[tabID]
tm.mu.Unlock()
diff --git a/internal/orchestrator/handlers.go b/internal/orchestrator/handlers.go
index 8af2e718..68438c0f 100644
--- a/internal/orchestrator/handlers.go
+++ b/internal/orchestrator/handlers.go
@@ -33,6 +33,7 @@ func (o *Orchestrator) RegisterHandlers(mux *http.ServeMux) {
mux.HandleFunc("POST /instances/{id}/tab", o.proxyToInstance)
mux.HandleFunc("GET /instances/{id}/screencast", o.proxyToInstance)
+ mux.HandleFunc("POST /tabs/{id}/close", o.handleTabClose)
mux.HandleFunc("POST /tabs/{id}/navigate", o.handleTabNavigate)
mux.HandleFunc("GET /tabs/{id}/snapshot", o.handleTabSnapshot)
mux.HandleFunc("GET /tabs/{id}/screenshot", o.handleTabScreenshot)
@@ -455,6 +456,47 @@ func readResponseBody(resp *http.Response) []byte {
return body
}
+func (o *Orchestrator) handleTabClose(w http.ResponseWriter, r *http.Request) {
+ tabID := r.PathValue("id")
+ if tabID == "" {
+ web.Error(w, 400, fmt.Errorf("tab id required"))
+ return
+ }
+
+ inst, err := o.findRunningInstanceByTabID(tabID)
+ if err != nil {
+ web.Error(w, 404, err)
+ return
+ }
+
+ // Construct request body for the bridge's /tab endpoint
+ reqBody, _ := json.Marshal(map[string]string{
+ "action": "close",
+ "tabId": tabID,
+ })
+
+ targetURL := fmt.Sprintf("http://localhost:%s/tab", inst.Port)
+ proxyReq, err := http.NewRequestWithContext(r.Context(), "POST", targetURL, bytes.NewReader(reqBody))
+ if err != nil {
+ web.Error(w, 500, err)
+ return
+ }
+ proxyReq.Header.Set("Content-Type", "application/json")
+
+ client := &http.Client{Timeout: 30 * time.Second}
+ resp, err := client.Do(proxyReq)
+ if err != nil {
+ web.Error(w, 502, fmt.Errorf("instance unreachable: %w", err))
+ return
+ }
+ defer func() { _ = resp.Body.Close() }()
+
+ body, _ := io.ReadAll(resp.Body)
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(resp.StatusCode)
+ _, _ = w.Write(body)
+}
+
func (o *Orchestrator) handleTabNavigate(w http.ResponseWriter, r *http.Request) {
tabID := r.PathValue("id")
if tabID == "" {