Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(spaces): stop the video reporter when the host removes its handler (#6751)

ddupont440c87f150b2
diff --git a/apps/cua-spaces-web/src/lib/stream-surface.test.tsx b/apps/cua-spaces-web/src/lib/stream-surface.test.tsx
index f903a958aa..46c80ca977 100644
--- a/apps/cua-spaces-web/src/lib/stream-surface.test.tsx
+++ b/apps/cua-spaces-web/src/lib/stream-surface.test.tsx
@@ -383,6 +383,89 @@ describe("stream surface helpers", () => {
});
describe("VideoSlotReporter", () => {
+ // CUA-SPACES-DESKTOP-3: WebKit's postMessage returns a promise that rejects with InvalidAccessError once the host
+ // removed the handler (its window closed) while this page's poll and frames keep running.
+ describe("when the host removes its handler", () => {
+ const gone = () => Object.assign(new Error("The object does not support the operation or argument."), { name: "InvalidAccessError" });
+
+ function setup(postMessage: (m: StreamSurfaceMessage) => unknown) {
+ vi.useFakeTimers();
+ vi.stubGlobal("innerWidth", 1000);
+ vi.stubGlobal("innerHeight", 700);
+ document.elementFromPoint = () => null;
+ document.elementsFromPoint = () => [];
+ const reporter = new VideoSlotReporter({ postMessage });
+ const el = document.createElement("div");
+ document.body.append(el);
+ box(el, { x: 10, y: 20, width: 232, height: 145 });
+ reporter.register(el, { spaceId: "a", tier: "tile", interactive: false, radius: 8 }, () => {});
+ return { reporter, el };
+ }
+
+ afterEach(() => {
+ vi.useRealTimers();
+ vi.restoreAllMocks();
+ });
+
+ it("stops its poll and frames, never leaves a rejection unhandled, and warns once", async () => {
+ const unhandled: unknown[] = [];
+ const onUnhandled = (e: unknown) => unhandled.push(e);
+ process.on("unhandledRejection", onUnhandled);
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
+ let removed = false;
+ let calls = 0;
+ const { reporter, el } = setup(() => {
+ calls += 1;
+ return removed ? Promise.reject(gone()) : Promise.resolve();
+ });
+ expect(calls).toBe(1);
+ removed = true;
+ el.style.width = "300px"; // something that changes the report
+ box(el, { x: 10, y: 20, width: 300, height: 145 });
+ await vi.advanceTimersByTimeAsync(400);
+ const afterFirst = calls;
+ expect(afterFirst).toBe(2);
+ expect(warn).toHaveBeenCalledTimes(1);
+ // The handler stays gone: later polls, frames and focus changes post nothing.
+ box(el, { x: 10, y: 20, width: 400, height: 145 });
+ reporter.schedule();
+ reporter.focus("x");
+ await vi.advanceTimersByTimeAsync(5000);
+ expect(calls).toBe(afterFirst);
+ expect(warn).toHaveBeenCalledTimes(1);
+ vi.useRealTimers();
+ await new Promise<void>((r) => setTimeout(r, 20));
+ process.off("unhandledRejection", onUnhandled);
+ expect(unhandled).toEqual([]);
+ });
+
+ it("treats a synchronous throw the same way", async () => {
+ vi.spyOn(console, "warn").mockImplementation(() => {});
+ let calls = 0;
+ setup(() => {
+ calls += 1;
+ throw gone();
+ });
+ await vi.advanceTimersByTimeAsync(5000);
+ expect(calls).toBe(1);
+ });
+
+ it("keeps reporting after any other failure, warning only once", async () => {
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
+ let calls = 0;
+ const { el } = setup(() => {
+ calls += 1;
+ return Promise.reject(new Error("transient"));
+ });
+ for (const w of [300, 320, 340]) {
+ box(el, { x: 10, y: 20, width: w, height: 145 });
+ await vi.advanceTimersByTimeAsync(400);
+ }
+ expect(calls).toBe(4);
+ expect(warn).toHaveBeenCalledTimes(1);
+ });
+ });
+
it("batches what changed, removes slots and relays the host's state and focus", () => {
vi.stubGlobal("innerWidth", 1000);
vi.stubGlobal("innerHeight", 700);
diff --git a/apps/cua-spaces-web/src/lib/stream-surface.ts b/apps/cua-spaces-web/src/lib/stream-surface.ts
index 429580bb83..5ce16fbe72 100644
--- a/apps/cua-spaces-web/src/lib/stream-surface.ts
+++ b/apps/cua-spaces-web/src/lib/stream-surface.ts
@@ -71,7 +71,8 @@ export type StreamSurfaceMessage =
export type SurfacePhase = "connecting" | "live" | "failed";
export interface VideoHandler {
- postMessage(message: StreamSurfaceMessage): void;
+ /** WebKit returns a promise that rejects when the host removed the handler. */
+ postMessage(message: StreamSurfaceMessage): unknown;
}
interface VideoHostWindow {
diff --git a/apps/cua-spaces-web/src/lib/video-slots.ts b/apps/cua-spaces-web/src/lib/video-slots.ts
index bd91bfbbdb..30e45a69d8 100644
--- a/apps/cua-spaces-web/src/lib/video-slots.ts
+++ b/apps/cua-spaces-web/src/lib/video-slots.ts
@@ -6,6 +6,7 @@ import {
type SurfacePhase,
type SurfaceRect,
type SurfaceState,
+ type StreamSurfaceMessage,
type SurfaceTier,
type VideoHandler,
} from "./stream-surface";
@@ -179,6 +180,9 @@ export class VideoSlotReporter {
/** The keyboard was handed back to the page for the modal dialog open now (once per dialog). */
private releasedForModal = false;
private next = 0;
+ /** The host removed its `cuaVideo` handler (its window closed or the page was torn down): nothing is listening any more. */
+ private gone = false;
+ private warned = false;
constructor(
private readonly handler: VideoHandler,
@@ -205,7 +209,37 @@ export class VideoSlotReporter {
/** Gives keys to a slot's Space (null: back to the page). */
focus(id: string | null): void {
- this.handler.postMessage({ type: "focus", surfaceId: id });
+ this.send({ type: "focus", surfaceId: id });
+ }
+
+ /**
+ * Posts to the host and settles the result here. WebKit's `postMessage`
+ * returns a promise that rejects with `InvalidAccessError` once the host
+ * removed the handler (`removeAllScriptMessageHandlers` when its window
+ * closes) or the frame is gone, while this page, its 400 ms poll and its
+ * animation frames keep running: an unhandled rejection on every tick
+ * (CUA-SPACES-DESKTOP-3). A handler that is gone stays gone, so the reporter
+ * stops; any other failure is warned about once and the loop goes on.
+ */
+ private send(message: StreamSurfaceMessage): void {
+ if (this.gone) return;
+ const fail = (err: unknown): void => {
+ if ((err as { name?: string } | null)?.name === "InvalidAccessError") {
+ this.gone = true;
+ this.stop();
+ }
+ if (this.warned) return;
+ this.warned = true;
+ console.warn("[video] the host's video handler rejected a message", err);
+ };
+ try {
+ const result: unknown = this.handler.postMessage(message);
+ if (result && typeof (result as PromiseLike<unknown>).then === "function") {
+ (result as PromiseLike<unknown>).then(undefined, fail);
+ }
+ } catch (err) {
+ fail(err);
+ }
}
/** The id of the slot `el` registered as, for `focus`. */
@@ -216,6 +250,7 @@ export class VideoSlotReporter {
/** Measure on the next frame, and for a few frames after. */
schedule = (): void => {
+ if (this.gone) return;
this.settle = SETTLE_FRAMES;
if (!this.frame) this.frame = this.win.requestAnimationFrame(this.tick);
};
@@ -223,7 +258,7 @@ export class VideoSlotReporter {
private tick = (): void => {
this.frame = 0;
this.flush();
- if (this.settle > 0) {
+ if (this.settle > 0 && !this.gone) {
this.settle -= 1;
this.frame = this.win.requestAnimationFrame(this.tick);
}
@@ -231,6 +266,7 @@ export class VideoSlotReporter {
/** Measures every slot now and sends what changed. */
flush(): void {
+ if (this.gone) return;
// A modal dialog takes the keyboard from a viewer that has it (once; the host answers `video.focus`).
const modal = modalOpen(this.win.document);
if (modal && this.focused && !this.releasedForModal) {
@@ -247,7 +283,7 @@ export class VideoSlotReporter {
}
const remove = this.removed;
this.removed = [];
- if (update.length || remove.length) this.handler.postMessage({ type: "surfaces", update, remove });
+ if (update.length || remove.length) this.send({ type: "surfaces", update, remove });
}
private measure(slot: Slot): SurfaceState {
@@ -312,6 +348,7 @@ export class VideoSlotReporter {
};
private start(): void {
+ if (this.gone) return;
const w = this.win;
if (typeof ResizeObserver !== "undefined") {
this.resize = new ResizeObserver(this.schedule);
@@ -354,6 +391,7 @@ export class VideoSlotReporter {
this.dpr = null;
if (this.poll) clearInterval(this.poll);
this.poll = null;
+ this.settle = 0;
}
}