Skip to content

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

fix: fail fast on new-tab creation under memory pressure instead of hanging 60s

Boschd85103a6035a
diff --git a/internal/bridge/tab_manager.go b/internal/bridge/tab_manager.go
index bddb5b74..b724e4c7 100644
--- a/internal/bridge/tab_manager.go
+++ b/internal/bridge/tab_manager.go
@@ -2,6 +2,7 @@ package bridge
import (
"context"
+ "errors"
"fmt"
"log/slog"
"sync"
@@ -15,6 +16,18 @@ import (
"github.com/pinchtab/pinchtab/internal/ids"
)
+const (
+ // tabCreateTimeout bounds the CDP target creation for a new tab. Creating a
+ // target is near-instant on a healthy browser, so a long stall means the
+ // browser is unhealthy (e.g. resource exhaustion) — fail fast instead of
+ // waiting tens of seconds.
+ tabCreateTimeout = 10 * time.Second
+ // tabCreateNavTimeout bounds the initial load of a freshly created tab. Kept
+ // well below the full navigate timeout so a wedged renderer (e.g. after an
+ // out-of-memory event) surfaces quickly rather than hanging ~60s.
+ tabCreateNavTimeout = 20 * time.Second
+)
+
type TabSetupFunc func(ctx context.Context, tabID string)
type TabManager struct {
@@ -150,7 +163,7 @@ func (tm *TabManager) CreateTab(url string) (string, context.Context, context.Ca
// target.CreateTarget works for both local and remote (CDP_URL) allocators.
var targetID target.ID
- createCtx, createCancel := context.WithTimeout(tm.browserCtx, 30*time.Second)
+ createCtx, createCancel := context.WithTimeout(tm.browserCtx, tabCreateTimeout)
if err := chromedp.Run(createCtx,
chromedp.ActionFunc(func(ctx context.Context) error {
var err error
@@ -159,6 +172,9 @@ func (tm *TabManager) CreateTab(url string) (string, context.Context, context.Ca
}),
); err != nil {
createCancel()
+ if errors.Is(err, context.DeadlineExceeded) {
+ return "", nil, nil, fmt.Errorf("create tab: browser did not open a new tab within %s — it may be out of memory or overloaded (close tabs or restart the instance)", tabCreateTimeout)
+ }
return "", nil, nil, fmt.Errorf("create target: %w", err)
}
createCancel()
@@ -186,13 +202,16 @@ func (tm *TabManager) CreateTab(url string) (string, context.Context, context.Ca
}
if url != "" && url != "about:blank" {
- navCtx, navCancel := context.WithTimeout(ctx, 30*time.Second)
+ navCtx, navCancel := context.WithTimeout(ctx, tabCreateNavTimeout)
if err := chromedp.Run(navCtx, chromedp.Navigate(url)); err != nil {
navCancel()
cancel()
if execCtx, execErr := browserExecutorContext(tm.browserCtx); execErr == nil {
_ = target.CloseTarget(targetID).Do(execCtx)
}
+ if errors.Is(err, context.DeadlineExceeded) {
+ return "", nil, nil, fmt.Errorf("navigate new tab: page did not load within %s — the browser may be out of memory or overloaded (close tabs or restart the instance)", tabCreateNavTimeout)
+ }
return "", nil, nil, fmt.Errorf("navigate: %w", err)
}
navCancel()
diff --git a/internal/handlers/navigation.go b/internal/handlers/navigation.go
index 268caa74..15ba83b7 100644
--- a/internal/handlers/navigation.go
+++ b/internal/handlers/navigation.go
@@ -399,11 +399,19 @@ func (h *Handlers) executeNavigate(w http.ResponseWriter, r *http.Request, req n
blockPatterns := navigateBlockPatterns(req, effectiveCfg)
newTabOpts := func() navigateBrowserOptions {
+ // A brand-new tab that can't load within the ceiling is far more often a
+ // wedged/out-of-memory renderer than a genuinely slow page, so cap the
+ // default new-tab budget to fail fast instead of hanging the full navigate
+ // timeout (~60s). An explicit --timeout still wins.
+ newTabTimeout := navTimeout
+ if req.Timeout <= 0 && newTabTimeout > newTabNavCeiling {
+ newTabTimeout = newTabNavCeiling
+ }
return navigateBrowserOptions{
URL: req.URL,
WaitFor: req.WaitFor,
WaitSelector: req.WaitSelector,
- NavTimeout: navTimeout,
+ NavTimeout: newTabTimeout,
TitleWait: titleWait,
Target: targets.target,
TrustedCIDRs: targets.trustedCIDRs,
@@ -509,6 +517,12 @@ func (h *Handlers) runNavigate(w http.ResponseWriter, r *http.Request, ex navExe
return
}
}
+ if ex.isNewTab && errors.Is(navErr, context.DeadlineExceeded) {
+ httpx.Error(w, http.StatusServiceUnavailable, fmt.Errorf(
+ "new tab did not load in time — the browser may be out of memory or overloaded; "+
+ "close tabs or restart the instance with `pinchtab server restart`"))
+ return
+ }
navigateErrorWithHint(w, classifyNavigateError(navErr), navErr, ex.url)
return
}
@@ -609,6 +623,11 @@ type staticFirstNavigator interface {
StaticFirstNavigate() bool
}
+// newTabNavCeiling caps the default time a new-tab navigation will wait before
+// failing, so an out-of-memory / wedged renderer surfaces quickly instead of
+// hanging the full NavigateTimeout. Explicit per-request timeouts override it.
+const newTabNavCeiling = 30 * time.Second
+
func (h *Handlers) navigateNewTabBrowser(w http.ResponseWriter, r *http.Request, opts navigateBrowserOptions) {
// Create a blank tab first so the requested URL becomes the first
// real history entry.