fix(process): classify macOS ownership helper capacity exhaustion as resource constraint (#11771)

* fix(process): classify macOS ownership helper capacity exhaustion as resource constraint

Refs #11679

Refs #11714

* fix(process): exclude cancelled preparation from host capacity detection
This commit is contained in:
morluto authored and GitHub committed 2026-11-16 14:58:30 -05:00
1 parent 0ef08895f1
commit bc8b3c4cfa
11 files changed
+293 -41

No files matched your search

+41
View File
@@ -1,4 +1,5 @@
import { validateJadxConfiguration } from "./JadxConfiguration.js";
import { DarwinProcessOwnershipInspectionError } from "../process/DarwinProcessRunTokenReader.js";
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
import { ArtifactResourceRetryFailure } from "../resources/ArtifactResourceScope.js";
import { expect, it, vi } from "vitest";
@@ -287,3 +288,43 @@ it.each([
}
},
);
it("reports EMFILE during macOS ownership helper preparation as a resource constraint", async () => {
const resources = createInspectionResources();
const emfile = spawnFailure("EMFILE", "/usr/bin/swiftc");
const prepare = vi
.spyOn(systemProcessOwnershipHost, "prepare")
.mockRejectedValue(
new DarwinProcessOwnershipInspectionError("helper capacity exhausted", {
cause: emfile,
}),
);
try {
const result: unknown = await inspectJadxJava(
"/selected/java",
{},
{
resourceOwner: resources.owner,
launcher: async (input) => {
await prepareProcessOwnershipInspection(input.signal);
throw new Error("Java must not start");
},
},
).catch((cause: unknown) => cause);
expect(result).toBeInstanceOf(AnalysisError);
if (!(result instanceof AnalysisError)) throw result;
expect(projectAnalysisError(result)).toMatchObject({
code: "resource_constraint",
retryable: true,
details: {
diagnostics: {
system_code: "EMFILE",
java_executable: "/selected/java",
},
},
});
} finally {
prepare.mockRestore();
await resources.close();
}
});
+9 -2
View File
@@ -24,6 +24,7 @@ import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
import { err, ok, type Result } from "../domain/result.js";
import {
prepareOwnedCommand,
preparationHostCapacityFailure,
runOwnedCommand,
OwnedCommandFailure,
} from "../process/OwnedCommand.js";
@@ -221,7 +222,11 @@ export const inspectJadxJava = async (
},
},
});
} else if (preparation !== undefined && command?.reason !== "timeout") {
} else if (
preparation !== undefined &&
command?.reason !== "timeout" &&
hostConstraint === undefined
) {
primary = new ProviderAdapterError("jadx", operation, {
cause,
...(output === undefined ? {} : { capturedOutput: output }),
@@ -295,7 +300,9 @@ const hostLaunchConstraint = (
command: Pick<OwnedCommandFailure, "processError"> | undefined,
cause: unknown,
) => {
const launch = launcherFailureOf(command?.processError ?? cause);
const launch =
preparationHostCapacityFailure(cause) ??
launcherFailureOf(command?.processError ?? cause);
return launch?.kind === "host-exhausted" || launch?.kind === "argument-list"
? launch
: undefined;
+41
View File
@@ -1,6 +1,7 @@
import * as commandOwner from "../process/OwnedCommand.js";
import { failedInspectionDescriptor } from "../../tests/fixtures/processInspectionCleanup.js";
import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js";
import { spawnFailure } from "../../tests/support/process/spawnFailure.js";
import {
AnalysisError,
primaryAnalysisError,
@@ -9,6 +10,7 @@ import {
ProcessOwnershipIdentityBatchFailure,
ProcessOwnershipInspectionCancelled,
} from "../process/ProcessInspectionCancellation.js";
import { DarwinProcessOwnershipInspectionError } from "../process/DarwinProcessRunTokenReader.js";
import { systemProcessOwnershipHost } from "../process/ProcessOwnershipObservation.js";
import { ApktoolCommandCleanup } from "./ApktoolCommandCleanup.js";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
@@ -663,3 +665,42 @@ it("keeps a recorded command timeout primary with its actual inspection stop fac
prepare.mockRestore();
}
});
it("reports EMFILE during macOS ownership helper preparation as a resource constraint", async () => {
const emfile = spawnFailure("EMFILE", "/usr/bin/swiftc");
const darwin = new DarwinProcessOwnershipInspectionError(
"helper capacity exhausted",
{ cause: emfile },
);
const failure = new commandOwner.OwnedCommandFailure(
"process",
"Ownership preparation failed",
null,
null,
{ preparationFailure: darwin },
);
const prepare = vi
.spyOn(systemProcessOwnershipHost, "prepare")
.mockResolvedValue(undefined);
const command = vi
.spyOn(commandOwner, "runOwnedCommand")
.mockRejectedValue(failure);
try {
const result: unknown = await inspectApktoolClient(
{ command: "/selected/apktool", commandSource: "environment" },
commands,
{ platform: "linux" },
).catch((cause: unknown) => cause);
expect(result).toMatchObject({
_tag: "AnalysisResourceConstraintError",
retryable: true,
diagnostics: {
system_code: "EMFILE",
selected_command: "/selected/apktool",
},
});
} finally {
command.mockRestore();
prepare.mockRestore();
}
});
+30 -31
View File
@@ -26,6 +26,7 @@ import { ProviderStartupDeadline } from "../process/ProviderDeadline.js";
import { randomUUID } from "node:crypto";
import {
OwnedCommandFailure,
preparationHostCapacityFailure,
runOwnedCommand,
prepareOwnedCommand,
} from "../process/OwnedCommand.js";
@@ -488,10 +489,35 @@ const versionProbeFailure = (
)
);
if (inspectionCancellation !== undefined) return inspectionCancellation;
if (
preparation !== undefined &&
!(cause instanceof OwnedCommandFailure && cause.reason === "timeout")
)
if (cause instanceof OwnedCommandFailure && cause.reason === "timeout")
return new AnalysisTimeoutError(
"inspect_apktool_client",
VERSION_TIMEOUT_MS,
{
cause,
...(output === undefined ? {} : { capturedOutput: output }),
...apktoolVersionProbeTimeoutGuidance(),
},
);
const launch =
preparationHostCapacityFailure(cause) ?? launcherFailureOf(cause);
// The host, not the selected launcher, refused this launch.
if (launch?.kind === "host-exhausted" || launch?.kind === "argument-list")
return classifyLaunchFailure(launch, {
providerId: "apktool",
operation: "inspect_apktool_client",
command: selection.command,
fixedArguments: true,
selection: APKTOOL_COMMAND_ENV,
details: {
cause,
diagnostics: {
selected_command: selection.command,
command_source: selection.commandSource,
},
},
});
if (preparation !== undefined)
return new ProviderAdapterError("apktool", "inspect_apktool_client", {
cause,
diagnostics: {
@@ -510,33 +536,6 @@ const versionProbeFailure = (
? {}
: { capturedOutput: preparation.capturedOutput }),
});
if (cause instanceof OwnedCommandFailure && cause.reason === "timeout")
return new AnalysisTimeoutError(
"inspect_apktool_client",
VERSION_TIMEOUT_MS,
{
cause,
...(output === undefined ? {} : { capturedOutput: output }),
...apktoolVersionProbeTimeoutGuidance(),
},
);
const launch = launcherFailureOf(cause);
// The host, not the selected launcher, refused this launch.
if (launch?.kind === "host-exhausted" || launch?.kind === "argument-list")
return classifyLaunchFailure(launch, {
providerId: "apktool",
operation: "inspect_apktool_client",
command: selection.command,
fixedArguments: true,
selection: APKTOOL_COMMAND_ENV,
details: {
cause,
diagnostics: {
selected_command: selection.command,
command_source: selection.commandSource,
},
},
});
if (launch?.kind === "missing" || launch?.kind === "not_executable") {
const execDenied = launch.kind === "not_executable";
return new ApktoolConfigurationFailure(
@@ -71,6 +71,7 @@ import {
import { classifyLaunchFailure } from "../../process/classifyProviderOutcome.js";
import {
OwnedCommandFailure,
preparationHostCapacityFailure,
runOwnedCommand,
} from "../../process/OwnedCommand.js";
import { DarwinProcessOwnershipInspectionError } from "../../process/DarwinProcessRunTokenReader.js";
@@ -780,11 +781,13 @@ export class ReverseEngineeringService {
outputLimitReached = true;
// Host capacity and an oversized argument list are classified by the
// shared launch owner; a refusal keeps this provider's repair below.
const launch = launcherFailureOf(
cause instanceof LinuxOwnedProcessPrerequisiteError
? cause.cause
: cause,
);
const launch =
preparationHostCapacityFailure(ownedCause) ??
launcherFailureOf(
cause instanceof LinuxOwnedProcessPrerequisiteError
? cause.cause
: cause,
);
if (
earlyFailure === undefined &&
(launch?.kind === "host-exhausted" || launch?.kind === "argument-list")
@@ -5,6 +5,7 @@ import {
primaryAnalysisError,
} from "../domain/analysisErrorBase.js";
import { ArtifactResourceRetryFailure } from "../resources/ArtifactResourceScope.js";
import { DarwinProcessOwnershipInspectionError } from "../process/DarwinProcessRunTokenReader.js";
import {
ProcessOwnershipIdentityBatchFailure,
ProcessOwnershipInspectionCancelled,
@@ -16,6 +17,7 @@ import {
import type { OwnedProviderProcessSpawnOptions } from "../process/ProviderProcess.js";
import { failedInspectionDescriptor } from "../../tests/fixtures/processInspectionCleanup.js";
import { createInspectionResources } from "../../tests/fixtures/inspectionResources.js";
import { spawnFailure } from "../../tests/support/process/spawnFailure.js";
it.each([false, true])(
"retains physical preparation ownership before Java launch (batch: %s)",
@@ -141,3 +143,79 @@ it("keeps the owned command deadline primary when launcher inspection is stopped
await resources.close();
}
});
it("reports EMFILE during macOS ownership helper preparation as a resource constraint", async () => {
const resources = createInspectionResources();
const emfile = spawnFailure("EMFILE", "/usr/bin/swiftc");
const prepare = vi
.spyOn(systemProcessOwnershipHost, "prepare")
.mockRejectedValue(
new DarwinProcessOwnershipInspectionError("helper capacity exhausted", {
cause: emfile,
}),
);
try {
const result = await probeGhidraJava(
"/selected/java",
{},
{
resourceOwner: resources.owner,
launcher: async (input) => {
await prepareProcessOwnershipInspection(input.signal);
throw new Error("Java must not start");
},
},
);
expect(result.ok).toBe(false);
if (result.ok) throw new Error("Expected resource constraint failure");
expect(result.error).toMatchObject({
_tag: "AnalysisResourceConstraintError",
retryable: true,
diagnostics: {
system_code: "EMFILE",
java_executable: "/selected/java",
},
});
} finally {
prepare.mockRestore();
await resources.close();
}
});
it("keeps caller cancellation primary when macOS helper preparation is cancelled with a spawn EMFILE reason", async () => {
const resources = createInspectionResources();
const reason = spawnFailure("EMFILE", "/usr/bin/swiftc");
const prepare = vi
.spyOn(systemProcessOwnershipHost, "prepare")
.mockRejectedValue(
new DarwinProcessOwnershipInspectionError(
"helper compilation cancelled",
{
cause: reason,
cancellation: { reason },
},
),
);
try {
const result = await probeGhidraJava(
"/selected/java",
{},
{
resourceOwner: resources.owner,
launcher: async (input) => {
await prepareProcessOwnershipInspection(input.signal);
throw new Error("Java must not start");
},
},
);
expect(result.ok).toBe(false);
if (result.ok) throw new Error("Expected cancelled probe failure");
expect(result.error).toMatchObject({
_tag: "AnalysisCancelledError",
cause: reason,
});
} finally {
prepare.mockRestore();
await resources.close();
}
});
+6 -2
View File
@@ -20,6 +20,7 @@ import type { RetainedResourceScope } from "../ports/AnalysisProvider.js";
import { DarwinProcessOwnershipInspectionError } from "../process/DarwinProcessRunTokenReader.js";
import {
OwnedCommandFailure,
preparationHostCapacityFailure,
runOwnedCommand,
prepareOwnedCommand,
} from "../process/OwnedCommand.js";
@@ -272,7 +273,8 @@ const observeGhidraJava = async (
});
} else if (
preparation !== undefined &&
commandFailure?.reason !== "timeout"
commandFailure?.reason !== "timeout" &&
preparationHostCapacityFailure(cause) === undefined
) {
primary = new ProviderAdapterError("ghidra", "ghidra:java", {
cause,
@@ -292,7 +294,9 @@ const observeGhidraJava = async (
} else if (cause instanceof AnalysisError) {
primary = cause;
}
const launch = launcherFailureOf(commandFailure?.processError ?? cause);
const launch =
preparationHostCapacityFailure(cause) ??
launcherFailureOf(commandFailure?.processError ?? cause);
if (
primary === undefined &&
(launch?.kind === "host-exhausted" || launch?.kind === "argument-list")
+24
View File
@@ -40,6 +40,7 @@ import {
waitForAbortableDelay,
} from "./ProviderDeadline.js";
import { errorMessage } from "../kernel/errorMessage.js";
import { launcherFailureOf, type LauncherFailure } from "./launchFailure.js";
/** A short-lived command failed its lifecycle or complete-output contract. */
export class OwnedCommandFailure extends Error {
@@ -198,6 +199,29 @@ function throwCommandPreparationFailure(
throw cause;
}
/**
* Ownership preparation could not start a process it needed, such as the
* macOS ownership helper's compiler, because the host lacked capacity. The
* command cannot start for the same reason, so callers report it as their own
* host-capacity launch failure. Other preparation failures, including a
* missing helper compiler, say nothing about the command's executable.
*/
export const preparationHostCapacityFailure = (
cause: unknown,
): LauncherFailure | undefined => {
if (cause instanceof OwnedCommandFailure && cause.cancellation !== undefined)
return undefined;
const preparation =
cause instanceof OwnedCommandFailure ? cause.preparationFailure : cause;
if (
!(preparation instanceof DarwinProcessOwnershipInspectionError) ||
preparation.cancellation !== undefined
)
return undefined;
const launch = launcherFailureOf(preparation.cause);
return launch?.kind === "host-exhausted" ? launch : undefined;
};
/** Execute a single owned process with independent cleanup and bounded diagnostic retention. */
export const runOwnedCommand = async (
spawn: OwnedProviderProcessSpawnOptions,
@@ -15,6 +15,7 @@ import {
processOwnershipAdmissionRecovery,
} from "../process/ProcessOwnershipObservation.js";
import { ownedProcessPrerequisiteAvailability } from "./ownedProcessAvailability.js";
import { spawnFailure } from "../../tests/support/process/spawnFailure.js";
describe("Linux process table prerequisite discovery", () => {
it.each(["ENOENT", "EACCES", "EIO"])(
@@ -259,4 +260,20 @@ describe("Linux ownership prerequisite discovery", () => {
},
});
});
it("does not report macOS ownership helper capacity exhaustion as missing Command Line Tools", () => {
const emfile = spawnFailure("EMFILE", "/usr/bin/swiftc");
const darwin = new DarwinProcessOwnershipInspectionError(
"helper compiler capacity exhausted",
{ cause: emfile },
);
expect(ownedProcessPrerequisiteAvailability(darwin)).toBeUndefined();
expect(
ownedProcessPrerequisiteAvailability(
new OwnedCommandFailure("process", "preparation failed", null, null, {
preparationFailure: darwin,
}),
),
).toBeUndefined();
});
});
+5 -1
View File
@@ -2,7 +2,10 @@ import { AnalysisCapabilityUnavailableError } from "../domain/analysisErrorCore.
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
import { jsonValueSchema } from "../domain/jsonValue.js";
import { DarwinProcessOwnershipInspectionError } from "../process/DarwinProcessRunTokenReader.js";
import { OwnedCommandFailure } from "../process/OwnedCommand.js";
import {
OwnedCommandFailure,
preparationHostCapacityFailure,
} from "../process/OwnedCommand.js";
import {
DARWIN_PROCESS_OWNERSHIP_REMEDIATION,
LINUX_PROCESS_OWNERSHIP_REMEDIATION,
@@ -54,6 +57,7 @@ const darwinPrerequisiteRefusal = (
current = current.preparationFailure;
}
return current instanceof DarwinProcessOwnershipInspectionError &&
preparationHostCapacityFailure(current) === undefined &&
current.cleanupFailure === undefined &&
current.cleanupOwner === undefined &&
current.cancellation === undefined
@@ -530,6 +530,40 @@ it("preserves command deadline timeout when macOS helper preparation aborts with
}
});
it("keeps caller cancellation primary when macOS helper preparation is cancelled with a spawn EMFILE reason", async () => {
const { directory, path } = await createArtifact();
const command = resolve(directory, "configured-objdump");
const reason = spawnFailure("EMFILE", "/usr/bin/swiftc");
const darwinError = new DarwinProcessOwnershipInspectionError(
"Darwin process ownership helper preparation was cancelled",
{
cause: reason,
cancellation: { reason },
},
);
const service = new TestReverseEngineeringService({
environment: { REA_OBJDUMP_COMMAND: command },
runOwned: async () => {
throw darwinError;
},
});
try {
const result = await service.inspectWithObjdump({
path,
operation: "file_headers",
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error._tag).toBe("AnalysisCancelledError");
expect(result.error.cause).toBe(reason);
}
expect(service.observedLaunchFailures()).toEqual({});
} finally {
await service.close();
await rm(directory, { recursive: true, force: true });
}
});
it("uses platform-aware recovery without /proc when Darwin baseline capture rejects identity_snapshot_unavailable", async () => {
const { directory } = await createArtifact();
const command = resolve(directory, "configured-objdump");