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:
@@ -188,7 +188,11 @@ export class CdpBrowserProvider implements BrowserObservationPort {
|
||||
return err(providerError(cause, "observe_web_session"));
|
||||
} finally {
|
||||
if (targetSession !== undefined)
|
||||
await closeCdpTargetSession(targetSession, CLEANUP_DOMAINS);
|
||||
await closeCdpTargetSession(
|
||||
targetSession,
|
||||
CLEANUP_DOMAINS,
|
||||
options.signal,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,7 +233,11 @@ export class CdpBrowserProvider implements BrowserObservationPort {
|
||||
return err(providerError(cause, "discover_webmcp_tools"));
|
||||
} finally {
|
||||
if (targetSession !== undefined)
|
||||
await closeCdpTargetSession(targetSession, CLEANUP_DOMAINS);
|
||||
await closeCdpTargetSession(
|
||||
targetSession,
|
||||
CLEANUP_DOMAINS,
|
||||
options.signal,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -282,7 +290,11 @@ export class CdpBrowserProvider implements BrowserObservationPort {
|
||||
return err(providerError(cause, "capture_web_screenshot"));
|
||||
} finally {
|
||||
if (targetSession !== undefined)
|
||||
await closeCdpTargetSession(targetSession, CLEANUP_DOMAINS);
|
||||
await closeCdpTargetSession(
|
||||
targetSession,
|
||||
CLEANUP_DOMAINS,
|
||||
options.signal,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -329,7 +341,11 @@ export class CdpBrowserProvider implements BrowserObservationPort {
|
||||
});
|
||||
} finally {
|
||||
if (targetSession !== undefined)
|
||||
await closeCdpTargetSession(targetSession, CLEANUP_DOMAINS);
|
||||
await closeCdpTargetSession(
|
||||
targetSession,
|
||||
CLEANUP_DOMAINS,
|
||||
options.signal,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
import { expect, it, onTestFinished, vi } from "vitest";
|
||||
|
||||
import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js";
|
||||
import { observeWebSessionInputSchema } from "../../../src/domain/browserSession.js";
|
||||
import { startFakeCdpBrowser } from "../../fixtures/fakeCdpBrowser.js";
|
||||
|
||||
it.each(["Page.disable", "Target.detachFromTarget"])(
|
||||
"finishes caller cancellation even when %s has no response",
|
||||
async (hangOnMethod) => {
|
||||
const browser = await startFakeCdpBrowser({ hangOnMethod });
|
||||
onTestFinished(async () => {
|
||||
await browser.close();
|
||||
});
|
||||
const controller = new AbortController();
|
||||
const result = await new CdpBrowserProvider().observeSession(
|
||||
observeWebSessionInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
allowed_origins: [browser.allowedOrigin],
|
||||
target_id: "allowed-page",
|
||||
observation_ms: 1_000,
|
||||
}),
|
||||
{
|
||||
signal: controller.signal,
|
||||
progress: {
|
||||
report(update) {
|
||||
if (update.completed === 1) controller.abort();
|
||||
return Promise.resolve();
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
_tag: "AnalysisCancelledError",
|
||||
operation: "observe_web_session",
|
||||
},
|
||||
});
|
||||
const methods = browser.commands.map(({ method }) => method);
|
||||
expect(methods).toContain("Target.detachFromTarget");
|
||||
expect(methods).not.toContain("Target.closeTarget");
|
||||
expect(methods).not.toContain("Browser.close");
|
||||
},
|
||||
);
|
||||
|
||||
it("releases cleanup when cancellation arrives after capture completes", async () => {
|
||||
const browser = await startFakeCdpBrowser({ hangOnMethod: "Page.disable" });
|
||||
onTestFinished(async () => {
|
||||
await browser.close();
|
||||
});
|
||||
const controller = new AbortController();
|
||||
const pending = new CdpBrowserProvider().observeSession(
|
||||
observeWebSessionInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
allowed_origins: [browser.allowedOrigin],
|
||||
target_id: "allowed-page",
|
||||
observation_ms: 1,
|
||||
}),
|
||||
{ signal: controller.signal },
|
||||
);
|
||||
await vi.waitFor(() => {
|
||||
expect(
|
||||
browser.commands.some(({ method }) => method === "Page.disable"),
|
||||
).toBe(true);
|
||||
});
|
||||
controller.abort();
|
||||
const result = await pending;
|
||||
expect(result.ok).toBe(true);
|
||||
expect(browser.commands.map(({ method }) => method)).toContain(
|
||||
"Target.detachFromTarget",
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user