Skip to content

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

fix: /find 502 EOF - nil ctx from CDPID tab lookup

Yash Jadhav8efff86b563b
diff --git a/cmd/pinchtab/cmd_bridge.go b/cmd/pinchtab/cmd_bridge.go
index deedf364..4af5d70f 100644
--- a/cmd/pinchtab/cmd_bridge.go
+++ b/cmd/pinchtab/cmd_bridge.go
@@ -2,6 +2,7 @@ package main
import (
"context"
+ "fmt"
"log/slog"
"net/http"
"os"
@@ -42,7 +43,7 @@ func runBridgeServer(cfg *config.RuntimeConfig) {
// HTTP server
server := &http.Server{
Addr: listenAddr,
- Handler: loggingMiddleware(mux),
+ Handler: recoveryMiddleware(loggingMiddleware(mux)),
ReadHeaderTimeout: 10 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 60 * time.Second,
@@ -88,6 +89,26 @@ func loggingMiddleware(next http.Handler) http.Handler {
})
}
+// recoveryMiddleware catches panics in HTTP handlers and returns a 500
+// instead of crashing the bridge process. Go's net/http server only
+// recovers panics in the serve goroutine; this middleware provides the
+// same guarantee for the handler level and logs the panic for debugging.
+func recoveryMiddleware(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ defer func() {
+ if p := recover(); p != nil {
+ slog.Error("handler panic recovered",
+ "method", r.Method,
+ "path", r.URL.Path,
+ "panic", fmt.Sprintf("%v", p),
+ )
+ http.Error(w, `{"error":"internal server error"}`, http.StatusInternalServerError)
+ }
+ }()
+ next.ServeHTTP(w, r)
+ })
+}
+
type statusRecorder struct {
http.ResponseWriter
statusCode int
diff --git a/internal/handlers/find.go b/internal/handlers/find.go
index 7345d8f3..47f43056 100644
--- a/internal/handlers/find.go
+++ b/internal/handlers/find.go
@@ -74,7 +74,8 @@ func (h *Handlers) HandleFind(w http.ResponseWriter, r *http.Request) {
}
// Resolve tab context to get the resolved ID for cache lookup.
- _, resolvedTabID, err := h.Bridge.TabContext(req.TabID)
+ // Keep ctxTab so we can reuse it for CDP operations (e.g. auto-refresh).
+ ctxTab, resolvedTabID, err := h.Bridge.TabContext(req.TabID)
if err != nil {
web.Error(w, 404, err)
return
@@ -83,9 +84,10 @@ func (h *Handlers) HandleFind(w http.ResponseWriter, r *http.Request) {
// Try cached snapshot first; auto-fetch if not available.
nodes := h.resolveSnapshotNodes(resolvedTabID)
if len(nodes) == 0 {
- // Auto-refresh: take a fresh snapshot via CDP.
- ctx, _, _ := h.Bridge.TabContext(resolvedTabID)
- h.refreshRefCache(ctx, resolvedTabID)
+ // Auto-refresh: take a fresh snapshot via CDP using the context
+ // obtained from the initial TabContext call (resolvedTabID is the
+ // raw CDPID and cannot be passed to TabContext again).
+ h.refreshRefCache(ctxTab, resolvedTabID)
nodes = h.resolveSnapshotNodes(resolvedTabID)
}
if len(nodes) == 0 {
diff --git a/internal/semantic/combined_matcher.go b/internal/semantic/combined_matcher.go
index 135503ff..b58e4ade 100644
--- a/internal/semantic/combined_matcher.go
+++ b/internal/semantic/combined_matcher.go
@@ -2,6 +2,7 @@ package semantic
import (
"context"
+ "fmt"
"sort"
)
@@ -70,10 +71,20 @@ func (c *CombinedMatcher) Find(ctx context.Context, query string, elements []Ele
embCh := make(chan matcherResult, 1)
go func() {
+ defer func() {
+ if p := recover(); p != nil {
+ lexCh <- matcherResult{err: fmt.Errorf("lexical matcher panic: %v", p)}
+ }
+ }()
r, err := c.lexical.Find(ctx, query, elements, internalOpts)
lexCh <- matcherResult{r, err}
}()
go func() {
+ defer func() {
+ if p := recover(); p != nil {
+ embCh <- matcherResult{err: fmt.Errorf("embedding matcher panic: %v", p)}
+ }
+ }()
r, err := c.embedding.Find(ctx, query, elements, internalOpts)
embCh <- matcherResult{r, err}
}()