From 9738583ed8f9546c3a2739081d685d0e00a347d2 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Mon, 16 Nov 2026 09:11:49 +0800 Subject: [PATCH] fix(native,hopper): keep the descriptor a failed process identity read left open (#11633) The LLDB tracer's target start-identity read and exit/signal identity checks, and the Hopper client's bridge start-identity read and exit-identity check, reduced an inspection-owner rejection to a message, so a descriptor whose close failed was dropped. Each reader now keeps the identity unavailable (or the exit unverified) and moves the rejection's descriptor owners into the session's inspection scope, which trace/close retries; the reported failure names the descriptor until it is released. The Hopper exit check no longer makes close() reject. Fixes #11602 --- src/hopper/HopperCleanup.test.ts | 6 + src/hopper/HopperCleanup.ts | 45 +++- src/hopper/HopperClient.ts | 10 + src/native/LldbCallTracer.ts | 81 +++++-- src/providers/OwnedCommandCleanup.ts | 22 +- .../hopper/hopperClient.cleanupRetry.test.ts | 4 + .../hopper/hopperClient.externalExit.test.ts | 2 + .../hopperClient.identityCleanup.test.ts | 150 +++++++++++++ .../native/lldbTargetIdentityCleanup.test.ts | 200 ++++++++++++++++++ 9 files changed, 499 insertions(+), 21 deletions(-) create mode 100644 tests/boundary/providers/hopper/hopperClient.identityCleanup.test.ts create mode 100644 tests/boundary/providers/native/lldbTargetIdentityCleanup.test.ts diff --git a/src/hopper/HopperCleanup.test.ts b/src/hopper/HopperCleanup.test.ts index c608061782..7715841627 100644 --- a/src/hopper/HopperCleanup.test.ts +++ b/src/hopper/HopperCleanup.test.ts @@ -19,6 +19,7 @@ import { confirmExternalApplicationExit, type HopperOwnedResources, } from "./HopperCleanup.js"; +import { ArtifactResourceScope } from "../resources/index.js"; const loggerHarness = () => { const logs: unknown[] = []; @@ -70,6 +71,7 @@ const externalApplicationResources = (): HopperOwnedResources => ({ processSupervisor: undefined, runtimeRoot: undefined, shutdownConfirmed: false, + inspections: new ArtifactResourceScope(), externalApplication: { pid: 123, identity: { state: "readable", identity: "captured-start" }, @@ -224,6 +226,7 @@ describe("Hopper shutdown rejection diagnostics", () => { processSupervisor: undefined, runtimeRoot: undefined, shutdownConfirmed: false, + inspections: new ArtifactResourceScope(), externalApplication: undefined, }, activeRequest: null, @@ -264,6 +267,7 @@ describe("Hopper shutdown rejection diagnostics", () => { processSupervisor: undefined, runtimeRoot: undefined, shutdownConfirmed: false, + inspections: new ArtifactResourceScope(), externalApplication: undefined, }, activeRequest: null, @@ -368,6 +372,7 @@ it.each([ processSupervisor: undefined, runtimeRoot: undefined, shutdownConfirmed: false, + inspections: new ArtifactResourceScope(), externalApplication: undefined, }, activeRequest: null, @@ -425,6 +430,7 @@ it("keeps a prepared backing image when native document closure cannot be confir runtimeRoot, processSupervisor: undefined, shutdownConfirmed: false, + inspections: new ArtifactResourceScope(), externalApplication: undefined, }, activeRequest: null, diff --git a/src/hopper/HopperCleanup.ts b/src/hopper/HopperCleanup.ts index b792f11dea..90335ce9e2 100644 --- a/src/hopper/HopperCleanup.ts +++ b/src/hopper/HopperCleanup.ts @@ -25,6 +25,8 @@ import { import type { HopperRequestActivity } from "./HopperRequestQueue.js"; import { parseHopperShutdownReply } from "./HopperReplyContracts.js"; import { errorMessage } from "../kernel/errorMessage.js"; +import { retainProcessInspectionOwners } from "../providers/OwnedCommandCleanup.js"; +import type { ArtifactResourceScope } from "../resources/index.js"; interface CleanupIssue { readonly resource: string; @@ -59,6 +61,8 @@ export interface HopperOwnedResources { readonly identity: ProcessIdentityObservation; } | undefined; + /** Descriptors a failed bridge identity read left open, released by cleanup. */ + readonly inspections: ArtifactResourceScope; } export interface HopperCleanupInput { @@ -146,9 +150,28 @@ export const cleanupHopperSession = async ( }); } } + await releaseInspections(input, state); return cleanupOutcome(input, state); }; +/** Retry descriptors whose identity read could not close them. */ +const releaseInspections = async ( + input: HopperCleanupInput, + state: CleanupState, +): Promise => { + try { + await input.resources.inspections.retryCleanup(); + } catch (cause: unknown) { + const resources = input.resources.inspections.cleanupResources; + for (const resource of resources.length === 0 + ? ["hopper-process-inspection"] + : resources) { + state.resources.add(resource); + state.issues.push({ resource, reason: errorMessage(cause) }); + } + } +}; + export const confirmExternalApplicationExit = async ( resources: HopperOwnedResources, socket: Socket | undefined, @@ -162,10 +185,24 @@ export const confirmExternalApplicationExit = async ( application?.identity.state !== "readable" ) return undefined; - const observed = await observeExit( - application.pid, - application.identity.identity, - ); + let observed: Awaited>; + try { + observed = await observeExit( + application.pid, + application.identity.identity, + ); + } catch (cause: unknown) { + // Exit stays unverified; descriptors the read left open stay owned. + if ( + retainProcessInspectionOwners( + cause, + resources.inspections, + "close_binary", + ).length === 0 + ) + throw cause; + return "unverified"; + } if (observed === "exited") { resources.shutdownConfirmed = true; releaseTransport(socket); diff --git a/src/hopper/HopperClient.ts b/src/hopper/HopperClient.ts index 203bfbb8eb..05fd05be63 100644 --- a/src/hopper/HopperClient.ts +++ b/src/hopper/HopperClient.ts @@ -75,6 +75,8 @@ import { ProviderProcessSupervisor, } from "../process/ProviderProcess.js"; import { observeProcessStartIdentity } from "../process/ProcessOwnershipObservation.js"; +import { retainProcessInspectionOwners } from "../providers/OwnedCommandCleanup.js"; +import { ArtifactResourceScope } from "../resources/index.js"; import type { BridgeLaunch, BridgeLauncher } from "./BridgeLauncher.js"; import { hopperOperationStage } from "./HopperOperationStage.js"; import type { HopperDiagnostic } from "./HopperDiagnostics.js"; @@ -198,6 +200,7 @@ export class HopperClient< runtimeRoot: undefined, shutdownConfirmed: false, externalApplication: undefined, + inspections: new ArtifactResourceScope(), }; #token: string | undefined; /** Reply ceiling admitted with the request on the wire; the default before any. */ @@ -702,6 +705,13 @@ export class HopperClient< try { identity = await observeProcessStartIdentity(parsed.value.processId); } catch (cause: unknown) { + // The identity stays unavailable; descriptors the read left open stay + // owned until session cleanup releases them. + retainProcessInspectionOwners( + cause, + this.#resources.inspections, + "open_binary", + ); identity = { state: "unavailable" as const, reason: diff --git a/src/native/LldbCallTracer.ts b/src/native/LldbCallTracer.ts index 2c48087f2d..361aa046eb 100644 --- a/src/native/LldbCallTracer.ts +++ b/src/native/LldbCallTracer.ts @@ -72,6 +72,10 @@ import { LldbReadbackFiles } from "./LldbReadbackFiles.js"; import { analysisErrorWithCleanupDependencies } from "../domain/analysisErrorCleanup.js"; import { resolvedAnalysisError } from "../domain/analysisErrorBase.js"; import { runOwned } from "../providers/runOwned.js"; +import { + processInspectionFailure, + retainProcessInspectionCleanup, +} from "../providers/OwnedCommandCleanup.js"; import { readLldbObservationJournal, readBoundedPrefix, @@ -279,6 +283,7 @@ export class LldbCallTracer implements NativeCallTracer { targetPid: undefined, processStopped: true, targetStopped: true, + identityInspections: [], }; let primaryFailure: AnalysisError | undefined; const finalized = await runOwned( @@ -370,15 +375,36 @@ export class LldbCallTracer implements NativeCallTracer { ), }, ); + // An identity inspection failure always fails the trace: the bridge + // refuses an unavailable identity, and an unverified target exit is a + // cleanup failure. Only failures carry these descriptors. if (finalized.kind === "completed") return ok(finalized.value); if ( finalized.kind !== "completed-cleanup-retained" && finalized.cleanup.kind === "released" ) return err( - resolvedAnalysisError(bindLldbCleanup(finalized.error, owner)), + this.#retainIdentityInspections( + owner, + resolvedAnalysisError(bindLldbCleanup(finalized.error, owner)), + ), ); - return err(finalized.error); + return err(this.#retainIdentityInspections(owner, finalized.error)); + } + + /** Move identity-read descriptors to the inspection scope that close retries. */ + #retainIdentityInspections( + owner: RetainedLldbResources, + failure: AnalysisError, + ): AnalysisError { + for (const cause of owner.identityInspections.splice(0)) + failure = retainProcessInspectionCleanup( + cause, + failure, + this.#inspectionResources, + OPERATION, + ); + return failure; } /** Retry retained LLDB cleanup without abandoning its process or runtime owner. */ @@ -410,7 +436,7 @@ export class LldbCallTracer implements NativeCallTracer { return call; } - async #retryPendingCleanup(): Promise { + async #retryPendingCleanup(): Promise { const owner = this.#pendingCleanup; if (owner === undefined) return undefined; const failures: unknown[] = []; @@ -452,10 +478,7 @@ export class LldbCallTracer implements NativeCallTracer { owner.targetStopped = true; else { try { - const targetTermination = await ensureTerminated( - owner.targetPid, - owner.targetIdentity, - ); + const targetTermination = await ensureTerminated(owner); if (targetTermination !== "terminated") failures.push( new Error(targetTerminationReason(targetTermination)), @@ -475,10 +498,13 @@ export class LldbCallTracer implements NativeCallTracer { failures.push(cause); } } - return lldbCleanupFailure( + return this.#retainIdentityInspections( owner, - failures.map(errorMessage).join("; "), - new AggregateError(failures, "LLDB cleanup remains incomplete"), + lldbCleanupFailure( + owner, + failures.map(errorMessage).join("; "), + new AggregateError(failures, "LLDB cleanup remains incomplete"), + ), ); } @@ -678,6 +704,8 @@ export class LldbCallTracer implements NativeCallTracer { } retained.supervisor = exited.cleanupOwner; retained.targetIdentity = exited.targetIdentity; + if (exited.targetIdentityInspection !== undefined) + retained.identityInspections.push(exited.targetIdentityInspection); retained.processStopped = exited.cleanupOwner === undefined && exited.cleanupFailure === undefined; retained.targetStopped = !exited.targetLaunched; @@ -695,7 +723,7 @@ export class LldbCallTracer implements NativeCallTracer { const targetTermination: TargetTermination = exited.targetLaunched && !bridgeProvesTargetGone(published, retained.targetPid) - ? await ensureTerminated(retained.targetPid, exited.targetIdentity) + ? await ensureTerminated(retained) : "terminated"; retained.targetStopped = targetTermination === "terminated"; if ( @@ -1289,6 +1317,11 @@ interface RetainedLldbResources { targetPid: number | undefined; processStopped: boolean; targetStopped: boolean; + /** + * Inspection-owner rejections of target identity reads. Their descriptors + * move to the tracer's inspection scope when the trace or retry reports. + */ + readonly identityInspections: unknown[]; } /** Preserve compiler facts and a foreign helper marker without inventing an LLDB exit. */ @@ -1397,6 +1430,8 @@ type LldbExit = readonly targetIdentity: string | undefined; /** Why the host acknowledged the launched target's identity as unavailable. */ readonly targetIdentityFailure?: string; + /** The inspection-owner rejection of that read, which may own descriptors. */ + readonly targetIdentityInspection?: unknown; readonly cleanupFailure: string | undefined; /** The work budget elapsed before this physical LLDB exit was selected. */ readonly overran?: true; @@ -1445,6 +1480,7 @@ const runLldb = async ( ); let targetIdentity: string | undefined; let targetIdentityFailure: string | undefined; + let targetIdentityInspection: unknown; let supervisor: ProviderProcessSupervisor | undefined; let retained = false; let processFailureBeforeInterruption: @@ -1477,6 +1513,9 @@ const runLldb = async ( targetLaunched: supervisor !== undefined, targetIdentity, ...(targetIdentityFailure === undefined ? {} : { targetIdentityFailure }), + ...(targetIdentityInspection === undefined + ? {} + : { targetIdentityInspection }), cleanupFailure: stopped?.status === "incomplete" ? stopped.reason : undefined, ...(overran === undefined ? {} : { overran }), @@ -1535,6 +1574,8 @@ const runLldb = async ( } catch (cause: unknown) { observation = undefined; targetIdentityFailure = errorMessage(cause); + if (processInspectionFailure(cause) !== undefined) + targetIdentityInspection = cause; } if (observation?.state === "readable") targetIdentity = observation.identity; @@ -1605,8 +1646,24 @@ type TargetTermination = | "identity-changed" | "signal-did-not-stop"; -/** Only signal a surviving target when its captured OS start identity still matches. */ +/** + * Only signal a surviving target when its captured OS start identity still + * matches. An identity check the inspection owner rejects leaves the target + * unverified and keeps the rejection on the owner for retention. + */ const ensureTerminated = async ( + owner: RetainedLldbResources, +): Promise => { + try { + return await terminateTarget(owner.targetPid, owner.targetIdentity); + } catch (cause: unknown) { + if (processInspectionFailure(cause) === undefined) throw cause; + owner.identityInspections.push(cause); + return "identity-unavailable"; + } +}; + +const terminateTarget = async ( pid: number | undefined, identity: string | undefined, ): Promise => { diff --git a/src/providers/OwnedCommandCleanup.ts b/src/providers/OwnedCommandCleanup.ts index 685ee39851..65e69ee380 100644 --- a/src/providers/OwnedCommandCleanup.ts +++ b/src/providers/OwnedCommandCleanup.ts @@ -189,14 +189,17 @@ export const processInspectionCancellationError = ( return undefined; }; -/** Bind explicit inspection owners to an artifact scope while preserving the caller's primary. */ -export const retainProcessInspectionCleanup = ( +/** + * Retain explicit inspection owners in an artifact scope, for a reader that + * reports the inspection as unavailable instead of failing. Returns the + * cleanup failures that stay open until the scope releases them. + */ +export const retainProcessInspectionOwners = ( cause: unknown, - primary: AnalysisError, resources: ArtifactResourceScope, operation: string, -): AnalysisError => { - const failures = retainOwnedCommandCleanup( +): readonly AnalysisError[] => + retainOwnedCommandCleanup( cause, { retain: (owner, resource) => @@ -210,6 +213,15 @@ export const retainProcessInspectionCleanup = ( "process-ownership", operation, ); + +/** Bind explicit inspection owners to an artifact scope while preserving the caller's primary. */ +export const retainProcessInspectionCleanup = ( + cause: unknown, + primary: AnalysisError, + resources: ArtifactResourceScope, + operation: string, +): AnalysisError => { + const failures = retainProcessInspectionOwners(cause, resources, operation); for (const failure of failures) primary = analysisErrorWithCleanupDependencies(primary, failure, operation); return bindProcessInspectionCancellations(cause, primary, operation); diff --git a/tests/boundary/providers/hopper/hopperClient.cleanupRetry.test.ts b/tests/boundary/providers/hopper/hopperClient.cleanupRetry.test.ts index b3030c775c..ae0dc3df5b 100644 --- a/tests/boundary/providers/hopper/hopperClient.cleanupRetry.test.ts +++ b/tests/boundary/providers/hopper/hopperClient.cleanupRetry.test.ts @@ -26,6 +26,7 @@ import { import { silentLogger } from "../../../../src/logger.js"; import { PrivateRuntimeRoot } from "../../../../src/process/PrivateRuntimeRoot.js"; import { ProviderProcessSupervisor } from "../../../../src/process/ProviderProcess.js"; +import { ArtifactResourceScope } from "../../../../src/resources/index.js"; class RetryCleanupLauncher implements BridgeLauncher { readonly sessions: BridgeSession[] = []; @@ -191,6 +192,7 @@ it.each([false, true])( processSupervisor: supervisor, runtimeRoot, shutdownConfirmed: false, + inspections: new ArtifactResourceScope(), externalApplication: undefined, }; const socket = new Socket(); @@ -480,6 +482,7 @@ it("preserves confirmed document shutdown while retrying owned process cleanup", processSupervisor: supervisor, runtimeRoot, shutdownConfirmed: false, + inspections: new ArtifactResourceScope(), externalApplication: undefined, } satisfies HopperOwnedResources, activeRequest: null, @@ -545,6 +548,7 @@ it.each([ processSupervisor: supervisor, runtimeRoot, shutdownConfirmed: false, + inspections: new ArtifactResourceScope(), externalApplication: undefined, }; const result = await cleanupHopperSession({ diff --git a/tests/boundary/providers/hopper/hopperClient.externalExit.test.ts b/tests/boundary/providers/hopper/hopperClient.externalExit.test.ts index 0998fc9d10..54966f73b0 100644 --- a/tests/boundary/providers/hopper/hopperClient.externalExit.test.ts +++ b/tests/boundary/providers/hopper/hopperClient.externalExit.test.ts @@ -30,6 +30,7 @@ import { transportReplyContracts, type TransportHopperClient, } from "./hopperClient.fixture.js"; +import { ArtifactResourceScope } from "../../../../src/resources/index.js"; const identities = { hopper: { id: "hopper", name: "Hopper fixture", version: null }, @@ -235,6 +236,7 @@ it.skipIf(process.platform === "win32")( processSupervisor: undefined, runtimeRoot, shutdownConfirmed: false, + inspections: new ArtifactResourceScope(), externalApplication: { pid: started.value.processId, identity, diff --git a/tests/boundary/providers/hopper/hopperClient.identityCleanup.test.ts b/tests/boundary/providers/hopper/hopperClient.identityCleanup.test.ts new file mode 100644 index 0000000000..41ade3f2ad --- /dev/null +++ b/tests/boundary/providers/hopper/hopperClient.identityCleanup.test.ts @@ -0,0 +1,150 @@ +import type { ChildProcess } from "node:child_process"; +import { once } from "node:events"; +import { rm } from "node:fs/promises"; + +import { expect, it, onTestFinished, vi } from "vitest"; + +import { ok } from "../../../../src/domain/result.js"; +import type { BridgeSession } from "../../../../src/hopper/BridgeLauncher.js"; +import { HopperClient } from "../../../../src/hopper/HopperClient.js"; +import { failedInspectionDescriptor } from "../../../fixtures/processInspectionCleanup.js"; +import { + HopperFixtureLauncher, + transportReplyContracts, +} from "./hopperClient.fixture.js"; + +/** Each identity read rejects with this cause while it is set. */ +const rejections = vi.hoisted(() => ({ + startIdentity: undefined as unknown, + exitIdentity: undefined as unknown, +})); + +vi.mock( + "../../../../src/process/ProcessOwnershipObservation.js", + async (importOriginal) => { + const original = + await importOriginal< + typeof import("../../../../src/process/ProcessOwnershipObservation.js") + >(); + return { + ...original, + observeProcessStartIdentity: async ( + ...arguments_: Parameters + ) => { + if (rejections.startIdentity !== undefined) + throw rejections.startIdentity; + return original.observeProcessStartIdentity(...arguments_); + }, + observeProcessStartIdentityExit: async ( + ...arguments_: Parameters< + typeof original.observeProcessStartIdentityExit + > + ) => { + if (rejections.exitIdentity !== undefined) + throw rejections.exitIdentity; + return original.observeProcessStartIdentityExit(...arguments_); + }, + }; + }, +); + +const stopFixture = async (child: ChildProcess): Promise => { + if (child.exitCode !== null || child.signalCode !== null) return; + const exited = once(child, "exit"); + child.kill("SIGKILL"); + await exited; +}; + +/** A client whose fixture bridge runs as an authenticated external Hopper. */ +const externalClient = () => { + const fixture = new HopperFixtureLauncher(); + const client = new HopperClient({ + replyContracts: transportReplyContracts, + startupTimeoutMs: 2_000, + launcher: { + async launch(session: BridgeSession) { + const launched = await fixture.launch(session); + if (!launched.ok) return launched; + return ok({ + process: launched.value.process, + ownsProcessLifetime: false as const, + providerLifetime: "external-application" as const, + shutdownMode: "bridge-request" as const, + }); + }, + }, + }); + onTestFinished(async () => { + rejections.startIdentity = undefined; + rejections.exitIdentity = undefined; + await client.close(); + for (const child of fixture.processes) await stopFixture(child); + await Promise.all( + fixture.directories.map((directory) => + rm(directory, { recursive: true, force: true }), + ), + ); + }); + return client; +}; + +/** Close reports the descriptor until it closes, then the retry releases it. */ +const expectRetainedUntilReleased = async ( + client: ReturnType, + descriptor: Awaited>, +): Promise => { + const refused = await client.close(); + if (refused.ok) throw new Error("Expected the retained descriptor"); + expect( + refused.error.cleanupResources, + "close names the descriptor it could not release", + ).toContain(descriptor.path); + const closesBefore = descriptor.closes(); + + descriptor.allow(); + const released = await client.close(); + expect( + descriptor.closes(), + "the retry closes the same descriptor", + ).toBeGreaterThan(closesBefore); + expect(released.ok ? [] : released.error.cleanupResources).not.toContain( + descriptor.path, + ); +}; + +it.skipIf(process.platform === "win32")( + "retains the descriptor a failed bridge start-identity read left open until close releases it", + async () => { + const client = externalClient(); + const descriptor = await failedInspectionDescriptor(); + onTestFinished(() => descriptor.allow()); + + rejections.startIdentity = descriptor.failure; + const started = await client.start(); + rejections.startIdentity = undefined; + if (!started.ok) throw started.error; + + await expectRetainedUntilReleased(client, descriptor); + }, +); + +it.skipIf(process.platform === "win32")( + "retains the descriptor a failed bridge exit-identity check left open during close", + async () => { + const client = externalClient(); + const descriptor = await failedInspectionDescriptor(); + onTestFinished(() => descriptor.allow()); + const started = await client.start(); + if (!started.ok) throw started.error; + + rejections.exitIdentity = descriptor.failure; + const refused = client.close(); + await expect( + refused, + "close reports instead of rejecting", + ).resolves.toEqual(expect.objectContaining({ ok: false })); + rejections.exitIdentity = undefined; + + await expectRetainedUntilReleased(client, descriptor); + }, +); diff --git a/tests/boundary/providers/native/lldbTargetIdentityCleanup.test.ts b/tests/boundary/providers/native/lldbTargetIdentityCleanup.test.ts new file mode 100644 index 0000000000..eaf02df14d --- /dev/null +++ b/tests/boundary/providers/native/lldbTargetIdentityCleanup.test.ts @@ -0,0 +1,200 @@ +import { spawn } from "node:child_process"; +import { once } from "node:events"; +import { writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import { expect, it, onTestFinished, vi } from "vitest"; + +import { ok } from "../../../../src/domain/result.js"; +import type { NativeCallTracer } from "../../../../src/native/LldbCallTracer.js"; +import { LldbCallTracer } from "../../../../src/native/LldbCallTracer.js"; +import { nativeResolvedToolFixture } from "../../../fixtures/nativeCommands.js"; +import { failedInspectionDescriptor } from "../../../fixtures/processInspectionCleanup.js"; +import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js"; + +/** Each identity read rejects with this cause while it is set. */ +const rejections = vi.hoisted(() => ({ + startIdentity: undefined as unknown, + exitIdentity: undefined as unknown, +})); + +vi.mock( + "../../../../src/process/ProcessOwnershipObservation.js", + async (importOriginal) => { + const original = + await importOriginal< + typeof import("../../../../src/process/ProcessOwnershipObservation.js") + >(); + return { + ...original, + observeProcessStartIdentity: async ( + ...arguments_: Parameters + ) => { + if (rejections.startIdentity !== undefined) + throw rejections.startIdentity; + return original.observeProcessStartIdentity(...arguments_); + }, + observeProcessStartIdentityExit: async ( + ...arguments_: Parameters< + typeof original.observeProcessStartIdentityExit + > + ) => { + if (rejections.exitIdentity !== undefined) + throw rejections.exitIdentity; + return original.observeProcessStartIdentityExit(...arguments_); + }, + }; + }, +); + +const request: Parameters[0] = { + executable: "/fixture/target", + architecture: "arm64", + expectedSha256: "0".repeat(64), + input: { + breakpoints: [{ kind: "function", name: "main" }], + arguments: [], + environment: {}, + duration_ms: 5_000, + max_events: 1, + argument_registers: 0, + backtrace_frames: 0, + }, +}; + +/** + * A fake LLDB that publishes `FIXTURE_TARGET_PID` (or its own PID) as the + * target, waits for the host's identity acknowledgement, and then publishes + * `FIXTURE_RESULT` with empty capture files. + */ +const fakeLldb = async (root: string): Promise => { + const path = join(root, "fake-lldb"); + await writeFile( + path, + `#!${process.execPath} +const fs = require("node:fs"); +const command = process.argv.at(-1); +if (!command?.startsWith("rea_trace ")) process.exit(91); +const config = JSON.parse(fs.readFileSync(command.slice("rea_trace ".length), "utf8")); +for (const file of [config.observation_path, config.stdout_capture_path, config.stderr_capture_path]) + fs.writeFileSync(file, ""); +fs.writeFileSync(config.pid_path, process.env.FIXTURE_TARGET_PID ?? String(process.pid)); +const wait = () => { + if (!fs.existsSync(config.identity_ack_path)) return setTimeout(wait, 10); + fs.writeFileSync(config.result_path, process.env.FIXTURE_RESULT); +}; +wait(); +`, + { mode: 0o700 }, + ); + return path; +}; + +const tracerFor = ( + executable: string, + environment: Readonly>, +): LldbCallTracer => { + const tracer = new LldbCallTracer( + { ...process.env, ...environment }, + undefined, + async () => ok(nativeResolvedToolFixture(executable, "c".repeat(64))), + ); + return tracer; +}; + +it.skipIf(process.platform === "win32")( + "retains the descriptor a failed target start-identity read left open until close releases it", + async () => { + const root = await createTestTempDirectory("rea-lldb-identity-fd-"); + const descriptor = await failedInspectionDescriptor(root); + const tracer = tracerFor(await fakeLldb(root), { + FIXTURE_RESULT: JSON.stringify({ + status: "target-identity-unavailable", + error: "host could not establish the launched process identity", + target_process_created: true, + target_process_exit_observed: true, + }), + }); + rejections.startIdentity = descriptor.failure; + onTestFinished(() => { + rejections.startIdentity = undefined; + }); + + const result = await tracer.trace(request); + rejections.startIdentity = undefined; + + if (result.ok) throw new Error("Expected the identity-unavailable failure"); + expect( + result.error.cleanupIncomplete, + "the open descriptor is reported as cleanup", + ).toBe(true); + expect(result.error.cleanupResources).toContain(descriptor.path); + const refused = await tracer.close(); + expect(refused.ok, "close keeps the descriptor while it fails").toBe(false); + expect(descriptor.closes()).toBeGreaterThan(1); + + descriptor.allow(); + expect(await tracer.close()).toEqual(ok(null)); + expect( + result.error.cleanupIncomplete, + "the release is visible on the reported failure", + ).toBe(false); + }, +); + +it.skipIf(process.platform === "win32")( + "retains the descriptor a failed target exit-identity check left open while terminating the target", + async () => { + const root = await createTestTempDirectory("rea-lldb-exit-fd-"); + const target = spawn( + process.execPath, + ["-e", "setInterval(() => {}, 1000)"], + { + stdio: "ignore", + }, + ); + await once(target, "spawn"); + onTestFinished(async () => { + if (target.exitCode === null && target.signalCode === null) { + target.kill("SIGKILL"); + await once(target, "exit"); + } + }); + const descriptor = await failedInspectionDescriptor(root); + const tracer = tracerFor(await fakeLldb(root), { + FIXTURE_TARGET_PID: String(target.pid), + FIXTURE_RESULT: JSON.stringify({ + status: "target-error", + error: "fixture target failure", + target_process_created: true, + }), + }); + rejections.exitIdentity = descriptor.failure; + onTestFinished(() => { + rejections.exitIdentity = undefined; + }); + + const result = await tracer.trace(request); + rejections.exitIdentity = undefined; + + if (result.ok) throw new Error("Expected the target failure"); + expect(result.error.cleanupIncomplete).toBe(true); + expect( + result.error.cleanupResources, + "the descriptor is reported with the surviving target", + ).toEqual( + expect.arrayContaining([ + descriptor.path, + `native-target:${String(target.pid)}`, + ]), + ); + + target.kill("SIGKILL"); + await once(target, "exit"); + expect((await tracer.close()).ok, "the descriptor still refuses").toBe( + false, + ); + descriptor.allow(); + expect(await tracer.close()).toEqual(ok(null)); + expect(result.error.cleanupIncomplete).toBe(false); + }, +);