fix(browser): release cancelled CDP cleanup without waiting for responses (#517)

Signed-off-by: RudyCelekli <47457359+rudycelekli@users.noreply.github.com>
This commit is contained in:
Rudy Mizrahi Celekli
2026-10-05 14:58:33 +08:00
committed by GitHub
parent d8c18f604f
commit 488fd5f60b
3 changed files with 130 additions and 7 deletions
+38 -3
View File
@@ -47,23 +47,58 @@ export const openCdpTargetSession = async (
export const closeCdpTargetSession = async (
targetSession: CdpTargetSession,
enabledDomains: readonly string[],
signal?: AbortSignal,
): Promise<void> => {
const { connection, sessionId } = targetSession;
for (const domain of enabledDomains)
for (const [index, domain] of enabledDomains.entries()) {
if (signal?.aborted === true) {
await closeCancelledTargetSession(
targetSession,
enabledDomains.slice(index),
);
return;
}
try {
await connection.send(`${domain}.disable`, {}, sessionId);
await connection.send(`${domain}.disable`, {}, sessionId, signal);
} catch {
// Cleanup continues to the detach or direct-socket close boundary.
}
}
if (signal?.aborted === true) {
await closeCancelledTargetSession(targetSession, []);
return;
}
if (sessionId !== undefined)
try {
await connection.send("Target.detachFromTarget", { sessionId });
await connection.send(
"Target.detachFromTarget",
{ sessionId },
undefined,
signal,
);
} catch {
// Closing REA's socket is the final non-destructive cleanup boundary.
}
await connection.close();
};
const closeCancelledTargetSession = async (
{ connection, sessionId }: CdpTargetSession,
enabledDomains: readonly string[],
): Promise<void> => {
const commands = enabledDomains.map((domain) =>
connection.send(`${domain}.disable`, {}, sessionId),
);
if (sessionId !== undefined)
commands.push(connection.send("Target.detachFromTarget", { sessionId }));
const settled = Promise.allSettled(commands);
try {
await connection.close();
} finally {
await settled;
}
};
const attachedSessionId = (
value: unknown,
operation: BrowserObservationOperation,