fix(cdp): avoid unhandled detach by returning original sendCDP promise

Merged
MO
10mo ago
+65-4

why

  • We saw worker crashes caused by unhandledRejection: Error("CDP session detached") when a Playwright‑connected page closed while Stagehand had inflight CDP calls.
  • Root cause: Page.sendCDP was async, which wraps the underlying CDP promise. The detach rejection hit the inner promise before the outer wrapper was awaited, so Node treated it as unhandled.

what changed

  • Fix by making Page.sendCDP return the original promise (remove async) so callers receive the same promise that is already handled internally. This eliminates the unhandled rejection race without changing semantics.

test plan

  • The new regression test reproduces this by starting a long‑running CDP command and closing the page; it failed on main with an unhandled rejection.

Summary by cubic

Fixes worker crashes from unhandled CDP session detaches when a page closes during in‑flight calls. Addresses STG-1240 by returning the original sendCDP promise and guarding internal CDP promises.

  • Bug Fixes
    • Page.sendCDP now returns the original promise (removed async) so rejections are handled by callers.
    • Added catch guards in CdpConnection to prevent early unhandledRejection on detach.
    • New regression test: long Runtime.evaluate + page close rejects cleanly with no unhandledRejection.
    • Patch changeset for @browserbasehq/stagehand.