Skip to content

Commit d8993f0

Browse files
committed
fix(desktop): release debugger state after oversized preview events
1 parent d25e526 commit d8993f0

4 files changed

Lines changed: 67 additions & 24 deletions

File tree

‎apps/desktop/src/preview/CdpRelay.test.ts‎

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ describe("CDP relay", () => {
101101
it("acknowledges Playwright's focus emulation without forcing it onto the guest", async () => {
102102
const send = vi.fn(async () => ({}));
103103
const written: Array<Record<string, unknown>> = [];
104-
const relay = createCdpRelayConnection(makeTarget(send), (raw) =>
105-
written.push(JSON.parse(raw)),
106-
);
104+
const relay = makeRelay(makeTarget(send), (raw) => written.push(JSON.parse(raw)));
107105
relay.receive(
108106
JSON.stringify({
109107
id: 1,
@@ -239,22 +237,26 @@ describe("CDP relay payload budget", () => {
239237
]);
240238
});
241239

242-
it("closes instead of dropping an oversized event", async () => {
243-
const written: Array<Record<string, unknown>> = [];
244-
const close = vi.fn();
245-
const relay = createCdpRelayConnection(
246-
makeTarget(vi.fn()),
247-
(raw) => written.push(JSON.parse(raw)),
248-
close,
249-
);
250-
relay.receive(JSON.stringify({ id: 1, method: "Target.setAutoAttach", params: {} }));
251-
await settle();
252-
written.length = 0;
240+
it.each([oversized, "\u0001".repeat(12 * 1024 * 1024)])(
241+
"closes instead of dropping an oversized event (%#)",
242+
async (args) => {
243+
const written: Array<Record<string, unknown>> = [];
244+
const close = vi.fn();
245+
const relay = createCdpRelayConnection(
246+
makeTarget(vi.fn()),
247+
(raw) => written.push(JSON.parse(raw)),
248+
close,
249+
);
250+
relay.receive(JSON.stringify({ id: 1, method: "Target.setAutoAttach", params: {} }));
251+
await settle();
252+
written.length = 0;
253253

254-
relay.event("Runtime.consoleAPICalled", { args: oversized }, "");
255-
relay.event("Page.loadEventFired", { timestamp: 1 }, "");
254+
relay.event("Runtime.consoleAPICalled", { args }, "");
255+
relay.event("Page.loadEventFired", { timestamp: 1 }, "");
256256

257-
expect(close).toHaveBeenCalledOnce();
258-
expect(written.map((message) => message["method"])).toEqual([]);
259-
});
257+
relay.receive(JSON.stringify({ id: 2, method: "Target.setAutoAttach" }));
258+
expect(close).toHaveBeenCalledOnce();
259+
expect(written.map((message) => message["method"])).toEqual([]);
260+
},
261+
);
260262
});

‎apps/desktop/src/preview/CdpRelay.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ export function createCdpRelayConnection(
196196

197197
return {
198198
receive: (raw) => {
199+
if (closed) return;
199200
let command: unknown;
200201
try {
201202
command = JSON.parse(raw);

‎apps/desktop/src/preview/DesktopBrowserHost.test.ts‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// @effect-diagnostics nodeBuiltinImport:off - Stands in for an Electron debugger.
2-
import { describe, expect, it } from "@effect/vitest";
2+
import { describe, expect, it, vi } from "@effect/vitest";
33
import { DesktopBrowserEvent } from "@t3tools/contracts";
44
import * as Effect from "effect/Effect";
55
import * as Fiber from "effect/Fiber";
66
import * as Queue from "effect/Queue";
77
import * as Schema from "effect/Schema";
8-
import * as Queue from "effect/Queue";
98
import * as Stream from "effect/Stream";
109
import * as TestClock from "effect/testing/TestClock";
1110
import * as NodeEvents from "node:events";
@@ -973,7 +972,8 @@ describe("DesktopBrowserHost", () => {
973972
it.effect("detaches only the tab whose CDP event is too large to relay", () =>
974973
Effect.gen(function* () {
975974
const host = yield* DesktopBrowserHost.make;
976-
const tabs = [makeDebuggee(), makeDebuggee()];
975+
const tabs = [makeDebuggee({ deferFocus: true }), makeDebuggee()];
976+
const send = vi.spyOn(tabs[0]!.tab.debugger, "sendCommand");
977977
const keys = [key, { threadId: "thread-2", tabId: "tab-2" }];
978978
const events = yield* Queue.unbounded<DesktopBrowserEvent>();
979979
yield* host.events.pipe(
@@ -996,6 +996,19 @@ describe("DesktopBrowserHost", () => {
996996
yield* Queue.takeN(events, 2);
997997
}
998998

999+
yield* host.handleCommandLine(
1000+
encodeJson({
1001+
type: "cdp",
1002+
...key,
1003+
message: encodeJson({
1004+
id: 2,
1005+
method: "Target.setAutoAttach",
1006+
params: { autoAttach: true, waitForDebuggerOnStart: true, flatten: true },
1007+
sessionId: "t3-preview-page",
1008+
}),
1009+
}),
1010+
);
1011+
yield* command(host, 3, "Page.startScreencast");
9991012
tabs[0]!.emit("Runtime.consoleAPICalled", {
10001013
args: Array(2048).fill("x".repeat(40 * 1024)),
10011014
});
@@ -1004,6 +1017,20 @@ describe("DesktopBrowserHost", () => {
10041017

10051018
const { type, threadId, tabId } = detached!;
10061019
expect({ type, threadId, tabId }).toEqual({ type: "detached", ...key });
1020+
expect(send).toHaveBeenCalledWith("Target.setAutoAttach", {
1021+
autoAttach: false,
1022+
waitForDebuggerOnStart: false,
1023+
flatten: true,
1024+
});
1025+
tabs[0]!.settleFocus();
1026+
yield* settle;
1027+
expect(send).not.toHaveBeenCalledWith("Target.setAutoAttach", {
1028+
autoAttach: true,
1029+
waitForDebuggerOnStart: true,
1030+
flatten: true,
1031+
});
1032+
expect(send).toHaveBeenCalledWith("Page.stopScreencast");
1033+
expect(tabs[0]!.focusEmulation).toEqual([true, false]);
10071034
expect(next).toMatchObject({ type: "cdp", ...keys[1] });
10081035
expect(JSON.parse((next as { message: string }).message)).toMatchObject({
10091036
method: "Page.loadEventFired",

‎apps/desktop/src/preview/DesktopBrowserHost.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ export const make = Effect.gen(function* () {
329329
const forward = (current?: () => boolean) => {
330330
const change = tab.focusChange;
331331
const run = () =>
332-
current === undefined || current()
332+
tab.relay === relay && (current === undefined || current())
333333
? send(method, params, sessionId)
334334
: Promise.reject(new Error("The page this screenshot was for has moved on."));
335335
return change === null ? run() : change.then(run);
@@ -375,7 +375,20 @@ export const make = Effect.gen(function* () {
375375
// Announcing the tab again would reconnect Playwright, and the page could
376376
// replay the same oversized event, so the tab stays detached.
377377
() => {
378-
if (tab.relay === relay && tabs.get(keyOf(tab.key)) === tab) detach(tab.key);
378+
if (tab.relay !== relay || tabs.get(keyOf(tab.key)) !== tab) return;
379+
// Disabling auto-attach detaches child sessions and releases their debugger pauses.
380+
void debuggee
381+
.sendCommand("Target.setAutoAttach", {
382+
autoAttach: false,
383+
waitForDebuggerOnStart: false,
384+
flatten: true,
385+
})
386+
.catch(() => {});
387+
if (tab.screencasts.size > 0) {
388+
void debuggee.sendCommand("Page.stopScreencast").catch(() => {});
389+
}
390+
setFocusEmulation(tab, false);
391+
detach(tab.key);
379392
},
380393
);
381394
tab.relay = relay;

0 commit comments

Comments
 (0)