Files
reverse-engineering-anything/src/process/SharedStartupAttempt.test.ts
T
morluto 85da0bd4ee refactor(errors): require a reason for every analysis cancellation (#10566)
* 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(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
2026-11-14 15:20:03 -05:00

114 lines
3.9 KiB
TypeScript

import { expect, it } from "vitest";
import { createDeferred } from "../../tests/fixtures/binarySession.js";
import {
SharedStartupAttempt,
STARTUP_OWNER_CLOSED,
} from "./SharedStartupAttempt.js";
it("keeps shared startup running for an interested peer and returns the departing caller's reason", async () => {
const completed = createDeferred<string>();
const attempt = new SharedStartupAttempt(() => completed.promise);
const controller = new AbortController();
const departing = attempt.wait(controller.signal);
const staying = attempt.wait();
const reason = { caller: "departing" };
controller.abort(reason);
const departed = await departing;
expect(departed).toEqual({ status: "cancelled", reason });
expect(departed.status === "cancelled" ? departed.reason : undefined).toBe(
reason,
);
expect(attempt.signal.aborted).toBe(false);
completed.resolve("ready");
await expect(staying).resolves.toEqual({
status: "completed",
value: "ready",
});
});
it("retains the final cleanup result when all callers cancel together", async () => {
const cleanup = createDeferred<{ cleanupIncomplete: boolean }>();
const attempt = new SharedStartupAttempt(() => cleanup.promise);
const first = new AbortController();
const last = new AbortController();
const waits = [attempt.wait(first.signal), attempt.wait(last.signal)];
const firstReason = new Error("first caller left");
first.abort(firstReason);
last.abort();
await expect(waits[0]).resolves.toEqual({
status: "cancelled",
reason: firstReason,
});
expect(attempt.signal.aborted).toBe(true);
expect(attempt.settled).toBe(false);
const failure = { cleanupIncomplete: true };
cleanup.resolve(failure);
await expect(waits[1]).resolves.toEqual({
status: "stopped",
value: failure,
});
expect(attempt.settled).toBe(true);
});
it("distinguishes provider closure from a caller's cancellation", async () => {
const cleanup = createDeferred<string>();
const attempt = new SharedStartupAttempt(() => cleanup.promise);
const controller = new AbortController();
const waiting = attempt.wait(controller.signal);
attempt.close();
expect(attempt.signal.reason).toBe(STARTUP_OWNER_CLOSED);
expect(controller.signal.aborted).toBe(false);
cleanup.resolve("provider stopped");
await expect(waiting).resolves.toEqual({
status: "completed",
value: "provider stopped",
});
});
it("does not lose an unexpected producer rejection after its final caller cancels", async () => {
let reject: ((cause: unknown) => void) | undefined;
const promise = new Promise<never>((_resolve, fail) => {
reject = fail;
});
const attempt = new SharedStartupAttempt(() => promise);
const controller = new AbortController();
const waiting = attempt.wait(controller.signal);
controller.abort();
const cause = new TypeError("Cleanup rejected");
reject?.(cause);
await expect(waiting).rejects.toBe(cause);
});
it.each(["caller", "owner"])(
"preserves %s interruption when owning cancellation and closure occur together",
async (first) => {
const completed = createDeferred<string>();
const controller = new AbortController();
const reason = { caller: "operation owner" };
const attempt = new SharedStartupAttempt(
() => completed.promise,
controller.signal,
);
const waiting = attempt.wait();
if (first === "caller") {
controller.abort(reason);
attempt.close();
} else {
attempt.close();
expect(attempt.signal.aborted).toBe(true);
expect(attempt.signal.reason).toBe(STARTUP_OWNER_CLOSED);
controller.abort(reason);
}
expect(attempt.signal.reason).toBe(
first === "caller" ? reason : STARTUP_OWNER_CLOSED,
);
expect(attempt.settled).toBe(false);
completed.resolve("finalized");
await expect(waiting).resolves.toEqual({
status: "completed",
value: "finalized",
});
},
);