Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/petite-rocks-lead.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@browserbasehq/stagehand": patch
---

clean up cdp session event handlers on target detach
59 changes: 39 additions & 20 deletions packages/core/lib/v3/understudy/cdp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,40 @@ export class CdpConnection implements CDPSessionLike {
}
}

private clearSessionEventHandlers(sessionId: string): void {
const prefix = `${sessionId}:`;
for (const key of Array.from(this.eventHandlers.keys())) {
if (key.startsWith(prefix)) {
this.eventHandlers.delete(key);
}
}
}

private rejectSessionPendingWork(
sessionId: string,
targetId: string | null,
): void {
for (const [id, entry] of this.inflight.entries()) {
if (entry.sessionId === sessionId) {
entry.reject(
new PageNotFoundError(
`target closed before CDP response (sessionId=${sessionId}, targetId=${targetId})`,
),
);
this.inflight.delete(id);
}
}
for (const waiter of Array.from(this.sessionDispatchWaiters)) {
if (waiter.sessionId === sessionId) {
waiter.reject(
new PageNotFoundError(
`target closed before CDP send (sessionId=${sessionId}, targetId=${targetId})`,
),
);
}
}
}

getSession(sessionId: string): CdpSession | undefined {
return this.sessions.get(sessionId);
}
Expand Down Expand Up @@ -334,25 +368,8 @@ export class CdpConnection implements CDPSessionLike {
} else if (msg.method === "Target.detachedFromTarget") {
const p = (msg as { params: Protocol.Target.DetachedFromTargetEvent })
.params;
for (const [id, entry] of this.inflight.entries()) {
if (entry.sessionId === p.sessionId) {
entry.reject(
new PageNotFoundError(
`target closed before CDP response (sessionId=${p.sessionId}, targetId=${p.targetId})`,
),
);
this.inflight.delete(id);
}
}
for (const waiter of Array.from(this.sessionDispatchWaiters)) {
if (waiter.sessionId === p.sessionId) {
waiter.reject(
new PageNotFoundError(
`target closed before CDP send (sessionId=${p.sessionId}, targetId=${p.targetId})`,
),
);
}
}
this.rejectSessionPendingWork(p.sessionId, p.targetId ?? null);
this.clearSessionEventHandlers(p.sessionId);
this.sessions.delete(p.sessionId);
this.sessionToTarget.delete(p.sessionId);
this.latestCdpCallEvent.delete(p.sessionId);
Expand All @@ -361,9 +378,11 @@ export class CdpConnection implements CDPSessionLike {
// Remove any session mapping for this target
for (const [sessionId, targetId] of this.sessionToTarget.entries()) {
if (targetId === p.targetId) {
this.rejectSessionPendingWork(sessionId, p.targetId);
this.clearSessionEventHandlers(sessionId);
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
this.sessions.delete(sessionId);
this.sessionToTarget.delete(sessionId);
this.latestCdpCallEvent.delete(sessionId);
break;
}
}
}
Expand Down
Loading
Loading