diff --git a/scripts/verify/architecture-guard-baselines.json b/scripts/verify/architecture-guard-baselines.json index cf94e558a9f..35b688f8a0a 100644 --- a/scripts/verify/architecture-guard-baselines.json +++ b/scripts/verify/architecture-guard-baselines.json @@ -167,7 +167,6 @@ "src/server/registerSessionRecordTools.ts": 1, "src/server/runDerivedOperation.ts": 2, "src/signals/SigrokRecordingProvider.ts": 1, - "src/sqlite/SqliteDatabaseFailures.ts": 1, "src/sqlite/SqliteDatabaseProvider.ts": 4, "src/sqlite/SqliteDatabaseWorkerExecution.ts": 1 }, @@ -387,5 +386,12 @@ }, "wall-clock-deadline": { "src/browser/PlaywrightScenarioRequest.ts": 1 + }, + "abort-error-name": { + "src/evm/EvmoleInterfaceProvider.ts": 1, + "src/ida/IdaBusyHealth.ts": 1, + "src/process/ExecFileOutput.ts": 1, + "src/server/http/listenHttpServer.ts": 1, + "src/sqlite/SqliteDatabaseFailures.ts": 1 } } diff --git a/scripts/verify/architecture-guards.mjs b/scripts/verify/architecture-guards.mjs index 4bc14463f87..b5853f66eb0 100644 --- a/scripts/verify/architecture-guards.mjs +++ b/scripts/verify/architecture-guards.mjs @@ -234,6 +234,16 @@ checkCounted( ["src/domain/analysisErrorCore.ts"], ); +// Whether an abort error is a signal's own cancellation is decided by the +// signal owner; a name comparison alone accepts another signal's abort. +checkCounted( + "AbortError name comparison (use isAbortOf or isAbortWithReason from src/process/SignalAbort)", + "abort-error-name", + files, + /\.name\s*===\s*["']AbortError["']/gu, + ["src/process/SignalAbort.ts"], +); + // A deadline is a monotonic budget owned by `Deadline` in src/domain/deadline; // a wall-clock sum moves with clock changes and has no finalize phase. The // remaining copy converts a monotonic budget for a browser page, which can diff --git a/src/flutter/FlutterBuildProvider.ts b/src/flutter/FlutterBuildProvider.ts index 37826c3bf26..b106ff7170d 100644 --- a/src/flutter/FlutterBuildProvider.ts +++ b/src/flutter/FlutterBuildProvider.ts @@ -42,6 +42,7 @@ import { cancelledBy, carriedReason, } from "../domain/analysisErrorCore.js"; +import { isAbortWithReason } from "../process/SignalAbort.js"; import { compareUnicodeCodePoints } from "../domain/unicodeCodePointOrder.js"; import { fixedAllowanceOptions as flutterBudget } from "../domain/fixedAllowanceRemediation.js"; import { @@ -113,11 +114,7 @@ const sha256File = async ( } return hash.digest("hex"); } catch (cause) { - if ( - cause instanceof Error && - cause.name === "AbortError" && - cause.cause === marker - ) + if (isAbortWithReason(cause, marker)) throw cancelledBy(operation, carriedReason(signal?.reason)); throw cause; } finally { diff --git a/src/sqlite/SqliteDatabaseFailures.test.ts b/src/sqlite/SqliteDatabaseFailures.test.ts new file mode 100644 index 00000000000..74f0f48c87f --- /dev/null +++ b/src/sqlite/SqliteDatabaseFailures.test.ts @@ -0,0 +1,50 @@ +import { setTimeout as delay } from "node:timers/promises"; + +import { expect, it } from "vitest"; + +import { AnalysisCancelledError } from "../domain/analysisErrorCore.js"; +import { classifyProviderFailure } from "../providers/classifyProviderFailure.js"; +import { sqliteDatabaseDialect } from "./SqliteDatabaseFailures.js"; + +/** The abort error Node's signal-aware I/O rejects with for `signal`. */ +const nodeAbortError = (signal: AbortSignal): Promise => + delay(1, undefined, { signal }).then( + () => { + throw new Error("Expected the aborted delay to reject"); + }, + (cause: unknown) => cause, + ); + +it.each([ + ["an object", { source: "caller" }], + ["NaN", Number.NaN], +])( + "reports the caller's own abort with its reason when the reason is %s", + async (_label, reason) => { + const caller = new AbortController(); + caller.abort(reason); + const failure = classifyProviderFailure( + await nodeAbortError(caller.signal), + sqliteDatabaseDialect("/selected.db", "artifact-read", caller.signal), + ); + + expect(failure).toBeInstanceOf(AnalysisCancelledError); + expect(Object.is(failure.cause, reason)).toBe(true); + }, +); + +it("keeps another signal's abort error as the cancellation's cause", async () => { + const caller = new AbortController(); + caller.abort({ source: "caller" }); + const other = new AbortController(); + other.abort({ source: "another owner" }); + const foreign = await nodeAbortError(other.signal); + + const failure = classifyProviderFailure( + foreign, + sqliteDatabaseDialect("/selected.db", "artifact-read", caller.signal), + ); + + expect(failure).toBeInstanceOf(AnalysisCancelledError); + expect(failure.cause).toBe(foreign); +}); diff --git a/src/sqlite/SqliteDatabaseFailures.ts b/src/sqlite/SqliteDatabaseFailures.ts index 7436119ad87..9fae4f2ddfd 100644 --- a/src/sqlite/SqliteDatabaseFailures.ts +++ b/src/sqlite/SqliteDatabaseFailures.ts @@ -17,8 +17,10 @@ import type { JsonValue } from "../domain/jsonValue.js"; import { AnalysisArtifactChangedError, AnalysisCapabilityUnavailableError, - AnalysisCancelledError, + cancelledBy, + carriedReason, } from "../domain/analysisErrorCore.js"; +import { isAbortOf } from "../process/SignalAbort.js"; import { fixedAllowanceOptions } from "../domain/fixedAllowanceRemediation.js"; import { ProviderAdapterError } from "../domain/providerAdapterError.js"; import { ProviderCleanupError } from "../domain/providerCleanupError.js"; @@ -129,13 +131,11 @@ export const sqliteDatabaseDialect = ( ? undefined : stableArtifactInputFailure(cause, OPERATION); } + // This signal's own abort carries its reason; another abort error + // is still a cancellation, reported as itself. + if (isAbortOf(cause, signal)) return cancelledBy(OPERATION, signal); if (cause instanceof Error && cause.name === "AbortError") - return new AnalysisCancelledError(OPERATION, { - cause: - signal?.aborted === true && cause.cause === signal.reason - ? signal.reason - : cause, - }); + return cancelledBy(OPERATION, carriedReason(cause)); // Windows reports a sharing violation as EBUSY: another process holds // the file without read sharing. The unchanged request succeeds once // that process releases it. Name the member of the file set that was diff --git a/src/wasm/WabtArtifactProvider.ts b/src/wasm/WabtArtifactProvider.ts index ddada057c71..4cacbae06fb 100644 --- a/src/wasm/WabtArtifactProvider.ts +++ b/src/wasm/WabtArtifactProvider.ts @@ -35,6 +35,7 @@ import { AnalysisInputError, AnalysisOutputError, } from "../domain/analysisErrorCore.js"; +import { isAbortOf } from "../process/SignalAbort.js"; import { ProviderCleanupError } from "../domain/providerCleanupError.js"; import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.js"; import { analysisErrorWithCancellation } from "../domain/analysisErrorCancellation.js"; @@ -520,12 +521,7 @@ export class WabtArtifactProvider implements WasmArtifactPort { }).catch((cause: unknown) => { // Node rejects a write this signal aborted with an envelope that // carries the signal's own reason; any other failure stays itself. - if ( - isAborted(signal) && - cause instanceof Error && - cause.cause === signal.reason - ) - throw cancelledBy(OPERATION, signal); + if (isAbortOf(cause, signal)) throw cancelledBy(OPERATION, signal); throw cause; }); snapshotHash = selected.sha256;