Skip to content

Commit d293589

Browse files
committed
fix(preview): make press cleanup interruption safe
Run preview press key-up and focus-emulation cleanup in an uninterruptible region so cancellation cannot leave Chromium with held input or stale focus emulation. Add deterministic Deferred-backed cancellation coverage at the cleanup boundary. Model: GPT-5 Harness: Codex CLI
1 parent 947a217 commit d293589

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3510,6 +3510,12 @@ describe("PreviewManager", () => {
35103510
Effect.gen(function* () {
35113511
let failKeyDown = false;
35123512
let interruptPress = false;
3513+
let blockCleanup = false;
3514+
const cleanupStarted = Deferred.makeUnsafe<void>();
3515+
let releaseCleanup!: () => void;
3516+
const cleanupRelease = new Promise<void>((resolve) => {
3517+
releaseCleanup = resolve;
3518+
});
35133519
let humanInput: ((_event: unknown, signal: unknown) => void) | undefined;
35143520
const sendCommand = vi.fn(async (method: string, params?: Record<string, unknown>) => {
35153521
if (
@@ -3534,6 +3540,10 @@ describe("PreviewManager", () => {
35343540
},
35353541
);
35363542
}
3543+
if (blockCleanup && method === "Input.dispatchKeyEvent" && params?.["type"] === "keyUp") {
3544+
Deferred.doneUnsafe(cleanupStarted, Effect.void);
3545+
await cleanupRelease;
3546+
}
35373547
return method === "Runtime.evaluate" ? { result: { value: { ok: true } } } : undefined;
35383548
});
35393549
const restoreFocus = vi.fn();
@@ -3728,6 +3738,35 @@ describe("PreviewManager", () => {
37283738
webContentsId: 42,
37293739
});
37303740
expect(restoreFocus).toHaveBeenCalledTimes(3);
3741+
3742+
sendCommand.mockClear();
3743+
getFocusedWebContents.mockReset();
3744+
getFocusedWebContents.mockReturnValueOnce(previousFocused).mockReturnValue(focusedGuest);
3745+
interruptPress = false;
3746+
blockCleanup = true;
3747+
const cleanupPress = yield* manager
3748+
.automationPress("tab_input", { key: "~" })
3749+
.pipe(Effect.forkChild);
3750+
yield* Deferred.await(cleanupStarted);
3751+
const interruptCleanup = yield* Effect.forkChild(Fiber.interrupt(cleanupPress));
3752+
yield* Effect.yieldNow;
3753+
releaseCleanup();
3754+
yield* Fiber.join(interruptCleanup);
3755+
3756+
const cleanupExit = yield* Fiber.await(cleanupPress);
3757+
expect(Exit.isFailure(cleanupExit)).toBe(true);
3758+
expect(sendCommand).toHaveBeenCalledWith("Input.dispatchKeyEvent", {
3759+
type: "keyUp",
3760+
key: "~",
3761+
code: "Backquote",
3762+
modifiers: 0,
3763+
windowsVirtualKeyCode: 192,
3764+
location: 0,
3765+
isKeypad: false,
3766+
});
3767+
expect(sendCommand).toHaveBeenCalledWith("Emulation.setFocusEmulationEnabled", {
3768+
enabled: false,
3769+
});
37313770
}),
37323771
),
37333772
);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3943,7 +3943,10 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
39433943
isPreviewAutomationControlInterruptedError(
39443944
Option.getOrNull(Cause.findErrorOption(pressExit.cause)),
39453945
);
3946-
yield* releaseInput(!interrupted);
3946+
// Input cleanup must finish even when the action fiber is interrupted while
3947+
// a CDP cleanup command is in flight. Otherwise Chromium can retain a held
3948+
// key or focus emulation state after the preview loses control.
3949+
yield* Effect.uninterruptible(releaseInput(!interrupted));
39473950
if (pressExit._tag === "Failure") return yield* Effect.failCause(pressExit.cause);
39483951
return focusDisposition;
39493952
});

0 commit comments

Comments
 (0)