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
8 changes: 8 additions & 0 deletions packages/engine/src/cdp/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,14 @@ export async function handleWebSocket(
budget: browser.workerBudget,
waitUntil: (promise) => ctx.waitUntil(promise),
});
if (target.retiringPageScript) {
const retiring = target.retiringPageScript;
target.retiringPageScript = null;
void retireStub(retiring, "wsClosed", "engine WS closed", {
budget: browser.workerBudget,
waitUntil: (promise) => ctx.waitUntil(promise),
});
}
void retireStub(target, "wsClosed", "engine WS closed", {
budget: browser.workerBudget,
waitUntil: (promise) => ctx.waitUntil(promise),
Expand Down
5 changes: 4 additions & 1 deletion packages/engine/src/cdp/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,10 @@ export function ensurePageScript(
spawnKind: "blank",
host: "",
});
for (const source of dctx.browser.preNavScripts.values()) {
for (const source of [
...dctx.browser.preNavScripts.values(),
...target.preNavScripts.values(),
]) {
try {
await stub.evaluate(source, {});
} catch (err) {
Expand Down
18 changes: 18 additions & 0 deletions packages/engine/src/cdp/fetch-interception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export interface FetchInterceptionState {
parked: Map<string, ParkedRequest>;
nextId: number;
pendingEmits: Protocol.Fetch.RequestPausedEvent[];
/** The session that sent `Fetch.enable`; undefined for the browser session. */
ownerSessionId?: string;
}

const registry = new Map<string, FetchInterceptionState>();
Expand Down Expand Up @@ -80,6 +82,22 @@ export function releaseParkedForConnection(connectionId: string): void {
if (st) releaseAllParked(st);
}

export function disableInterception(st: FetchInterceptionState): void {
releaseAllParked(st);
st.patterns = [];
st.pendingEmits = [];
}

// Like Chrome, interception ends with the session that enabled it; otherwise every later
// request on the connection parks for a verdict nobody can send.
export function endSessionInterception(connectionId: string, sessionId: string): void {
const st = registry.get(connectionId);
if (!st || st.ownerSessionId !== sessionId) return;
disableInterception(st);
st.emit = () => {};
st.ownerSessionId = undefined;
}

export function unregisterInterception(connectionId: string): void {
const st = registry.get(connectionId);
if (st) releaseAllParked(st);
Expand Down
9 changes: 3 additions & 6 deletions packages/engine/src/cdp/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import type { CdpRequest } from "./types.ts";
import type { DispatchCtx } from "./dispatch.ts";
import {
disableInterception,
ensureInterception,
getInterception,
MAX_GLOB_PATTERN_CHARS,
MAX_GLOB_PATTERNS,
releaseAllParked,
resolveParked,
type InterceptAction,
} from "./fetch-interception.ts";
Expand Down Expand Up @@ -41,17 +41,14 @@ export async function handleFetch(ctx: DispatchCtx, req: CdpRequest): Promise<un
resourceType: p.resourceType,
}));
const sessionId = req.sessionId;
st.ownerSessionId = sessionId;
st.emit = (params) => ctx.emit({ method: "Fetch.requestPaused", params, sessionId });
return {};
}

case "Fetch.disable": {
const st = getInterception(ctx.connectionId);
if (st) {
releaseAllParked(st);
st.patterns = [];
st.pendingEmits = [];
}
if (st) disableInterception(st);
return {};
}

Expand Down
40 changes: 37 additions & 3 deletions packages/engine/src/cdp/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import { retireStub } from "./page-script-generation.ts";
import { createCookieJar, type CookieJar } from "../cookies.ts";
import { engineWasmBytes, engineWasmInitialBytes } from "../engine-wasm-probe.ts";
import { isolateRetainedBodyBytesTotal } from "./network-events.ts";
import { imageStoreStats } from "../eval/resource-store.ts";
import { dropTargetImages, imageStoreStats } from "../eval/resource-store.ts";
import { assetCacheRetainedBytes } from "../eval/outbound.ts";
import { clearLearnedScripts } from "../eval/learned-scripts.ts";
import { releaseParkedForConnection } from "./fetch-interception.ts";
import { endSessionInterception, releaseParkedForConnection } from "./fetch-interception.ts";
import { releaseAllDomHandles } from "./dom-release.ts";
import { outerHtmlResponse, stubQueryDom } from "./page-session.ts";
import {
Expand Down Expand Up @@ -67,6 +67,7 @@ export function detachSessions(ctx: DispatchCtx, sessions: Set<string>, targetId
params: { sessionId, targetId },
});
ctx.browser.networkEvents.forgetSession(sessionId);
endSessionInterception(ctx.connectionId, sessionId);
}
sessions.clear();
}
Expand Down Expand Up @@ -282,6 +283,7 @@ export async function handleTarget(ctx: DispatchCtx, req: CdpRequest): Promise<u
}
}
ctx.browser.networkEvents.forgetSession(sessionId);
endSessionInterception(ctx.connectionId, sessionId);
if (sessionId === ctx.browser.rendererSessionId) {
ctx.browser.rendererSessionId = null;
}
Expand Down Expand Up @@ -334,6 +336,7 @@ export function ensureBrowserTarget(ctx: DispatchCtx): TargetState {
lastLoadAt: 0,
layoutMode: ctx.browser.layoutMode,
compiledScripts: [],
preNavScripts: new Map(),
debuggerEnabledSessions: new Set(),
domEnabledSessions: new Set(),
cssEnabledSessions: new Set(),
Expand Down Expand Up @@ -368,6 +371,16 @@ export async function closeTargetInternal(ctx: DispatchCtx, id: string): Promise
ctx.browser.screencasts.delete(id);
void cancelAndRemoveTargetWheelQueues(ctx.browser, id);
ctx.browser.dedicatedWorkers.terminateTarget(id, "Target.closeTarget");
if (target.retiringPageScript) {
const retiring = target.retiringPageScript;
target.retiringPageScript = null;
retirements.push(
retireStub(retiring, "closeTarget", "Target.closeTarget", {
budget: ctx.browser.workerBudget,
awaitTeardown: true,
}),
);
}
retirements.push(
retireStub(target, "closeTarget", "Target.closeTarget", {
budget: ctx.browser.workerBudget,
Expand All @@ -385,6 +398,7 @@ export async function closeTargetInternal(ctx: DispatchCtx, id: string): Promise
}
for (const sessionId of frame.sessions) {
ctx.browser.networkEvents.forgetSession(sessionId);
endSessionInterception(ctx.connectionId, sessionId);
}
frame.sessions.clear();
frame.detached = true;
Expand All @@ -398,7 +412,7 @@ export async function closeTargetInternal(ctx: DispatchCtx, id: string): Promise
if (!pre) return;
try {
const { discardPrespawnedFrame } = await import("../eval/oopif-render.ts");
await discardPrespawnedFrame(pre, "Target.closeTarget");
await discardPrespawnedFrame(pre, "Target.closeTarget", true);
} catch {}
}),
);
Expand All @@ -413,9 +427,28 @@ export async function closeTargetInternal(ctx: DispatchCtx, id: string): Promise
);
}
releaseAllDomHandles(target);
// A closed page cannot paint again; free its images and fonts now instead of at disconnect.
dropTargetImages(ctx.connectionId, id);
}
const existed = ctx.browser.targets.delete(id);
stopSceneStreams(ctx.browser, id);
if (ctx.browser.screencasts.size === 0 && ctx.browser.rendererManager) {
// Each page uses a unique resource scope, so closed targets leave unreuseable images/fonts
// in the renderer's wasm heap. Recycle it once idle and above the wasm high-water threshold.
const RENDERER_IDLE_RECYCLE_WASM_BYTES = 36 * 1024 * 1024;
const RENDERER_BUSY_RECYCLE_WASM_BYTES = 48 * 1024 * 1024;
let hasLivePage = false;
for (const remaining of ctx.browser.targets.values()) {
if (remaining.pageScript) {
hasLivePage = true;
break;
}
}
ctx.browser.rendererManager.recycleIfIdle(
hasLivePage ? "wasm high-water recycle" : "idle wasm high-water recycle",
hasLivePage ? RENDERER_BUSY_RECYCLE_WASM_BYTES : RENDERER_IDLE_RECYCLE_WASM_BYTES,
);
}
if (existed) {
ctx.emit({
method: "Target.targetDestroyed",
Expand Down Expand Up @@ -479,6 +512,7 @@ export function createPageTarget(
lastLoadAt: 0,
layoutMode: ctx.browser.layoutMode,
compiledScripts: [],
preNavScripts: new Map(),
debuggerEnabledSessions: new Set(),
domEnabledSessions: new Set(),
cssEnabledSessions: new Set(),
Expand Down
12 changes: 12 additions & 0 deletions packages/engine/src/cdp/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ export interface BrowserState {
isolateCapOverrides?: PageIsolateCapOverrides;
connectionMode: "page" | "browser";
ttyKeyboard?: boolean;
// Page.addScriptToEvaluateOnNewDocument sent without a session id is not
// target-scoped; it applies to every target the connection opens.
preNavScripts: Map<string, string>;
learnedInsertedScripts: LearnedScripts;
pendingBindings: Set<string>;
Expand Down Expand Up @@ -320,6 +322,12 @@ export interface TargetState {
pageScriptUrl?: string;
pageSession?: PageSession;
pageScriptLease: WorkerLease | null;
// Detached about:blank holder being retired concurrently with Page.navigate prefetches,
// kept reachable so Target.closeTarget and WS close can immediately kill it.
retiringPageScript?: {
pageScript: PageScriptStub | null;
pageScriptLease: WorkerLease | null;
} | null;
navigateGeneration: number;
wheelTeardownDepth: number;
// A navigation an input started and replied before; the next input waits for it, so it never
Expand All @@ -343,6 +351,10 @@ export interface TargetState {
lastLoadAt: number;
layoutMode: "full" | "off";
compiledScripts: CompiledScriptMeta[];
// Page.addScriptToEvaluateOnNewDocument is session-scoped in CDP: a script
// added on a page session belongs to that target and dies with it, never to
// a later target.
preNavScripts: Map<string, string>;
debuggerEnabledSessions: Set<string>;
domEnabledSessions: Set<string>;
cssEnabledSessions: Set<string>;
Expand Down
3 changes: 3 additions & 0 deletions packages/engine/src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export {
KitesurfEngine,
SandboxOutbound,
PageScriptTail,
// Wrapping Workers must re-export `PageIsolateHost` under this name so
// `ctx.exports.PageIsolateHost` hosts dynamic isolates in their own loopback IoContext.
PageIsolateHost,
DeniedOutbound,
SpanSink,
} from "./index.ts";
Expand Down
4 changes: 3 additions & 1 deletion packages/engine/src/eval/dedicated-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,9 @@ export function spawnDedicatedWorker(
const entrypoint = stub.getEntrypoint<DedicatedWorkerEntrypointStub>("DedicatedWorker", {
props: { KS_TAIL_TOKEN: tailToken },
});
const outboundHandle = opts.sandboxOutbound.session(outboundProps);
const init: OpenWorkerSessionInit = {
outbound: opts.sandboxOutbound.session(outboundProps),
outbound: outboundHandle.outbound,
upstream: (message: unknown) => {
if (!isWorkerUpstreamMessage(message)) {
console.warn(
Expand Down Expand Up @@ -212,6 +213,7 @@ export function spawnDedicatedWorker(
if (opts.gone) gone = true;
if (terminated) return terminated;
terminated = (async () => {
outboundHandle.cancelBodies(reason);
await settleWithin(
sessionReady.then(async (session) => {
try {
Expand Down
10 changes: 7 additions & 3 deletions packages/engine/src/eval/oopif-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,13 @@ async function discardFrameIsolate(
reason: string,
final = false,
): Promise<void> {
try {
await warmup;
} catch {}
if (final) {
void warmup.catch(() => {});
} else {
try {
await warmup;
} catch {}
}
await disposeStub(stub, { unload: final ? "none" : "awaited", reason, killAtOnce: final });
}

Expand Down
Loading
Loading