fix(android): retain mapping read failures through late cancellation (#12329)

This commit is contained in:
morluto authored and GitHub committed 2026-11-19 12:50:27 +00:00
1 parent ddcd62009b
commit 45046356c5
3 files changed
+107 -24

No files matched your search

+25 -21
View File
@@ -1,8 +1,8 @@
import {
artifactFileCancellationCause,
artifactFilePrimaryFacts,
bindArtifactFileCleanup,
ArtifactReaderFailure,
projectStableArtifactFailure,
stableArtifactInputRecovery,
readerIntegrityFailure,
type ArtifactResourceScope,
@@ -14,9 +14,9 @@ import {
} from "../domain/android/androidMapping.js";
import { parseProguardMapping } from "../domain/android/proguardMapping.js";
import type { AnalysisError } from "../domain/analysisErrorBase.js";
import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.js";
import {
AnalysisAccessDeniedError,
AnalysisCancelledError,
AnalysisInputError,
AnalysisResourceConstraintError,
AnalysisTimeoutError,
@@ -24,7 +24,6 @@ import {
carriedReason,
} from "../domain/analysisErrorCore.js";
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
import { err, ok, type Result } from "../domain/result.js";
import { isSelectedRegularFileError } from "../filesystem/RegularFile.js";
import { jadxOperationTimeoutGuidance } from "./JadxOperationTimeout.js";
@@ -116,8 +115,13 @@ const readFailurePrimary = (
{ cause: primary },
);
}
// A closed resource scope during shutdown is cancellation, not a file fault.
if (read.signal.aborted) return mappingInterruption(read, read.signal.reason);
// The scope can refuse admission during shutdown before any physical read.
if (
owned?.reason === "unavailable" &&
owned.cleanup === undefined &&
read.signal.aborted
)
return mappingInterruption(read, read.signal.reason);
return new ProviderAdapterError("jadx", read.operation, {
cause: primary,
diagnostics: {
@@ -127,24 +131,24 @@ const readFailurePrimary = (
});
};
/** Compose the read failure with any descriptor cleanup the scope retained. */
/** Preserve the read's outcome, any later interruption, and its descriptor lease. */
const readFailure = (cause: unknown, read: MappingRead): AnalysisError => {
const primary = readFailurePrimary(cause, read);
const error =
cause instanceof ArtifactReaderFailure && cause.cleanup !== undefined
? analysisErrorWithCleanupFailure(
primary,
new ProviderCleanupError(
"jadx",
cause.cleanup.resources,
{ reason: cause.cleanup.reason },
{ operation: read.operation, cause },
),
read.operation,
)
: primary;
bindArtifactFileCleanup(cause, error);
return error;
if (
read.signal.aborted &&
!(primary instanceof AnalysisCancelledError) &&
!(primary instanceof AnalysisTimeoutError)
)
primary.retainSecondaryFailure(
mappingInterruption(read, read.signal.reason),
);
return projectStableArtifactFailure(
cause,
primary,
read.path,
"jadx",
read.operation,
);
};
/**
@@ -1,7 +1,14 @@
import { createHash } from "node:crypto";
import { stat, unlink, utimes, writeFile } from "node:fs/promises";
import {
open,
stat,
unlink,
utimes,
writeFile,
type FileHandle,
} from "node:fs/promises";
import { join } from "node:path";
import { expect, it, onTestFinished } from "vitest";
import { expect, it, onTestFinished, vi } from "vitest";
import { readAndroidMapping } from "../../../src/android/AndroidMappingFile.js";
import { ArtifactResourceScope } from "../../../src/resources/ArtifactResourceScope.js";
import {
@@ -9,11 +16,17 @@ import {
AnalysisInputError,
AnalysisResourceConstraintError,
} from "../../../src/domain/analysisErrorCore.js";
import { ProviderAdapterError } from "../../../src/domain/providerAdapterError.js";
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
import { ANDROID_MAPPING_LIMITS } from "../../../src/domain/android/androidMapping.js";
import { resolveProguardClass } from "../../../src/domain/android/proguardMapping.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
vi.mock("node:fs/promises", async (importOriginal) => {
const actual = await importOriginal<typeof import("node:fs/promises")>();
return { ...actual, open: vi.fn(actual.open) };
});
const SOURCE = "# compiler: R8\nexample.Old -> a:\n int value -> b\n";
const OPERATION = "inspect_android_class";
const digest = (text: string): string =>
@@ -172,6 +185,64 @@ it("preserves the caller cancellation cause instead of returning a cached mappin
expect(resources.hasPending).toBe(false);
});
it.each([false, true])(
"preserves physical mapping read failure and late cancellation (close fails: %s)",
async (closeFails) => {
const { path, resources } = await fixture();
const controller = new AbortController();
const reason = { caller: "saved late cancellation" };
const physical = Object.assign(new Error("mapping read failed"), {
code: "EIO",
});
const closeFailure = new Error("mapping descriptor close failed");
let allowClose = !closeFails;
let file: FileHandle | undefined;
const actual =
await vi.importActual<typeof import("node:fs/promises")>(
"node:fs/promises",
);
vi.mocked(open).mockImplementationOnce(async (...args) => {
const handle = await actual.open(...args);
file = handle;
vi.spyOn(handle, "read").mockRejectedValue(physical);
const close = handle.close.bind(handle);
vi.spyOn(handle, "close").mockImplementation(async () => {
controller.abort(reason);
if (!allowClose) throw closeFailure;
await close();
});
return handle;
});
try {
const result = await readAndroidMapping(
path,
OPERATION,
resources,
controller.signal,
);
if (result.ok) throw new Error("Expected the physical mapping failure");
const primary = result.error.cleanupPrimary ?? result.error;
expect(primary).toBeInstanceOf(ProviderAdapterError);
expect(primary.cause).toBe(physical);
expect(primary.secondaryFailures).toHaveLength(1);
expect(primary.secondaryFailures[0]).toBeInstanceOf(
AnalysisCancelledError,
);
expect(primary.secondaryFailures[0]?.cause).toBe(reason);
expect(result.error.cleanupIncomplete).toBe(closeFails);
expect(resources.cleanupResources).toEqual(closeFails ? [path] : []);
if (closeFails) expect(file?.fd).toBeGreaterThanOrEqual(0);
allowClose = true;
await resources.retryCleanup();
expect(result.error.cleanupIncomplete).toBe(false);
expect(file?.fd).toBe(-1);
} finally {
allowClose = true;
await resources.close();
}
},
);
it("refuses expanded shared-name claims from a small mapping with auditable nonretryable capacity facts", async () => {
// One source name expands into every mapped field claim. Eight fields are
// enough to cross the projection bound without a large record inventory.
@@ -1,3 +1,4 @@
import { createHash } from "node:crypto";
import { stat, writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { expect, it, onTestFinished, vi } from "vitest";
@@ -23,7 +24,8 @@ it.each(
{ REA_JADX_OPERATION_TIMEOUT_MS: "2000" },
);
const mapping = join(dirname(apk), "mapping.txt");
await writeFile(mapping, "example.Original -> fixture.Target:\n");
const mappingText = "example.Original -> fixture.Target:\n";
await writeFile(mapping, mappingText);
// Resolve the actual APK admission before the descriptor seam is active.
const target = await parseBinaryTarget(apk);
if (!target.ok) throw target.error;
@@ -124,6 +126,12 @@ it.each(
expect(primary.cause).toBe(
first === "caller" ? callerReason : deadlineReason,
);
expect(primary.partialObservation).toMatchObject({
kind: "file-snapshot-hash",
path: mapping,
bytes: Buffer.byteLength(mappingText),
sha256: createHash("sha256").update(mappingText).digest("hex"),
});
const expectedCode =
first === "caller" ? "cancelled" : "provider_timeout";
if (cleanupFails) {