Skip to content

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

fix: prevent 30s hang on dialog-blocking click by skipping JS cleanup

Mario8093ac8ee975
diff --git a/internal/bridge/action_pointer.go b/internal/bridge/action_pointer.go
index 68ac579a..f715de2c 100644
--- a/internal/bridge/action_pointer.go
+++ b/internal/bridge/action_pointer.go
@@ -34,7 +34,17 @@ func clickByNodeIDWithJSFallback(ctx context.Context, nodeID int64) error {
return ctx.Err()
}
if errors.Is(err, context.DeadlineExceeded) {
- return jsClickByBackendNodeAction(ctx, nodeID)
+ // Re-check: the parent context may have been cancelled (e.g. by the
+ // dialog-detection polling loop) while the trusted click was running.
+ if ctx.Err() != nil {
+ return ctx.Err()
+ }
+ // Use a bounded timeout for the JS fallback so it cannot hang for the
+ // full action timeout when JS execution is blocked (e.g. by an open
+ // dialog).
+ jsCtx, jsCancel := context.WithTimeout(ctx, trustedNodeClickTimeout)
+ defer jsCancel()
+ return jsClickByBackendNodeAction(jsCtx, nodeID)
}
return err
}
@@ -50,7 +60,12 @@ func doubleClickByNodeIDWithJSFallback(ctx context.Context, nodeID int64) error
return ctx.Err()
}
if errors.Is(err, context.DeadlineExceeded) {
- return jsDoubleClickByBackendNodeAction(ctx, nodeID)
+ if ctx.Err() != nil {
+ return ctx.Err()
+ }
+ jsCtx, jsCancel := context.WithTimeout(ctx, trustedNodeClickTimeout)
+ defer jsCancel()
+ return jsDoubleClickByBackendNodeAction(jsCtx, nodeID)
}
return err
}
@@ -143,8 +158,6 @@ func submitFormIfButton(ctx context.Context, selector string) (bool, error) {
}
func (b *Bridge) actionClick(ctx context.Context, req ActionRequest) (result map[string]any, err error) {
- // Promote to the humanized click path when the caller (or instance
- // config) opted in via humanize=true.
if b.effectiveHumanize(req) {
return b.actionHumanizedClick(ctx, req)
}
@@ -159,7 +172,12 @@ func (b *Bridge) actionClick(ctx context.Context, req ActionRequest) (result map
if err == nil {
result = auto.finish(ctx, result)
} else {
- auto.cancel(ctx)
+ var dialogErr *ErrDialogBlocking
+ if errors.As(err, &dialogErr) {
+ auto.cancelWithoutRestore()
+ } else {
+ auto.cancel(ctx)
+ }
}
}()
@@ -304,7 +322,12 @@ func (b *Bridge) actionDoubleClick(ctx context.Context, req ActionRequest) (resu
if err == nil {
result = auto.finish(ctx, result)
} else {
- auto.cancel(ctx)
+ var dialogErr *ErrDialogBlocking
+ if errors.As(err, &dialogErr) {
+ auto.cancelWithoutRestore()
+ } else {
+ auto.cancel(ctx)
+ }
}
}()
if req.Selector != "" {
@@ -549,7 +572,12 @@ func (b *Bridge) actionHumanizedClick(ctx context.Context, req ActionRequest) (r
if err == nil {
result = auto.finish(ctx, result)
} else {
- auto.cancel(ctx)
+ var dialogErr *ErrDialogBlocking
+ if errors.As(err, &dialogErr) {
+ auto.cancelWithoutRestore()
+ } else {
+ auto.cancel(ctx)
+ }
}
}()
var backendNodeID cdp.BackendNodeID
diff --git a/internal/bridge/actions_test.go b/internal/bridge/actions_test.go
index d9c198cd..0f13f8cf 100644
--- a/internal/bridge/actions_test.go
+++ b/internal/bridge/actions_test.go
@@ -470,6 +470,34 @@ func TestClickByNodeIDWithJSFallback_DoesNotFallbackOnOtherErrors(t *testing.T)
}
}
+func TestClickByNodeIDWithJSFallback_SkipsFallbackOnCancelledCtx(t *testing.T) {
+ origTrusted := clickByNodeIDAction
+ origFallback := jsClickByBackendNodeAction
+ t.Cleanup(func() {
+ clickByNodeIDAction = origTrusted
+ jsClickByBackendNodeAction = origFallback
+ })
+
+ ctx, cancel := context.WithCancel(context.Background())
+ var fallbackCalled bool
+ clickByNodeIDAction = func(context.Context, int64) error {
+ cancel() // simulate dialog-detection cancelling the context
+ return context.DeadlineExceeded
+ }
+ jsClickByBackendNodeAction = func(context.Context, int64) error {
+ fallbackCalled = true
+ return nil
+ }
+
+ err := clickByNodeIDWithJSFallback(ctx, 42)
+ if err == nil {
+ t.Fatal("expected context cancelled error")
+ }
+ if fallbackCalled {
+ t.Fatal("JS fallback should not run when parent context is cancelled")
+ }
+}
+
func TestTypeAction_HumanizeOptInUsesHumanizedPath(t *testing.T) {
raw := New(context.TODO(), nil, &config.RuntimeConfig{Humanize: true})
ctx, cancel := context.WithCancel(context.Background())
diff --git a/internal/bridge/tab_auto_switch.go b/internal/bridge/tab_auto_switch.go
index b8b544e4..17fc2c6f 100644
--- a/internal/bridge/tab_auto_switch.go
+++ b/internal/bridge/tab_auto_switch.go
@@ -284,6 +284,18 @@ func (s *autoSwitchSession) cancel(ctx context.Context) {
}
}
+// cancelWithoutRestore releases the pending-click slot but skips the
+// window.open restore script. Used when JS execution is blocked (e.g. by
+// an open dialog) and the restore script would hang.
+func (s *autoSwitchSession) cancelWithoutRestore() {
+ if s == nil {
+ return
+ }
+ if targetID, ok := s.tm.clearPendingClick(s.openerCDP, s.slot); ok && s.tm.browserCtx != nil {
+ s.tm.closePopupTarget(targetID, s.openerCDP, "")
+ }
+}
+
// finish waits briefly for a popup target created by the click, adopts it,
// focuses it, and annotates result with "switchedToTab". If no popup arrives
// within autoSwitchGraceTimeout, result is returned unchanged.
diff --git a/tests/e2e/scenarios/api/actions-extended.sh b/tests/e2e/scenarios/api/actions-extended.sh
index 69ecb55f..e360c3b8 100644
--- a/tests/e2e/scenarios/api/actions-extended.sh
+++ b/tests/e2e/scenarios/api/actions-extended.sh
@@ -638,20 +638,20 @@ pt_get /snapshot
ALERT_REF=$(echo "$RESULT" | jq -r '[.nodes[] | select(.name == "Trigger Alert")][0].ref // empty')
# Click without dialogAction should fail fast with dialog_blocking error
-START_TIME=$(date +%s%3N 2>/dev/null || python3 -c 'import time; print(int(time.time()*1000))')
+_CLICK_T0=$(get_time_ms)
pt_post /action "{\"kind\":\"click\",\"ref\":\"${ALERT_REF}\"}"
-END_TIME=$(date +%s%3N 2>/dev/null || python3 -c 'import time; print(int(time.time()*1000))')
+_CLICK_T1=$(get_time_ms)
assert_not_ok "click without dialogAction fails"
assert_json_eq "$RESULT" '.code' 'dialog_blocking' "error code is dialog_blocking"
# Verify fast-fail: should complete in under 2 seconds (not 30s timeout)
-ELAPSED=$((END_TIME - START_TIME))
-if [ "$ELAPSED" -gt 2000 ]; then
- echo "FAIL: click took ${ELAPSED}ms, expected fast-fail under 2000ms"
+_CLICK_ELAPSED=$((_CLICK_T1 - _CLICK_T0))
+if [ "$_CLICK_ELAPSED" -gt 2000 ]; then
+ echo "FAIL: click took ${_CLICK_ELAPSED}ms, expected fast-fail under 2000ms"
exit 1
fi
-echo "PASS: fast-fail in ${ELAPSED}ms"
+echo "PASS: fast-fail in ${_CLICK_ELAPSED}ms"
# Clean up: dismiss the pending dialog
pt_post /dialog '{"action":"dismiss"}'