[v2]: handle target closed errors on popups
Merged
SE
8mo ago+95+95+01234567890123456789-5-5-0123456789
why
- some popups close so quickly that Stagehand tries to attach a CDP session after the target is already gone, which produces unhandled
Target.attachToTarget/newCDPSessionerrors - in the case of fast‑closing popups during auto‑init, we want to treat “target gone” as a benign race instead of surfacing as failures
what changed
- added shared “target gone” helpers in
lib/utils.ts - guarded CDP session creation to throw a targeted error when the page/frame is already closed or detached
- swallowed that targeted error during page initialization and frame listener attach only when the target is closed or the error indicates the target is gone, so other CDP failures still surface
Summary by cubic
Handle rapidly opening/closing popups by detecting “target gone” scenarios and preventing CDP session attach errors. This reduces flakiness during auto-init and keeps fast-closing popups from surfacing as failures.
- Bug Fixes
- Added StagehandTargetClosedError and utils to detect closed/detached targets and CDP “target gone” errors.
- Guarded CDP session creation and frame listener attach; swallow only “target closed” errors during init.
- Removed cached CDP clients when targets are gone and skipped work on already-closed pages/frames.