mirror of
https://github.com/morluto/rea.git
synced 2026-11-29 01:46:39 +00:00
* fix(rizin): terminate the owned debugger when cleanup cannot resynchronize
A Rizin debug command whose deadline expires while the target keeps running
leaves the debugger blocked with unread cleanup commands. Cleanup could not
observe its resynchronization marker, detach was never verified, and the
retained process outlived the MCP server with its target (issue #8779).
Detach results now carry an explicit `unavailable` declaration. Rizin returns
it when a bounded resynchronization probe (500 ms, inside the shared 1000 ms
detach budget) observes no marker. `stopOwnedDebuggerProcess` then terminates
the directly owned process and reports `stopped_inferior_unknown`, leaving the
inferior's disposition unknown instead of inferring it from a PID or path.
An observed marker followed by a refused detach still retains the cleanup
owner for retry, and a session whose output streams have not settled remains
retained. Rizin never signals the inferior, so its target may continue
running; timeout, cancellation and protocol-failure evidence name the
unverified detach and the unknown target state, as do the close contract,
the Rizin guide, and the runtime-observation skill.
Validation (rebased onto upstream/main at cdc4541ef):
- npm run test:focused -- 21 Rizin/GDB boundary and MCP files: 463 passed,
1 skipped (real Rizin 0.7.4 lane; the host runs 0.9.1).
- npm run test:coverage:reverse-tools: passed.
- npm run check:fast, format:check, metadata:check, docs:check,
verify:architecture-guards, knip:production and lint:typed: passed.
The repository-level knip task reports one pre-existing unused type
(src/domain/adb/adbDeviceAnalysis.ts) that reproduces on the clean base.
- Real Rizin 0.9.1 on Linux x64: on the base, a `dc` deadline left the
retained rizin process alive and closeAll raised retainedProcessIds; with
this change the command returns partial output with the new limitation,
the owned debugger is terminated, the target keeps running, and close
returns target_state unknown.
* fix(rizin): address Greptile P1/P2 and Codex review findings
* fix(rizin): address Greptile P1/P2 and Codex review findings
* fix(rizin): address Greptile P1/P2 and Codex review findings
* fix(rizin): keep the unknown target state when cancelled startup cannot detach
A startup that failed its protocol and was cancelled reported only the
cancellation. When cleanup then could not resynchronize and terminated
Rizin without a verified detach, the caller lost the only record that the
target may still be running, because the session is removed after the stop.
Cancellation stays the reported reason and the unverified detach is
retained as a secondary failure, as the protocol-failure branch already
states it. Also route the cleanup probe reason through errorMessage.
* test(rizin): use lifecycle assertions and typed failures
---------
Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
1612 lines
51 KiB
TypeScript
1612 lines
51 KiB
TypeScript
import { spawn } from "node:child_process";
|
|
import { randomUUID } from "node:crypto";
|
|
import { once } from "node:events";
|
|
import { writeFile } from "node:fs/promises";
|
|
import { connect } from "node:net";
|
|
import { join } from "node:path";
|
|
import { spawn as spawnPty } from "@lydell/node-pty";
|
|
|
|
import { expect, it, onTestFinished, vi } from "vitest";
|
|
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
import { waitForExit } from "../../support/process/processFixture.js";
|
|
import { testWaitDeadline, waitFor } from "../../support/testWait.js";
|
|
import {
|
|
OwnedDebuggerCleanupError,
|
|
retainedDebuggerProcessIds,
|
|
retainedDebuggerRemediation,
|
|
} from "../../../src/process/ownedDebuggerCleanupError.js";
|
|
import {
|
|
closeOwnedDebuggerSessions,
|
|
type OwnedDebuggerStopResult,
|
|
stopOwnedDebuggerProcess,
|
|
verifyOwnedInferiorsEnded,
|
|
} from "../../../src/process/ownedDebuggerSession.js";
|
|
import { ProviderProcessSupervisor } from "../../../src/process/ProviderProcess.js";
|
|
import { cleanupOwnedProcessGroup } from "../../../src/process/ProcessOwnership.js";
|
|
|
|
const triggerDescendantOutput = (port: number): void => {
|
|
const socket = connect(port, "127.0.0.1");
|
|
socket.on("error", () => {});
|
|
socket.on("connect", () => socket.end());
|
|
};
|
|
|
|
const createFakeSupervisor = () => ({
|
|
waitForExit: async () => true,
|
|
waitForOutputClose: async () => true,
|
|
limitAdditionalOutputBytes: vi.fn(),
|
|
dispose: vi.fn(),
|
|
unrefOutputStreams: vi.fn(),
|
|
ref: vi.fn(),
|
|
unref: vi.fn(),
|
|
});
|
|
|
|
/** A session reduced to what closing all sessions reads from it. */
|
|
const closableSession = (id: string, pid?: number) => ({
|
|
id,
|
|
launched: { process: { pid } },
|
|
supervisor: { ref: vi.fn(), unref: vi.fn() },
|
|
});
|
|
|
|
const startStopProbe = (source: string, pipeInput = false) => {
|
|
const runId = randomUUID();
|
|
const child = spawn(process.execPath, ["--input-type=module", "-e", source], {
|
|
detached: process.platform !== "win32",
|
|
env: { ...process.env, REA_PROCESS_RUN_ID: runId },
|
|
stdio: [pipeInput ? "pipe" : "ignore", "pipe", "pipe"],
|
|
});
|
|
const leaderPid = child.pid;
|
|
if (leaderPid === undefined) throw new Error("Fixture probe did not spawn");
|
|
onTestFinished(async () => {
|
|
const cleanup = await cleanupOwnedProcessGroup({
|
|
runId,
|
|
leaderPid,
|
|
processGroupId: leaderPid,
|
|
});
|
|
expect(cleanup.cleaned, JSON.stringify(cleanup)).toBe(true);
|
|
});
|
|
let output = "";
|
|
const collect = (chunk: Buffer | string): void => {
|
|
output += chunk.toString();
|
|
};
|
|
child.stdout?.on("data", collect);
|
|
child.stderr?.on("data", collect);
|
|
return {
|
|
child,
|
|
get output() {
|
|
return output;
|
|
},
|
|
};
|
|
};
|
|
|
|
const startDebuggerWithInferior = (inferiorSource: string) => {
|
|
const debuggerSource = [
|
|
'const { spawn } = require("node:child_process");',
|
|
`spawn(process.execPath, ["-e", ${JSON.stringify(inferiorSource)}], { stdio: "inherit" });`,
|
|
"process.exit(0);",
|
|
].join("\n");
|
|
const child = spawn(process.execPath, ["-e", debuggerSource], {
|
|
stdio: ["ignore", "pipe", "pipe"],
|
|
});
|
|
const supervisor = new ProviderProcessSupervisor({
|
|
process: child,
|
|
ownsProcessLifetime: true,
|
|
});
|
|
let readyOutput = "";
|
|
const inferiorPort = new Promise<number>((resolvePort) => {
|
|
child.stdout?.on("data", (chunk: Buffer | string) => {
|
|
readyOutput += chunk.toString();
|
|
const match = /^ready:(\d+)$/mu.exec(readyOutput);
|
|
if (match !== null) resolvePort(Number(match[1]));
|
|
});
|
|
});
|
|
return { child, supervisor, inferiorPort };
|
|
};
|
|
|
|
it("preserves the session receiver when invoking the provider detach callback", async () => {
|
|
const session = {
|
|
id: "receiver-bound-detach",
|
|
exited: false,
|
|
transport: { verified: true },
|
|
launched: { process: { kill: vi.fn(() => true) } },
|
|
async detachInferior(this: { transport: { verified: boolean } }) {
|
|
expect(this.transport.verified).toBe(true);
|
|
return { status: "no-inferior" } as const;
|
|
},
|
|
supervisor: createFakeSupervisor(),
|
|
};
|
|
|
|
await expect(
|
|
stopOwnedDebuggerProcess(session, "GDB", { platform: "linux" }),
|
|
).resolves.toEqual({ status: "stopped" });
|
|
expect(session.supervisor.limitAdditionalOutputBytes).toHaveBeenCalledWith(
|
|
64 * 1024,
|
|
);
|
|
});
|
|
|
|
it("clears the detach timeout when provider detachment settles immediately", async () => {
|
|
vi.useFakeTimers();
|
|
try {
|
|
const session = {
|
|
id: "immediate-detach",
|
|
exited: false,
|
|
launched: { process: { kill: vi.fn(() => true) } },
|
|
detachInferior: async () => ({ status: "no-inferior" }) as const,
|
|
supervisor: createFakeSupervisor(),
|
|
};
|
|
|
|
await expect(
|
|
stopOwnedDebuggerProcess(session, "GDB", { platform: "linux" }),
|
|
).resolves.toEqual({ status: "stopped" });
|
|
expect(vi.getTimerCount()).toBe(0);
|
|
} finally {
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
|
|
it("retries provider detachment after a rejected attempt", async () => {
|
|
const detachInferior = vi
|
|
.fn<() => Promise<{ readonly status: "no-inferior" }>>()
|
|
.mockRejectedValueOnce(new Error("detach transport failed"))
|
|
.mockResolvedValueOnce({ status: "no-inferior" });
|
|
const session = {
|
|
id: "retry-rejected-detach",
|
|
exited: false,
|
|
launched: { process: { kill: vi.fn(() => true) } },
|
|
detachInferior,
|
|
supervisor: createFakeSupervisor(),
|
|
};
|
|
|
|
await expect(
|
|
stopOwnedDebuggerProcess(session, "GDB", { platform: "linux" }),
|
|
).resolves.toMatchObject({ status: "incomplete" });
|
|
await expect(
|
|
stopOwnedDebuggerProcess(session, "GDB", { platform: "linux" }),
|
|
).resolves.toEqual({ status: "stopped" });
|
|
expect(detachInferior).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it("retains ownership after stop until inherited output writers close", async () => {
|
|
let exited = false;
|
|
let outputClosed = false;
|
|
const supervisor = {
|
|
...createFakeSupervisor(),
|
|
waitForOutputClose: async () => outputClosed,
|
|
};
|
|
const session = {
|
|
id: "stopped-with-open-output",
|
|
get exited() {
|
|
return exited;
|
|
},
|
|
launched: {
|
|
process: {
|
|
kill: vi.fn(() => {
|
|
exited = true;
|
|
return true;
|
|
}),
|
|
},
|
|
},
|
|
detachInferior: async () => ({ status: "no-inferior" }) as const,
|
|
supervisor,
|
|
};
|
|
|
|
await expect(
|
|
stopOwnedDebuggerProcess(session, "GDB", { platform: "linux" }),
|
|
).resolves.toMatchObject({ status: "incomplete", reason: /output streams/i });
|
|
expect(supervisor.unrefOutputStreams).not.toHaveBeenCalled();
|
|
expect(supervisor.dispose).not.toHaveBeenCalled();
|
|
|
|
outputClosed = true;
|
|
await expect(
|
|
stopOwnedDebuggerProcess(session, "GDB", { platform: "linux" }),
|
|
).resolves.toEqual({ status: "already_exited" });
|
|
expect(supervisor.unrefOutputStreams).toHaveBeenCalledOnce();
|
|
expect(supervisor.dispose).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("retains process supervision after an incomplete stop and disposes after a retry observes exit", async () => {
|
|
let waits = 0;
|
|
let disposals = 0;
|
|
let outputUnrefs = 0;
|
|
const signals: NodeJS.Signals[] = [];
|
|
const detachInferior = vi
|
|
.fn<
|
|
() => Promise<{
|
|
readonly status: "detached";
|
|
readonly outputSink: "durable";
|
|
}>
|
|
>()
|
|
.mockResolvedValueOnce({ status: "detached", outputSink: "durable" })
|
|
.mockRejectedValue(
|
|
new Error("debugger transport closed during termination"),
|
|
);
|
|
const session = {
|
|
id: "debugger-session",
|
|
exited: false,
|
|
launched: {
|
|
process: {
|
|
kill: (signal: NodeJS.Signals): boolean => {
|
|
signals.push(signal);
|
|
return true;
|
|
},
|
|
},
|
|
},
|
|
detachInferior,
|
|
supervisor: {
|
|
waitForExit: async (): Promise<boolean> => {
|
|
waits += 1;
|
|
return waits === 3;
|
|
},
|
|
limitAdditionalOutputBytes: vi.fn(),
|
|
waitForOutputClose: async (): Promise<boolean> => true,
|
|
dispose: (): void => {
|
|
disposals += 1;
|
|
},
|
|
unrefOutputStreams: (): void => {
|
|
outputUnrefs += 1;
|
|
},
|
|
},
|
|
};
|
|
|
|
const first = await stopOwnedDebuggerProcess(session, "GDB", {
|
|
platform: "linux",
|
|
});
|
|
expect(first).toEqual({
|
|
status: "incomplete",
|
|
code: "process-did-not-exit",
|
|
reason: "Owned GDB process did not exit after direct termination.",
|
|
});
|
|
expect(disposals).toBe(0);
|
|
expect(outputUnrefs).toBe(0);
|
|
expect(signals).toEqual(["SIGTERM", "SIGKILL"]);
|
|
|
|
const retry = await stopOwnedDebuggerProcess(session, "GDB", {
|
|
platform: "linux",
|
|
});
|
|
expect(retry).toEqual({ status: "stopped" });
|
|
expect(disposals).toBe(1);
|
|
expect(waits).toBe(3);
|
|
expect(detachInferior).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("refuses Windows job-backed debugger termination before signaling", async () => {
|
|
const signals: NodeJS.Signals[] = [];
|
|
let outputUnrefs = 0;
|
|
const session = {
|
|
id: "windows-debugger",
|
|
exited: false,
|
|
launched: {
|
|
process: {
|
|
kill: (signal: NodeJS.Signals): boolean => {
|
|
signals.push(signal);
|
|
return true;
|
|
},
|
|
},
|
|
},
|
|
supervisor: {
|
|
waitForExit: async (): Promise<boolean> => false,
|
|
limitAdditionalOutputBytes: vi.fn(),
|
|
waitForOutputClose: async (): Promise<boolean> => false,
|
|
dispose: (): void => {},
|
|
unrefOutputStreams: (): void => {
|
|
outputUnrefs += 1;
|
|
},
|
|
},
|
|
};
|
|
|
|
await expect(
|
|
stopOwnedDebuggerProcess(session, "GDB", { platform: "win32" }),
|
|
).resolves.toEqual({
|
|
status: "incomplete",
|
|
code: "windows-stop-unsafe",
|
|
reason: expect.stringMatching(
|
|
/Windows.*job termination.*inferior processes/i,
|
|
),
|
|
});
|
|
expect(signals).toEqual([]);
|
|
expect(outputUnrefs).toBe(0);
|
|
});
|
|
|
|
it("retains an already-exited Windows session while its job has descendants", async () => {
|
|
let outputUnrefs = 0;
|
|
let disposals = 0;
|
|
const result = await stopOwnedDebuggerProcess(
|
|
{
|
|
id: "windows-already-exited",
|
|
exited: true,
|
|
launched: { process: { kill: () => true } },
|
|
supervisor: {
|
|
waitForExit: async () => false,
|
|
limitAdditionalOutputBytes: vi.fn(),
|
|
waitForOutputClose: async () => false,
|
|
dispose: () => {
|
|
disposals += 1;
|
|
},
|
|
unrefOutputStreams: () => {
|
|
outputUnrefs += 1;
|
|
},
|
|
},
|
|
},
|
|
"GDB",
|
|
{ platform: "win32" },
|
|
);
|
|
|
|
expect(result).toEqual({
|
|
status: "incomplete",
|
|
code: "output-unsettled",
|
|
reason: expect.stringMatching(
|
|
/leader exited.*Windows Job Object.*active descendants.*settlement/i,
|
|
),
|
|
});
|
|
expect(disposals).toBe(0);
|
|
expect(outputUnrefs).toBe(0);
|
|
});
|
|
|
|
it("retains an exited POSIX session while descendants still hold its output streams", async () => {
|
|
let outputUnrefs = 0;
|
|
let disposals = 0;
|
|
const result = await stopOwnedDebuggerProcess(
|
|
{
|
|
id: "posix-already-exited-with-descendant",
|
|
exited: true,
|
|
launched: { process: { kill: () => true } },
|
|
supervisor: {
|
|
waitForExit: async () => true,
|
|
limitAdditionalOutputBytes: vi.fn(),
|
|
waitForOutputClose: async () => false,
|
|
dispose: () => {
|
|
disposals += 1;
|
|
},
|
|
unrefOutputStreams: () => {
|
|
outputUnrefs += 1;
|
|
},
|
|
},
|
|
},
|
|
"GDB",
|
|
{ platform: "linux" },
|
|
);
|
|
|
|
expect(result).toEqual({
|
|
status: "incomplete",
|
|
code: "output-unsettled",
|
|
reason: expect.stringMatching(
|
|
/leader exited.*output streams.*open.*settlement/i,
|
|
),
|
|
});
|
|
expect(disposals).toBe(0);
|
|
expect(outputUnrefs).toBe(0);
|
|
});
|
|
|
|
it.each([
|
|
[
|
|
"POSIX Error",
|
|
"linux",
|
|
new Error("pipe state unknown"),
|
|
"Owned GDB inherited output stream settlement could not be verified: pipe state unknown",
|
|
],
|
|
[
|
|
"POSIX non-Error",
|
|
"linux",
|
|
"pipe state unknown",
|
|
"Owned GDB inherited output stream settlement could not be verified: pipe state unknown",
|
|
],
|
|
[
|
|
"Windows Error",
|
|
"win32",
|
|
new Error("job state unknown"),
|
|
"Owned GDB Windows Job Object settlement could not be verified: job state unknown",
|
|
],
|
|
[
|
|
"Windows non-Error",
|
|
"win32",
|
|
"job state unknown",
|
|
"Owned GDB Windows Job Object settlement could not be verified: job state unknown",
|
|
],
|
|
] as const)(
|
|
"retains an exited debugger when output settlement throws (%s)",
|
|
async (_label, platform, cause, expectedReason) => {
|
|
let disposals = 0;
|
|
const result = await stopOwnedDebuggerProcess(
|
|
{
|
|
id: "output-settlement-error",
|
|
exited: true,
|
|
launched: { process: { kill: () => true } },
|
|
supervisor: {
|
|
waitForExit: async () => true,
|
|
waitForOutputClose: async () => Promise.reject(cause),
|
|
limitAdditionalOutputBytes: () => {},
|
|
dispose: () => {
|
|
disposals += 1;
|
|
},
|
|
unrefOutputStreams: () => {},
|
|
},
|
|
},
|
|
"GDB",
|
|
{ platform },
|
|
);
|
|
|
|
expect(result).toEqual({
|
|
status: "incomplete",
|
|
code: "output-unsettled",
|
|
reason: expectedReason,
|
|
});
|
|
expect(disposals).toBe(0);
|
|
},
|
|
);
|
|
|
|
it("does not signal an active debugger when its provider cannot detach the inferior", async () => {
|
|
const signals: NodeJS.Signals[] = [];
|
|
const result = await stopOwnedDebuggerProcess(
|
|
{
|
|
id: "missing-detach-capability",
|
|
exited: false,
|
|
launched: { process: { kill: (signal) => (signals.push(signal), true) } },
|
|
supervisor: {
|
|
waitForExit: async () => false,
|
|
waitForOutputClose: async () => false,
|
|
limitAdditionalOutputBytes: () => {},
|
|
dispose: () => {},
|
|
unrefOutputStreams: () => {},
|
|
},
|
|
},
|
|
"GDB",
|
|
{ platform: "linux" },
|
|
);
|
|
|
|
expect(result).toEqual({
|
|
status: "incomplete",
|
|
code: "detach-unsupported",
|
|
reason: expect.stringMatching(
|
|
/inferior detach.*cannot be verified.*no detach capability/i,
|
|
),
|
|
});
|
|
expect(signals).toEqual([]);
|
|
});
|
|
|
|
it("preserves a non-Error provider detach rejection and does not signal the debugger", async () => {
|
|
const signals: NodeJS.Signals[] = [];
|
|
const result = await stopOwnedDebuggerProcess(
|
|
{
|
|
id: "non-error-detach-failure",
|
|
exited: false,
|
|
launched: { process: { kill: (signal) => (signals.push(signal), true) } },
|
|
detachInferior: async () => Promise.reject("detach state unknown"),
|
|
supervisor: {
|
|
waitForExit: async () => false,
|
|
waitForOutputClose: async () => false,
|
|
limitAdditionalOutputBytes: () => {},
|
|
dispose: () => {},
|
|
unrefOutputStreams: () => {},
|
|
},
|
|
},
|
|
"GDB",
|
|
{ platform: "linux" },
|
|
);
|
|
|
|
expect(result).toEqual({
|
|
status: "incomplete",
|
|
code: "detach-failed",
|
|
reason: expect.stringMatching(
|
|
/inferior detach.*could not be verified.*detach state unknown/i,
|
|
),
|
|
});
|
|
expect(signals).toEqual([]);
|
|
});
|
|
|
|
it("does not signal a POSIX debugger when provider detach cannot be verified", async () => {
|
|
const signals: NodeJS.Signals[] = [];
|
|
const result = await stopOwnedDebuggerProcess(
|
|
{
|
|
id: "unverified-detach",
|
|
exited: false,
|
|
launched: { process: { kill: (signal) => (signals.push(signal), true) } },
|
|
detachInferior: async () => {
|
|
throw new Error("MI detach failed");
|
|
},
|
|
supervisor: {
|
|
waitForExit: async () => false,
|
|
limitAdditionalOutputBytes: vi.fn(),
|
|
waitForOutputClose: async () => false,
|
|
dispose: () => {},
|
|
unrefOutputStreams: () => {},
|
|
},
|
|
},
|
|
"GDB",
|
|
{ platform: "linux" },
|
|
);
|
|
|
|
expect(result).toEqual({
|
|
status: "incomplete",
|
|
code: "detach-failed",
|
|
reason: "Owned GDB inferior detach could not be verified: MI detach failed",
|
|
});
|
|
expect(signals).toEqual([]);
|
|
});
|
|
|
|
it("terminates the owned debugger when the provider declares detach unavailable", async () => {
|
|
const signals: NodeJS.Signals[] = [];
|
|
const supervisor = {
|
|
...createFakeSupervisor(),
|
|
waitForExit: async () => true,
|
|
};
|
|
const result = await stopOwnedDebuggerProcess(
|
|
{
|
|
id: "unavailable-detach",
|
|
exited: false,
|
|
launched: { process: { kill: (signal) => (signals.push(signal), true) } },
|
|
detachInferior: async () => ({
|
|
status: "unavailable",
|
|
reason: "Rizin did not resynchronize cleanup within 500 ms",
|
|
}),
|
|
supervisor,
|
|
},
|
|
"Rizin",
|
|
{ platform: "linux" },
|
|
);
|
|
|
|
expect(result).toEqual({
|
|
status: "stopped_inferior_unknown",
|
|
reason: "Rizin did not resynchronize cleanup within 500 ms",
|
|
});
|
|
expect(signals).toEqual(["SIGTERM"]);
|
|
expect(supervisor.dispose).toHaveBeenCalledOnce();
|
|
expect(supervisor.unrefOutputStreams).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("escalates an unavailable-detach termination that ignores SIGTERM", async () => {
|
|
const signals: NodeJS.Signals[] = [];
|
|
let waits = 0;
|
|
const result = await stopOwnedDebuggerProcess(
|
|
{
|
|
id: "unavailable-detach-forced",
|
|
exited: false,
|
|
launched: { process: { kill: (signal) => (signals.push(signal), true) } },
|
|
detachInferior: async () => ({
|
|
status: "unavailable",
|
|
reason: "transport desynchronized",
|
|
}),
|
|
supervisor: {
|
|
waitForExit: async () => ++waits === 2,
|
|
limitAdditionalOutputBytes: vi.fn(),
|
|
waitForOutputClose: async () => true,
|
|
dispose: vi.fn(),
|
|
unrefOutputStreams: vi.fn(),
|
|
},
|
|
},
|
|
"Rizin",
|
|
{ platform: "linux" },
|
|
);
|
|
|
|
expect(result).toEqual({
|
|
status: "stopped_inferior_unknown",
|
|
reason: "transport desynchronized",
|
|
});
|
|
expect(signals).toEqual(["SIGTERM", "SIGKILL"]);
|
|
});
|
|
|
|
it("retains an unavailable-detach session until its output streams settle", async () => {
|
|
let outputClosed = false;
|
|
const session = {
|
|
id: "unavailable-detach-output",
|
|
exited: false,
|
|
launched: { process: { kill: vi.fn(() => true) } },
|
|
detachInferior: async () => ({
|
|
status: "unavailable" as const,
|
|
reason: "transport desynchronized",
|
|
}),
|
|
supervisor: {
|
|
waitForExit: async () => true,
|
|
limitAdditionalOutputBytes: vi.fn(),
|
|
waitForOutputClose: async () => outputClosed,
|
|
dispose: vi.fn(),
|
|
unrefOutputStreams: vi.fn(),
|
|
},
|
|
};
|
|
|
|
await expect(
|
|
stopOwnedDebuggerProcess(session, "Rizin", { platform: "linux" }),
|
|
).resolves.toMatchObject({ status: "incomplete" });
|
|
outputClosed = true;
|
|
await expect(
|
|
stopOwnedDebuggerProcess(session, "Rizin", { platform: "linux" }),
|
|
).resolves.toEqual({
|
|
status: "stopped_inferior_unknown",
|
|
reason: "transport desynchronized",
|
|
});
|
|
});
|
|
|
|
it("skips inferior-ended proof for a recorded unavailable detach after exit", async () => {
|
|
let exited = false;
|
|
let inferiorProofs = 0;
|
|
const session = {
|
|
id: "unavailable-detach-exited",
|
|
get exited() {
|
|
return exited;
|
|
},
|
|
inferiorStateUnknown: () => true,
|
|
verifyInferiorsEndedAfterExit: async () => {
|
|
inferiorProofs += 1;
|
|
return false;
|
|
},
|
|
launched: { process: { pid: 101, kill: vi.fn(() => true) } },
|
|
detachInferior: async () => ({
|
|
status: "unavailable" as const,
|
|
reason: "transport desynchronized",
|
|
}),
|
|
supervisor: {
|
|
waitForExit: async () => false,
|
|
limitAdditionalOutputBytes: vi.fn(),
|
|
waitForOutputClose: async () => true,
|
|
dispose: vi.fn(),
|
|
unrefOutputStreams: vi.fn(),
|
|
},
|
|
};
|
|
|
|
await expect(
|
|
stopOwnedDebuggerProcess(session, "Rizin", { platform: "linux" }),
|
|
).resolves.toMatchObject({ status: "incomplete" });
|
|
exited = true;
|
|
await expect(
|
|
stopOwnedDebuggerProcess(session, "Rizin", { platform: "linux" }),
|
|
).resolves.toEqual({
|
|
status: "stopped_inferior_unknown",
|
|
reason: "transport desynchronized",
|
|
});
|
|
expect(inferiorProofs).toBe(0);
|
|
});
|
|
|
|
it("retains an exited debugger after provider detach failed", async () => {
|
|
let exited = false;
|
|
let disposals = 0;
|
|
const session = {
|
|
id: "detach-failed-then-exited",
|
|
get exited() {
|
|
return exited;
|
|
},
|
|
launched: { process: { pid: 101, kill: () => true } },
|
|
detachInferior: async () => {
|
|
throw new Error("MI detach failed");
|
|
},
|
|
supervisor: {
|
|
waitForExit: async () => true,
|
|
limitAdditionalOutputBytes: vi.fn(),
|
|
waitForOutputClose: async () => true,
|
|
dispose: () => {
|
|
disposals += 1;
|
|
},
|
|
unrefOutputStreams: () => {},
|
|
ref: () => {},
|
|
unref: () => {},
|
|
},
|
|
};
|
|
const sessions = new Map([[session.id, session]]);
|
|
|
|
expect(
|
|
await stopOwnedDebuggerProcess(session, "GDB", { platform: "linux" }),
|
|
).toEqual({
|
|
status: "incomplete",
|
|
code: "detach-failed",
|
|
reason: "Owned GDB inferior detach could not be verified: MI detach failed",
|
|
});
|
|
|
|
exited = true;
|
|
await expect(
|
|
closeOwnedDebuggerSessions(
|
|
sessions,
|
|
(owned) => stopOwnedDebuggerProcess(owned, "GDB", { platform: "linux" }),
|
|
"GDB",
|
|
),
|
|
).rejects.toThrow("One or more owned GDB processes did not stop cleanly");
|
|
expect(sessions.get(session.id)).toBe(session);
|
|
expect(disposals).toBe(0);
|
|
});
|
|
|
|
it("recovers failed detach only after the identified inferior ends and output settles", async () => {
|
|
let exited = false;
|
|
let inferiorEnded = false;
|
|
const supervisor = {
|
|
...createFakeSupervisor(),
|
|
waitForOutputClose: vi.fn(async () => true),
|
|
};
|
|
const session = {
|
|
id: "failed-detach-recovery",
|
|
get exited() {
|
|
return exited;
|
|
},
|
|
launched: { process: { pid: 101, kill: vi.fn(() => true) } },
|
|
detachInferior: async () => {
|
|
throw new Error("detach failed");
|
|
},
|
|
verifyInferiorsEndedAfterExit: async () => inferiorEnded,
|
|
supervisor,
|
|
};
|
|
const sessions = new Map([[session.id, session]]);
|
|
const stop = (owned: typeof session) =>
|
|
stopOwnedDebuggerProcess(owned, "GDB", { platform: "linux" });
|
|
expect(await stop(session)).toMatchObject({ status: "incomplete" });
|
|
exited = true;
|
|
expect(await stop(session)).toMatchObject({ status: "incomplete" });
|
|
expect(supervisor.waitForOutputClose).not.toHaveBeenCalled();
|
|
expect(session.launched.process.kill).not.toHaveBeenCalled();
|
|
inferiorEnded = true;
|
|
await closeOwnedDebuggerSessions(sessions, stop, "GDB");
|
|
expect(supervisor.waitForOutputClose).toHaveBeenCalledOnce();
|
|
expect(supervisor.dispose).toHaveBeenCalledOnce();
|
|
expect(sessions.size).toBe(0);
|
|
});
|
|
|
|
it.each([
|
|
{ observed: "100", ended: false },
|
|
{ observed: "200", ended: true },
|
|
{ observed: null, ended: true },
|
|
])(
|
|
"compares captured inferior identity with $observed",
|
|
async ({ observed, ended }) => {
|
|
expect(
|
|
await verifyOwnedInferiorsEnded(
|
|
[{ pid: "123", startTime: "100" }],
|
|
async () => observed,
|
|
),
|
|
).toBe(ended);
|
|
},
|
|
);
|
|
|
|
it("preserves unknown identity reads and rejects empty proof", async () => {
|
|
const read = vi.fn(async () => {
|
|
throw new Error("identity unreadable");
|
|
});
|
|
await expect(
|
|
verifyOwnedInferiorsEnded([{ pid: "123", startTime: "100" }], read),
|
|
).rejects.toThrow("identity unreadable");
|
|
read.mockClear();
|
|
expect(await verifyOwnedInferiorsEnded([], read)).toBe(false);
|
|
expect(read).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("allows closing when the provider confirms no inferior is attached", async () => {
|
|
const signals: NodeJS.Signals[] = [];
|
|
const result = await stopOwnedDebuggerProcess(
|
|
{
|
|
id: "no-inferior",
|
|
exited: false,
|
|
launched: { process: { kill: (signal) => (signals.push(signal), true) } },
|
|
detachInferior: async () => ({ status: "no-inferior" }),
|
|
supervisor: {
|
|
waitForExit: async () => true,
|
|
limitAdditionalOutputBytes: vi.fn(),
|
|
waitForOutputClose: async () => true,
|
|
dispose: () => {},
|
|
unrefOutputStreams: () => {},
|
|
},
|
|
},
|
|
"GDB",
|
|
{ platform: "linux" },
|
|
);
|
|
|
|
expect(result).toEqual({ status: "stopped" });
|
|
expect(signals).toEqual(["SIGTERM"]);
|
|
});
|
|
|
|
it("fails closed when detach lacks durable output sink confirmation", async () => {
|
|
const signals: NodeJS.Signals[] = [];
|
|
const result = await stopOwnedDebuggerProcess(
|
|
{
|
|
id: "non-durable-sink",
|
|
exited: false,
|
|
launched: { process: { kill: (signal) => (signals.push(signal), true) } },
|
|
detachInferior: async () => ({ status: "detached" }) as never,
|
|
supervisor: {
|
|
waitForExit: async () => true,
|
|
limitAdditionalOutputBytes: vi.fn(),
|
|
waitForOutputClose: async () => false,
|
|
dispose: () => {},
|
|
unrefOutputStreams: () => {},
|
|
},
|
|
},
|
|
"GDB",
|
|
{ platform: "linux" },
|
|
);
|
|
|
|
expect(result).toEqual({
|
|
status: "incomplete",
|
|
code: "detach-unconfirmed",
|
|
reason: expect.stringMatching(
|
|
/did not confirm no inferior or detachment.*durable output sink/i,
|
|
),
|
|
});
|
|
expect(signals).toEqual([]);
|
|
});
|
|
|
|
it("retains an owned debugger when inferior detach does not settle", async () => {
|
|
vi.useFakeTimers();
|
|
const signals: NodeJS.Signals[] = [];
|
|
let detachCalls = 0;
|
|
let resolveDetach!: (value: {
|
|
readonly status: "detached";
|
|
readonly outputSink: "durable";
|
|
}) => void;
|
|
let disposals = 0;
|
|
try {
|
|
const session = {
|
|
id: "detach-timeout",
|
|
exited: false,
|
|
launched: {
|
|
process: {
|
|
kill: (signal: NodeJS.Signals) => (signals.push(signal), true),
|
|
},
|
|
},
|
|
detachInferior: () => {
|
|
detachCalls += 1;
|
|
return new Promise<{
|
|
readonly status: "detached";
|
|
readonly outputSink: "durable";
|
|
}>((resolve) => {
|
|
resolveDetach = resolve;
|
|
});
|
|
},
|
|
supervisor: {
|
|
waitForExit: async () => true,
|
|
limitAdditionalOutputBytes: vi.fn(),
|
|
waitForOutputClose: async () => true,
|
|
dispose: () => {
|
|
disposals += 1;
|
|
},
|
|
unrefOutputStreams: () => {},
|
|
},
|
|
};
|
|
|
|
const attempt = stopOwnedDebuggerProcess(session, "GDB", {
|
|
platform: "linux",
|
|
});
|
|
await vi.advanceTimersByTimeAsync(1_000);
|
|
expect(await attempt).toEqual({
|
|
status: "incomplete",
|
|
code: "detach-timeout",
|
|
reason: expect.stringMatching(
|
|
/inferior detach.*not verified.*1000 ms.*no runtime override/i,
|
|
),
|
|
});
|
|
const retry = stopOwnedDebuggerProcess(session, "GDB", {
|
|
platform: "linux",
|
|
});
|
|
await vi.advanceTimersByTimeAsync(1_000);
|
|
expect(await retry).toEqual({
|
|
status: "incomplete",
|
|
code: "detach-timeout",
|
|
reason: expect.stringMatching(
|
|
/inferior detach.*not verified.*1000 ms.*no runtime override/i,
|
|
),
|
|
});
|
|
expect(detachCalls).toBe(1);
|
|
expect(signals).toEqual([]);
|
|
expect(disposals).toBe(0);
|
|
|
|
resolveDetach({ status: "detached", outputSink: "durable" });
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
expect(
|
|
await stopOwnedDebuggerProcess(session, "GDB", { platform: "linux" }),
|
|
).toEqual({ status: "stopped" });
|
|
expect(detachCalls).toBe(1);
|
|
expect(signals).toEqual(["SIGTERM"]);
|
|
expect(disposals).toBe(1);
|
|
} finally {
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
|
|
it("retains an exited session while a timed-out detach attempt is still pending", async () => {
|
|
vi.useFakeTimers();
|
|
let exited = false;
|
|
let resolveDetach!: (value: { readonly status: "no-inferior" }) => void;
|
|
let detachCalls = 0;
|
|
const session = {
|
|
id: "exited-with-pending-detach",
|
|
get exited() {
|
|
return exited;
|
|
},
|
|
launched: { process: { pid: 101, kill: () => true } },
|
|
detachInferior: () => {
|
|
detachCalls += 1;
|
|
return new Promise<{ readonly status: "no-inferior" }>((resolve) => {
|
|
resolveDetach = resolve;
|
|
});
|
|
},
|
|
supervisor: {
|
|
waitForExit: async () => true,
|
|
limitAdditionalOutputBytes: vi.fn(),
|
|
waitForOutputClose: async () => true,
|
|
dispose: vi.fn(),
|
|
unrefOutputStreams: vi.fn(),
|
|
ref: vi.fn(),
|
|
unref: vi.fn(),
|
|
},
|
|
};
|
|
const sessions = new Map([[session.id, session]]);
|
|
|
|
try {
|
|
const attempt = stopOwnedDebuggerProcess(session, "GDB", {
|
|
platform: "linux",
|
|
});
|
|
await vi.advanceTimersByTimeAsync(1_000);
|
|
expect(await attempt).toMatchObject({ status: "incomplete" });
|
|
|
|
exited = true;
|
|
await expect(
|
|
closeOwnedDebuggerSessions(
|
|
sessions,
|
|
(owned) =>
|
|
stopOwnedDebuggerProcess(owned, "GDB", { platform: "linux" }),
|
|
"GDB",
|
|
),
|
|
).rejects.toThrow("One or more owned GDB processes did not stop cleanly");
|
|
expect(sessions.get(session.id)).toBe(session);
|
|
expect(session.supervisor.dispose).not.toHaveBeenCalled();
|
|
expect(detachCalls).toBe(1);
|
|
|
|
resolveDetach({ status: "no-inferior" });
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
await Promise.resolve();
|
|
await expect(
|
|
closeOwnedDebuggerSessions(
|
|
sessions,
|
|
(owned) =>
|
|
stopOwnedDebuggerProcess(owned, "GDB", { platform: "linux" }),
|
|
"GDB",
|
|
),
|
|
).resolves.toBeUndefined();
|
|
expect(sessions.size).toBe(0);
|
|
expect(session.supervisor.dispose).toHaveBeenCalledTimes(1);
|
|
expect(detachCalls).toBe(1);
|
|
} finally {
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
|
|
it("requires the Windows Job Object to close before releasing an exited session", async () => {
|
|
let closeWaits = 0;
|
|
let disposals = 0;
|
|
const session = {
|
|
id: "windows-job-settlement",
|
|
exited: true,
|
|
launched: { process: { kill: () => true } },
|
|
supervisor: {
|
|
waitForExit: async () => true,
|
|
limitAdditionalOutputBytes: vi.fn(),
|
|
waitForOutputClose: async () => ++closeWaits > 1,
|
|
dispose: () => {
|
|
disposals += 1;
|
|
},
|
|
unrefOutputStreams: () => {},
|
|
},
|
|
};
|
|
|
|
expect(
|
|
await stopOwnedDebuggerProcess(session, "GDB", { platform: "win32" }),
|
|
).toMatchObject({ status: "incomplete" });
|
|
expect(disposals).toBe(0);
|
|
expect(
|
|
await stopOwnedDebuggerProcess(session, "GDB", { platform: "win32" }),
|
|
).toEqual({ status: "already_exited" });
|
|
expect(closeWaits).toBe(2);
|
|
expect(disposals).toBe(1);
|
|
});
|
|
|
|
it("disposes supervision when a process had already exited", async () => {
|
|
let disposals = 0;
|
|
const lifecycle: string[] = [];
|
|
let outputUnrefs = 0;
|
|
const result = await stopOwnedDebuggerProcess(
|
|
{
|
|
id: "already-exited",
|
|
exited: true,
|
|
launched: { process: { kill: () => true } },
|
|
supervisor: {
|
|
waitForExit: async () => false,
|
|
limitAdditionalOutputBytes: vi.fn(),
|
|
waitForOutputClose: async () => true,
|
|
dispose: () => {
|
|
lifecycle.push("dispose");
|
|
disposals += 1;
|
|
},
|
|
unrefOutputStreams: () => {
|
|
lifecycle.push("unref");
|
|
outputUnrefs += 1;
|
|
},
|
|
},
|
|
},
|
|
"GDB",
|
|
{ platform: "linux" },
|
|
);
|
|
|
|
expect(result).toEqual({ status: "already_exited" });
|
|
expect(disposals).toBe(1);
|
|
expect(outputUnrefs).toBe(1);
|
|
expect(lifecycle).toEqual(["unref", "dispose"]);
|
|
});
|
|
|
|
it("uses the host platform when an exited debugger is stopped without options", async () => {
|
|
let outputCloseWaits = 0;
|
|
let outputUnrefs = 0;
|
|
const result = await stopOwnedDebuggerProcess(
|
|
{
|
|
id: "already-exited-default-platform",
|
|
exited: true,
|
|
launched: { process: { kill: () => true } },
|
|
supervisor: {
|
|
waitForExit: async () => true,
|
|
waitForOutputClose: async (timeoutMs) => {
|
|
expect(timeoutMs).toBe(1_000);
|
|
outputCloseWaits += 1;
|
|
return true;
|
|
},
|
|
limitAdditionalOutputBytes: () => {},
|
|
dispose: () => {},
|
|
unrefOutputStreams: () => {
|
|
outputUnrefs += 1;
|
|
},
|
|
},
|
|
},
|
|
"GDB",
|
|
);
|
|
|
|
expect(result).toEqual({ status: "already_exited" });
|
|
expect(outputCloseWaits).toBe(1);
|
|
expect(outputUnrefs).toBe(process.platform === "win32" ? 0 : 1);
|
|
});
|
|
|
|
it.skipIf(process.platform === "win32")(
|
|
"keeps supervision until a late-writing descendant closes inherited output after leader exit",
|
|
async () => {
|
|
const inferiorSource = [
|
|
'const { createServer } = require("node:net");',
|
|
"const server = createServer((socket) => {",
|
|
' process.stdout.write("late-stdout-sentinel\\n");',
|
|
' process.stderr.write("late-stderr-sentinel\\n");',
|
|
" socket.end();",
|
|
" setImmediate(() => process.exit(0));",
|
|
"});",
|
|
'server.listen(0, "127.0.0.1", () => process.stdout.write(`ready:${server.address().port}\\n`));',
|
|
].join("\n");
|
|
const { child, supervisor, inferiorPort } =
|
|
startDebuggerWithInferior(inferiorSource);
|
|
let port: number | undefined;
|
|
|
|
try {
|
|
await once(child, "exit");
|
|
const readyPort = await inferiorPort;
|
|
port = readyPort;
|
|
const limitAdditionalOutputBytes =
|
|
supervisor.limitAdditionalOutputBytes.bind(supervisor);
|
|
vi.spyOn(supervisor, "limitAdditionalOutputBytes").mockImplementation(
|
|
(maxBytes) => {
|
|
limitAdditionalOutputBytes(maxBytes);
|
|
triggerDescendantOutput(readyPort);
|
|
},
|
|
);
|
|
const result = await stopOwnedDebuggerProcess(
|
|
{
|
|
id: "leader-exited-with-inferior",
|
|
exited: true,
|
|
launched: { process: child },
|
|
supervisor,
|
|
},
|
|
"GDB",
|
|
{ platform: "linux" },
|
|
);
|
|
|
|
expect(result).toEqual({ status: "already_exited" });
|
|
expect(supervisor.snapshot().stdout.text).toContain(
|
|
"late-stdout-sentinel",
|
|
);
|
|
expect(supervisor.snapshot().stderr.text).toContain(
|
|
"late-stderr-sentinel",
|
|
);
|
|
} finally {
|
|
if (port !== undefined) triggerDescendantOutput(port);
|
|
supervisor.dispose();
|
|
}
|
|
},
|
|
);
|
|
|
|
it.skipIf(process.platform === "win32")(
|
|
"keeps late debugger output bounded and drained across cleanup retries",
|
|
async () => {
|
|
const inferiorSource = [
|
|
'const { createServer } = require("node:net");',
|
|
"const writePair = (stdout, stderr) => Promise.all([",
|
|
" new Promise((resolve) => process.stdout.write(stdout, resolve)),",
|
|
" new Promise((resolve) => process.stderr.write(stderr, resolve)),",
|
|
"]);",
|
|
"let outputCount = 0;",
|
|
"const server = createServer((socket) => {",
|
|
" outputCount += 1;",
|
|
' const output = outputCount === 1 ? writePair("a".repeat(40 * 1024), "b".repeat(40 * 1024)) : writePair("c".repeat(40 * 1024), "d".repeat(40 * 1024));',
|
|
" socket.end();",
|
|
" if (outputCount === 2) void output.then(() => process.exit(0));",
|
|
"});",
|
|
'server.listen(0, "127.0.0.1", () => process.stdout.write(`ready:${server.address().port}\\n`));',
|
|
].join("\n");
|
|
const { child, supervisor, inferiorPort } =
|
|
startDebuggerWithInferior(inferiorSource);
|
|
let port: number | undefined;
|
|
const session = {
|
|
id: "leader-exited-retrying-inferior",
|
|
exited: true,
|
|
launched: { process: child },
|
|
supervisor,
|
|
};
|
|
|
|
try {
|
|
await once(child, "exit");
|
|
const readyPort = await inferiorPort;
|
|
port = readyPort;
|
|
const initialStdoutBytes = supervisor.snapshot().stdout.observedBytes;
|
|
const limitAdditionalOutputBytes =
|
|
supervisor.limitAdditionalOutputBytes.bind(supervisor);
|
|
vi.spyOn(supervisor, "limitAdditionalOutputBytes").mockImplementation(
|
|
(maxBytes) => {
|
|
limitAdditionalOutputBytes(maxBytes);
|
|
triggerDescendantOutput(readyPort);
|
|
},
|
|
);
|
|
const first = await stopOwnedDebuggerProcess(session, "GDB", {
|
|
platform: "linux",
|
|
});
|
|
expect(first.status).toBe("incomplete");
|
|
expect(supervisor.snapshot().stdout.observedBytes).toBe(
|
|
initialStdoutBytes + 40 * 1024,
|
|
);
|
|
expect(supervisor.snapshot().stderr.observedBytes).toBe(40 * 1024);
|
|
|
|
const second = await stopOwnedDebuggerProcess(session, "GDB", {
|
|
platform: "linux",
|
|
});
|
|
expect(second).toEqual({ status: "already_exited" });
|
|
const snapshot = supervisor.snapshot();
|
|
expect(snapshot.stdout.observedBytes).toBe(
|
|
initialStdoutBytes + 80 * 1024,
|
|
);
|
|
expect(snapshot.stderr.observedBytes).toBe(80 * 1024);
|
|
expect(snapshot.stdout.bytes + snapshot.stderr.bytes).toBe(
|
|
initialStdoutBytes + 64 * 1024,
|
|
);
|
|
expect(snapshot.diagnosticTruncated).toBe(true);
|
|
} finally {
|
|
if (port !== undefined) {
|
|
triggerDescendantOutput(port);
|
|
triggerDescendantOutput(port);
|
|
}
|
|
if (child.exitCode === null && child.signalCode === null)
|
|
child.kill("SIGKILL");
|
|
supervisor.dispose();
|
|
}
|
|
},
|
|
);
|
|
|
|
it("does not escalate when the debugger exits after SIGTERM", async () => {
|
|
const signals: NodeJS.Signals[] = [];
|
|
const lifecycle: string[] = [];
|
|
let disposals = 0;
|
|
const result = await stopOwnedDebuggerProcess(
|
|
{
|
|
id: "graceful-exit",
|
|
exited: false,
|
|
launched: {
|
|
process: {
|
|
kill: (signal: NodeJS.Signals): boolean => {
|
|
signals.push(signal);
|
|
return true;
|
|
},
|
|
},
|
|
},
|
|
detachInferior: async () =>
|
|
({
|
|
status: "detached",
|
|
outputSink: "durable",
|
|
}) as const,
|
|
supervisor: {
|
|
waitForExit: async () => true,
|
|
limitAdditionalOutputBytes: vi.fn(),
|
|
waitForOutputClose: async () => true,
|
|
dispose: () => {
|
|
lifecycle.push("dispose");
|
|
disposals += 1;
|
|
},
|
|
unrefOutputStreams: () => lifecycle.push("unref"),
|
|
},
|
|
},
|
|
"Rizin",
|
|
{ platform: "linux" },
|
|
);
|
|
|
|
expect(result).toEqual({ status: "stopped" });
|
|
expect(signals).toEqual(["SIGTERM"]);
|
|
expect(disposals).toBe(1);
|
|
expect(lifecycle).toEqual(["unref", "dispose"]);
|
|
});
|
|
|
|
it("disposes supervision when the debugger exits after SIGKILL", async () => {
|
|
let waits = 0;
|
|
let disposals = 0;
|
|
const lifecycle: string[] = [];
|
|
const signals: NodeJS.Signals[] = [];
|
|
const result = await stopOwnedDebuggerProcess(
|
|
{
|
|
id: "forced-exit",
|
|
exited: false,
|
|
launched: {
|
|
process: {
|
|
kill: (signal: NodeJS.Signals): boolean => {
|
|
signals.push(signal);
|
|
return true;
|
|
},
|
|
},
|
|
},
|
|
detachInferior: async () =>
|
|
({
|
|
status: "detached",
|
|
outputSink: "durable",
|
|
}) as const,
|
|
supervisor: {
|
|
waitForExit: async () => {
|
|
waits += 1;
|
|
return waits === 2;
|
|
},
|
|
limitAdditionalOutputBytes: vi.fn(),
|
|
waitForOutputClose: async () => true,
|
|
dispose: () => {
|
|
lifecycle.push("dispose");
|
|
disposals += 1;
|
|
},
|
|
unrefOutputStreams: () => lifecycle.push("unref"),
|
|
},
|
|
},
|
|
"Rizin",
|
|
{ platform: "linux" },
|
|
);
|
|
|
|
expect(result).toEqual({ status: "stopped" });
|
|
expect(signals).toEqual(["SIGTERM", "SIGKILL"]);
|
|
expect(disposals).toBe(1);
|
|
expect(lifecycle).toEqual(["unref", "dispose"]);
|
|
});
|
|
|
|
it.skipIf(process.platform === "win32")(
|
|
"retains the debugger owner while an unknown descendant holds output pipes open",
|
|
async () => {
|
|
const releaseDirectory = await createTestTempDirectory(
|
|
"rea-debugger-output-writer-",
|
|
);
|
|
const releaseFile = join(releaseDirectory, "release-writer");
|
|
const writerSource = `const{existsSync,watch}=require("node:fs");const watcher=watch(${JSON.stringify(releaseDirectory)},()=>{if(existsSync(${JSON.stringify(releaseFile)})){watcher.close();process.exit(0)}});process.stdout.write("writer-ready\\n");`;
|
|
const providerProcessUrl = new URL(
|
|
"../../../dist/process/ProviderProcess.js",
|
|
import.meta.url,
|
|
).href;
|
|
const debuggerSessionUrl = new URL(
|
|
"../../../dist/process/ownedDebuggerSession.js",
|
|
import.meta.url,
|
|
).href;
|
|
const debuggerSource = [
|
|
'const { spawn } = require("node:child_process");',
|
|
`spawn(process.execPath, ["-e", ${JSON.stringify(writerSource)}], { stdio: "inherit" });`,
|
|
"process.stdout.write(`debugger=${process.pid}\\n`);",
|
|
"setInterval(() => {}, 1000);",
|
|
].join("\n");
|
|
const probeSource = [
|
|
'import { spawn } from "node:child_process";',
|
|
'import { once } from "node:events";',
|
|
`import { ProviderProcessSupervisor } from ${JSON.stringify(providerProcessUrl)};`,
|
|
`import { stopOwnedDebuggerProcess } from ${JSON.stringify(debuggerSessionUrl)};`,
|
|
`const child = spawn(process.execPath, ["-e", ${JSON.stringify(debuggerSource)}], { stdio: ["ignore", "pipe", "pipe"] });`,
|
|
"const supervisor = new ProviderProcessSupervisor({ process: child, ownsProcessLifetime: true });",
|
|
"const closed = Promise.all([once(child.stdout, 'close'), once(child.stderr, 'close')]);",
|
|
"let output = '';",
|
|
"await new Promise((resolve, reject) => {",
|
|
" child.stdout.on('data', (chunk) => {",
|
|
" output += chunk.toString();",
|
|
" const debuggerMatch = /^debugger=(\\d+)/mu.exec(output);",
|
|
" if (debuggerMatch) process.stderr.write(`debugger-pid=${debuggerMatch[1]}\\n`);",
|
|
" if (output.includes('writer-ready\\n')) { resolve(); }",
|
|
" });",
|
|
" child.once('error', reject);",
|
|
" child.once('close', () => reject(new Error('output writer did not report readiness: ' + output)));",
|
|
"});",
|
|
"const session = { id: 'debugger', get exited() { return child.exitCode !== null || child.signalCode !== null; }, launched: { process: child }, supervisor };",
|
|
"session.detachInferior = async () => ({ status: 'no-inferior' });",
|
|
"const result = await stopOwnedDebuggerProcess(session, 'GDB', { platform: 'linux' });",
|
|
"process.stdout.write(JSON.stringify({ result }) + '\\n');",
|
|
"await new Promise((resolve) => process.stdin.once('data', () => { process.stdin.destroy(); resolve(); }));",
|
|
"await closed;",
|
|
"const retried = await stopOwnedDebuggerProcess(session, 'GDB', { platform: 'linux' });",
|
|
"process.stdout.write(JSON.stringify({ retried }) + '\\n');",
|
|
].join("\n");
|
|
const stopProbe = startStopProbe(probeSource, true);
|
|
const probe = stopProbe.child;
|
|
await waitFor(() =>
|
|
expect(stopProbe.output).toMatch(/^\{"result":.*\}\r?\n/mu),
|
|
);
|
|
expect(stopProbe.output).toContain(
|
|
'"status":"incomplete","code":"output-unsettled"',
|
|
);
|
|
expect(probe.exitCode).toBeNull();
|
|
expect(probe.signalCode).toBeNull();
|
|
|
|
await writeFile(releaseFile, "release");
|
|
probe.stdin?.end("retry\n");
|
|
expect(await waitForExit(probe, testWaitDeadline()), stopProbe.output).toBe(
|
|
true,
|
|
);
|
|
expect(probe.exitCode, stopProbe.output).toBe(0);
|
|
await waitFor(() =>
|
|
expect(stopProbe.output).toContain(
|
|
'{"retried":{"status":"already_exited"}}\n',
|
|
),
|
|
);
|
|
},
|
|
);
|
|
|
|
it.skipIf(process.platform === "win32")(
|
|
"preserves inferior output written to its selected PTY after the REA probe exits",
|
|
async () => {
|
|
const inferiorSource = [
|
|
'process.stdin.setEncoding("utf8");',
|
|
'process.stdin.on("data", (input) => { if (input.includes("write-after-probe")) { process.stdout.write("stdout-after-probe-sentinel\\n"); process.stderr.write("stderr-after-probe-sentinel\\n"); } });',
|
|
'process.stdout.write("inferior-ready\\n");',
|
|
].join("\n");
|
|
const terminal = spawnPty(process.execPath, ["-e", inferiorSource], {
|
|
name: "xterm",
|
|
cols: 100,
|
|
rows: 20,
|
|
});
|
|
let terminalExited = false;
|
|
terminal.onExit(() => {
|
|
terminalExited = true;
|
|
});
|
|
onTestFinished(async () => {
|
|
if (!terminalExited) terminal.kill("SIGKILL");
|
|
await waitFor(() =>
|
|
expect(terminalExited, "PTY inferior exit").toBe(true),
|
|
);
|
|
});
|
|
let terminalOutput = "";
|
|
terminal.onData((data) => {
|
|
terminalOutput += data;
|
|
});
|
|
await waitFor(() => expect(terminalOutput).toContain("inferior-ready"));
|
|
const providerProcessUrl = new URL(
|
|
"../../../dist/process/ProviderProcess.js",
|
|
import.meta.url,
|
|
).href;
|
|
const debuggerSessionUrl = new URL(
|
|
"../../../dist/process/ownedDebuggerSession.js",
|
|
import.meta.url,
|
|
).href;
|
|
const probeSource = [
|
|
'import { spawn } from "node:child_process";',
|
|
`import { ProviderProcessSupervisor } from ${JSON.stringify(providerProcessUrl)};`,
|
|
`import { stopOwnedDebuggerProcess } from ${JSON.stringify(debuggerSessionUrl)};`,
|
|
'const child = spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)"], { stdio: ["ignore", "pipe", "pipe"] });',
|
|
"process.stdout.write(`debugger-pid=${child.pid}\\n`);",
|
|
"const supervisor = new ProviderProcessSupervisor({ process: child, ownsProcessLifetime: true });",
|
|
"const session = { id: 'debugger-with-selected-pty-inferior', get exited() { return child.exitCode !== null || child.signalCode !== null; }, launched: { process: child }, supervisor, detachInferior: async () => ({ status: 'detached', outputSink: 'durable' }) };",
|
|
"const result = await stopOwnedDebuggerProcess(session, 'GDB', { platform: 'linux' });",
|
|
"process.stdout.write(`probe-result=${JSON.stringify(result)}\\n`);",
|
|
].join("\n");
|
|
const stopProbe = startStopProbe(probeSource);
|
|
const probe = stopProbe.child;
|
|
expect(await waitForExit(probe, testWaitDeadline()), stopProbe.output).toBe(
|
|
true,
|
|
);
|
|
expect(probe.exitCode, stopProbe.output).toBe(0);
|
|
await waitFor(() =>
|
|
expect(stopProbe.output).toContain('probe-result={"status":"stopped"}'),
|
|
);
|
|
expect(terminalExited).toBe(false);
|
|
|
|
terminal.write("write-after-probe\n");
|
|
await waitFor(() => {
|
|
expect(terminalOutput).toContain("stdout-after-probe-sentinel");
|
|
expect(terminalOutput).toContain("stderr-after-probe-sentinel");
|
|
});
|
|
expect(terminalExited).toBe(false);
|
|
},
|
|
);
|
|
|
|
it("removes stopped sessions and retains incomplete sessions when closing all", async () => {
|
|
const sessions = new Map([
|
|
["stopped", closableSession("stopped")],
|
|
["incomplete", closableSession("incomplete")],
|
|
]);
|
|
|
|
await expect(
|
|
closeOwnedDebuggerSessions(
|
|
sessions,
|
|
async (session) =>
|
|
session.id === "incomplete"
|
|
? {
|
|
status: "incomplete",
|
|
code: "stop-failed",
|
|
reason: "process remains alive",
|
|
}
|
|
: { status: "stopped" },
|
|
"GDB",
|
|
),
|
|
).rejects.toMatchObject({
|
|
errors: [
|
|
expect.objectContaining({
|
|
code: "stop-failed",
|
|
message: "process remains alive",
|
|
}),
|
|
],
|
|
});
|
|
expect([...sessions.keys()]).toEqual(["incomplete"]);
|
|
});
|
|
|
|
it("does not remove a replacement session installed under the same key while stopping", async () => {
|
|
const original = closableSession("shared-key");
|
|
const replacement = closableSession("shared-key");
|
|
const sessions = new Map([["shared-key", original]]);
|
|
let finishStop!: (result: { readonly status: "stopped" }) => void;
|
|
const stopping = new Promise<{ readonly status: "stopped" }>((resolve) => {
|
|
finishStop = resolve;
|
|
});
|
|
|
|
const closing = closeOwnedDebuggerSessions(
|
|
sessions,
|
|
async () => stopping,
|
|
"GDB",
|
|
);
|
|
await Promise.resolve();
|
|
sessions.set("shared-key", replacement);
|
|
finishStop({ status: "stopped" });
|
|
await closing;
|
|
|
|
expect(sessions.get("shared-key")).toBe(replacement);
|
|
});
|
|
|
|
it("closes an empty owned-session collection", async () => {
|
|
const sessions = new Map<string, ReturnType<typeof closableSession>>();
|
|
await expect(
|
|
closeOwnedDebuggerSessions(
|
|
sessions,
|
|
async () => ({ status: "stopped" }),
|
|
"GDB",
|
|
),
|
|
).resolves.toBeUndefined();
|
|
});
|
|
|
|
it("settles every stop and retains rejected sessions while removing successful ones", async () => {
|
|
const sessions = new Map([
|
|
["stopped", closableSession("stopped")],
|
|
["rejected", closableSession("rejected")],
|
|
["string-rejected", closableSession("string-rejected")],
|
|
["incomplete", closableSession("incomplete")],
|
|
]);
|
|
const calls: string[] = [];
|
|
|
|
await expect(
|
|
closeOwnedDebuggerSessions(
|
|
sessions,
|
|
async (session) => {
|
|
calls.push(session.id);
|
|
if (session.id === "rejected") throw new Error("signal failed");
|
|
if (session.id === "string-rejected") throw "string rejection";
|
|
if (session.id === "incomplete")
|
|
return {
|
|
status: "incomplete",
|
|
code: "stop-failed",
|
|
reason: "still alive",
|
|
};
|
|
return { status: "stopped" };
|
|
},
|
|
"GDB",
|
|
),
|
|
).rejects.toMatchObject({
|
|
message: "One or more owned GDB processes did not stop cleanly",
|
|
errors: [
|
|
expect.objectContaining({ message: "signal failed" }),
|
|
expect.objectContaining({ message: "string rejection" }),
|
|
expect.objectContaining({ message: "still alive" }),
|
|
],
|
|
});
|
|
|
|
expect(calls).toHaveLength(4);
|
|
expect([...sessions.keys()]).toEqual([
|
|
"rejected",
|
|
"string-rejected",
|
|
"incomplete",
|
|
]);
|
|
});
|
|
|
|
it("stops retained sessions holding the host and names them, leaving stopped peers alone", async () => {
|
|
const stopped = closableSession("stopped", 101);
|
|
const rejected = closableSession("rejected", 102);
|
|
const incomplete = closableSession("incomplete", 103);
|
|
const unidentified = closableSession("unidentified");
|
|
const sessions = new Map(
|
|
[stopped, rejected, incomplete, unidentified].map((session) => [
|
|
session.id,
|
|
session,
|
|
]),
|
|
);
|
|
|
|
const failure: unknown = await closeOwnedDebuggerSessions(
|
|
sessions,
|
|
async (session) => {
|
|
if (session === stopped) return { status: "stopped" };
|
|
if (session === rejected) throw new Error("signal failed");
|
|
return {
|
|
status: "incomplete",
|
|
code: "stop-failed",
|
|
reason: "still alive",
|
|
};
|
|
},
|
|
"Rizin",
|
|
).catch((cause: unknown) => cause);
|
|
|
|
expect(failure).toBeInstanceOf(OwnedDebuggerCleanupError);
|
|
expect(failure).toBeInstanceOf(AggregateError);
|
|
expect((failure as OwnedDebuggerCleanupError).retainedProcessIds).toEqual([
|
|
102, 103,
|
|
]);
|
|
expect(stopped.supervisor.unref).not.toHaveBeenCalled();
|
|
for (const retained of [rejected, incomplete, unidentified])
|
|
expect(retained.supervisor.unref).toHaveBeenCalledOnce();
|
|
expect([...sessions.keys()]).toEqual([
|
|
"rejected",
|
|
"incomplete",
|
|
"unidentified",
|
|
]);
|
|
});
|
|
|
|
it("holds the host again while retrying a retained session and leaves it held on success", async () => {
|
|
const session = closableSession("retained", 101);
|
|
const sessions = new Map([[session.id, session]]);
|
|
const order: string[] = [];
|
|
session.supervisor.ref.mockImplementation(() => order.push("ref"));
|
|
session.supervisor.unref.mockImplementation(() => order.push("unref"));
|
|
let stopResult: OwnedDebuggerStopResult = {
|
|
status: "incomplete",
|
|
code: "stop-failed",
|
|
reason: "still alive",
|
|
};
|
|
const stop = async () => {
|
|
order.push("stop");
|
|
return stopResult;
|
|
};
|
|
|
|
await expect(
|
|
closeOwnedDebuggerSessions(sessions, stop, "GDB"),
|
|
).rejects.toBeInstanceOf(OwnedDebuggerCleanupError);
|
|
expect(order).toEqual(["ref", "stop", "unref"]);
|
|
|
|
order.length = 0;
|
|
stopResult = { status: "stopped" };
|
|
await closeOwnedDebuggerSessions(sessions, stop, "GDB");
|
|
expect(order).toEqual(["ref", "stop"]);
|
|
expect(sessions.size).toBe(0);
|
|
});
|
|
|
|
it("finds retained debugger IDs through composed failures without looping on cycles", () => {
|
|
const first = new OwnedDebuggerCleanupError([], "first", [101, 102]);
|
|
const second = new OwnedDebuggerCleanupError([], "second", [102, 103]);
|
|
const cyclic = new Error("cyclic", { cause: second });
|
|
Object.assign(second, { cause: cyclic });
|
|
const composed = new AggregateError(
|
|
[first, new Error("wrapped", { cause: cyclic }), new Error("peer"), null],
|
|
"outer",
|
|
);
|
|
|
|
expect(retainedDebuggerProcessIds(composed)).toEqual([101, 102, 103]);
|
|
expect(retainedDebuggerProcessIds(new Error("peer"))).toEqual([]);
|
|
expect(retainedDebuggerProcessIds(undefined)).toEqual([]);
|
|
});
|
|
|
|
it("names every retained debugger in its remediation", () => {
|
|
const remediation = retainedDebuggerRemediation([4242, 4243]);
|
|
expect(remediation).toMatch(/no later REA command can retry this cleanup/iu);
|
|
expect(remediation).toMatch(/\b4242\b/u);
|
|
expect(remediation).toMatch(/\b4243\b/u);
|
|
});
|