Files
reverse-engineering-anything/src/browser/PlaywrightExecutionBoundary.ts
T
morluto b3cdbb92ee refactor(browser): build browser cancellations with a required reason (#10581)
* refactor(errors): require a reason for every analysis cancellation

Add cancelledBy, the one constructor for AnalysisCancelledError. Its
reason argument is required: an aborted signal, an earlier cancellation,
a reason carried from an inner failure, or an OwnerCancellation when REA
closed the work's owner itself.

Closed adb, apktool, firmware, sqlite, gdb, web-bundle analyzer and
binary-session record owners now report OwnerCancellation.ownerClosed
instead of an absent reason. Firmware's predecessor wait returns its
cancellation as a Result, removing an unreachable reason-less fallback.

An architecture guard rejects new direct constructions, with a counted
per-file baseline that can only shrink.

* refactor(errors): share one aborted-signal predicate with cancelledBy

* refactor(process): return a departing caller's reason from a shared startup wait

* refactor(browser): build browser cancellations with a required reason

Migrate every AnalysisCancelledError construction under src/browser,
src/javascript, src/electron, src/inspector and src/captures to
cancelledBy and lower the guard baseline from 671 to 572. Service-worker
setup aborts with OwnerCancellation.ownerClosed on close.

* refactor(providers): build decompiler-provider cancellations with a required reason (#10587)

Migrate every AnalysisCancelledError construction under src/ghidra,
src/ida, src/cutter, src/jeb and src/kuna to cancelledBy and lower the
guard baseline from 671 to 572. Closed Kuna target commands report
OwnerCancellation.ownerClosed instead of an absent reason, and Kuna and
JEB close abort their lifetimes with that typed reason.

* fix(errors): read the reason of any signal-shaped cancellation source

* test(ci): cover the counted-baseline comparison behind the architecture guard

* refactor(electron): drop an unused error import from the fuse failure mapper
2026-11-15 17:46:20 +08:00

78 lines
2.3 KiB
TypeScript

import { MAX_NODE_TIMER_DELAY_MS } from "../domain/analysisResourceLimits.js";
import {
AnalysisTimeoutError,
cancelledBy,
carriedReason,
} from "../domain/analysisErrorCore.js";
export const PLAYWRIGHT_NETWORK_READ_TIMEOUT_MS = 5_000;
const OPERATION = "capture_browser_scenario" as const;
// Chrome shutdown can take several seconds; expiry bounds the response, not ownership.
export const PLAYWRIGHT_CLEANUP_CONFIRMATION_TIMEOUT_MS = 30_000;
export const withPlaywrightExecutionBoundary = <Value>(
work: () => Promise<Value>,
timeoutMs: number | undefined,
signal?: AbortSignal,
timeoutError: () => AnalysisTimeoutError = () =>
new AnalysisTimeoutError(OPERATION, timeoutMs ?? 0),
): Promise<Value> => {
if (timeoutMs !== undefined && timeoutMs <= 0)
return Promise.reject(timeoutError());
if (signal?.aborted === true)
return Promise.reject(cancelledBy(OPERATION, signal));
return new Promise<Value>((resolve, reject) => {
let settled = false;
let timer: ReturnType<typeof setTimeout> | undefined;
const deadline =
timeoutMs === undefined ? undefined : performance.now() + timeoutMs;
const scheduleTimeout = (): void => {
if (deadline === undefined) return;
const remaining = deadline - performance.now();
if (remaining <= 0) {
fail(timeoutError());
return;
}
timer = setTimeout(
scheduleTimeout,
Math.min(remaining, MAX_NODE_TIMER_DELAY_MS),
);
timer.unref();
};
const cleanup = (): void => {
if (timer !== undefined) clearTimeout(timer);
signal?.removeEventListener("abort", onAbort);
};
const succeed = (value: Value): void => {
if (settled) return;
settled = true;
cleanup();
resolve(value);
};
const fail = (error: unknown): void => {
if (settled) return;
settled = true;
cleanup();
reject(error);
};
const onAbort = (): void => {
fail(cancelledBy(OPERATION, carriedReason(signal?.reason)));
};
scheduleTimeout();
if (settled) return;
signal?.addEventListener("abort", onAbort, { once: true });
if (signal?.aborted === true) {
onAbort();
return;
}
try {
void work().then(succeed, fail);
} catch (cause: unknown) {
fail(cause);
}
});
};