Skip to content

Commit 4e2a566

Browse files
committed
fix: retain cancellation during Electron handle finalization
1 parent 5eada7e commit 4e2a566

2 files changed

Lines changed: 58 additions & 2 deletions

File tree

‎src/browser/PlaywrightElectronActiveActions.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,3 +748,40 @@ it.each(["acquisition", "identity"] as const)(
748748
expect(dispose).toHaveBeenCalledOnce();
749749
},
750750
);
751+
752+
it("retains the caller reason when cancellation precedes failed handle disposal", async () => {
753+
const controller = new AbortController();
754+
const reason = { stop: "during handle disposal" };
755+
const dispose = vi
756+
.fn()
757+
.mockImplementationOnce(async () => {
758+
controller.abort(reason);
759+
throw new Error("dispose rejected");
760+
})
761+
.mockResolvedValue(undefined);
762+
const page = { locator: () => ({ click: async () => {} }) };
763+
const resources = new ArtifactResourceScope();
764+
const outcome = await runElectronActions(
765+
{
766+
windows: () => [page],
767+
browserWindow: async () => ({ evaluate: async () => 99, dispose }),
768+
} as unknown as ElectronApplication,
769+
electronActiveObservationInputSchema.parse({
770+
executable_path: "/opt/electron",
771+
application_path: "/opt/app/main.js",
772+
application_root: "/opt/app",
773+
actions: [{ step_id: "selected", kind: "click", selector: "#selected" }],
774+
}),
775+
{ signal: controller.signal },
776+
resources,
777+
);
778+
expect(outcome.actions[0]).toMatchObject({
779+
status: "completed",
780+
target: "window:99",
781+
});
782+
expect(outcome.cleanupError?.cleanupPrimary?.cause).toBe(reason);
783+
expect(outcome.cleanupError?.cleanupIncomplete).toBe(true);
784+
await resources.close();
785+
expect(outcome.cleanupError?.cleanupIncomplete).toBe(false);
786+
expect(outcome.cleanupError?.cleanupPrimary?.cause).toBe(reason);
787+
});

‎src/browser/PlaywrightElectronActiveActions.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import type {
99
} from "../artifacts/ArtifactResourceScope.js";
1010
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
1111
import type { AnalysisError } from "../domain/analysisErrorBase.js";
12+
import { AnalysisCancelledError } from "../domain/analysisErrorCore.js";
13+
import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.js";
1214
import { BrowserObservationError } from "../domain/browserObservationError.js";
1315
import {
1416
electronActiveTimelineEventSchema,
@@ -159,7 +161,12 @@ export const runElectronActions = async (
159161
error: actionError,
160162
});
161163
if (handleOwner !== undefined) {
162-
cleanupError = await releaseActionWindow(resources, handleOwner);
164+
cleanupError = await releaseActionWindow(
165+
resources,
166+
handleOwner,
167+
stoppedBy ?? "completed",
168+
options.signal,
169+
);
163170
if (cleanupError !== undefined) stoppedBy = "failed";
164171
}
165172
}
@@ -206,6 +213,8 @@ const ownActionWindow = (
206213
const releaseActionWindow = async (
207214
resources: ArtifactResourceScope,
208215
owner: ArtifactResourceOwner,
216+
actionStatus: ElectronActions[number]["status"],
217+
signal: AbortSignal | undefined,
209218
): Promise<AnalysisError | undefined> => {
210219
const released = await resources.release(owner);
211220
if (released.kind === "released") return undefined;
@@ -219,7 +228,17 @@ const releaseActionWindow = async (
219228
);
220229
error.bindCleanupResourceOwner(released.lease);
221230
released.lease.bindCleanupError(error);
222-
return error;
231+
if (!signal?.aborted) return error;
232+
const cancellation = new AnalysisCancelledError(OPERATION, {
233+
cause: signal.reason,
234+
});
235+
return actionStatus === "failed"
236+
? error.retainSecondaryFailure(cancellation)
237+
: analysisErrorWithCleanupFailure(
238+
cancellation,
239+
error,
240+
OPERATION,
241+
).bindCleanupDependencies([cancellation, error]);
223242
};
224243

225244
type RunActionContext = {

0 commit comments

Comments
 (0)