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
This commit is contained in:
morluto authored and GitHub committed 2026-11-14 15:20:03 -05:00
1 parent 2f4c588584
commit 85da0bd4ee
51 files changed
+900 -488

No files matched your search

+24 -19
View File
@@ -19,11 +19,13 @@ import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.
import {
AnalysisCancelledError,
AnalysisOutputError,
cancelledBy,
OwnerCancellation,
} from "../domain/analysisErrorCore.js";
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
import { jsonValueSchema } from "../domain/jsonValue.js";
import { err, ok } from "../domain/result.js";
import { err, ok, type Result } from "../domain/result.js";
import { PrivateRuntimeRoot } from "../process/PrivateRuntimeRoot.js";
import type { ProviderProcessSupervisor } from "../process/ProviderProcess.js";
import { SafeOutputTree } from "../artifacts/SafeOutputTree.js";
@@ -116,17 +118,18 @@ const waitForPredecessor = (
predecessor: Promise<void>,
operation: string,
signal?: AbortSignal,
): Promise<void> =>
new Promise((resolve, reject) => {
const abort = () =>
reject(new AnalysisCancelledError(operation, { cause: signal?.reason }));
signal?.addEventListener("abort", abort, { once: true });
if (signal?.aborted === true) abort();
): Promise<Result<void, AnalysisCancelledError>> => {
if (signal === undefined) return predecessor.then(() => ok(undefined));
return new Promise((resolve) => {
const abort = () => resolve(err(cancelledBy(operation, signal)));
signal.addEventListener("abort", abort, { once: true });
if (signal.aborted) abort();
void predecessor.then(() => {
signal?.removeEventListener("abort", abort);
resolve();
signal.removeEventListener("abort", abort);
resolve(ok(undefined));
});
});
};
const limitations = [
"Signature matches and reported sizes do not establish partition validity, encryption or runtime mappings. Binwalk may infer unknown lengths before emitting its report.",
"Only Linux is supported by this integration. One worker, inherited per-process address-space/file-size/CPU limits and a deadline constrain execution; staging byte and entry budgets are polled and may transiently overshoot. Limits are not aggregate process-memory or filesystem quotas.",
@@ -166,26 +169,28 @@ export class FirmwareProvider implements FirmwareAnalysisPort {
request: FirmwareRequest,
options?: ExecutionOptions,
): Promise<Outcome> {
if (this.#closed) return err(new AnalysisCancelledError(request.operation));
if (this.#closed)
return err(cancelledBy(request.operation, OwnerCancellation.ownerClosed));
const predecessor = this.#tail;
let release = () => {};
this.#tail = new Promise<void>((resolve) => {
release = resolve;
});
// Cancel the caller promptly without letting a later request bypass its predecessor.
try {
await waitForPredecessor(predecessor, request.operation, options?.signal);
} catch (cause: unknown) {
const waited = await waitForPredecessor(
predecessor,
request.operation,
options?.signal,
);
if (!waited.ok) {
void predecessor.then(release);
return err(
cause instanceof AnalysisError
? cause
: new AnalysisCancelledError(request.operation),
);
return err(waited.error);
}
try {
if (this.#closed)
return err(new AnalysisCancelledError(request.operation));
return err(
cancelledBy(request.operation, OwnerCancellation.ownerClosed),
);
const cleanup = await this.#retryCleanup();
if (cleanup.executionBlocked && cleanup.failure !== undefined)
return err(cleanup.failure);