test(gdb): consolidate queue capacity and cancellation checks (#13017)

* test(gdb): consolidate queue capacity and cancellation checks

* test(gdb): require queued followers to await preceding replies
This commit is contained in:
morluto authored and GitHub committed 2026-11-21 07:06:58 +00:00
1 parent 8ab232b4d8
commit 7966097e87
3 files changed
+146 -213

No files matched your search

+146 -118
View File
@@ -1,4 +1,6 @@
import {
createGdbFixtureOwner,
expectGdbSessionStarted,
expectNoUnmodeledGdbCommands,
FAKE_GDB_SESSION_REPLIES,
fakeGdbModeledReplies,
@@ -6,44 +8,40 @@ import {
} from "./gdbTestHelpers.js";
import { chmod, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, expect, it } from "vitest";
import { GdbSessionManager } from "../../../src/gdb/GdbSessionManager.js";
import { awaitAcknowledgement } from "../../fixtures/lifecycleAcknowledgement.js";
import { waitFor } from "../../support/testWait.js";
let directory: string | undefined;
const managers: GdbSessionManager[] = [];
const fixtureOwner = createGdbFixtureOwner();
afterEach(async () => {
const closing = managers.splice(0);
const outcomes = await Promise.allSettled(
closing.map((manager) => manager.closeAll()),
);
const failures: unknown[] = [];
outcomes.forEach((outcome, index) => {
if (outcome.status === "rejected") {
managers.push(closing[index]!);
failures.push(outcome.reason);
}
await fixtureOwner.cleanup(managers, async () => {
if (directory !== undefined)
await rm(directory, { recursive: true, force: true });
directory = undefined;
});
if (failures.length > 0)
throw new AggregateError(failures, "GDB queue cleanup failed");
if (directory !== undefined)
await rm(directory, { recursive: true, force: true });
directory = undefined;
});
it("bounds active and queued GDB commands, including cancelled placeholders", async () => {
directory = await mkdtemp(join(tmpdir(), "rea-gdb-command-queue-limit-"));
const consoleLine = (name: string) => `-interpreter-exec console "${name}"`;
const makeQueueProvider = async (directory: string) => {
const provider = join(directory, "fake-gdb-queue.mjs");
const activePath = join(directory, "active-command");
const releasePath = join(directory, "release-command");
const releasePaths = {
hold: join(directory, "release-hold"),
follower: join(directory, "release-follower"),
};
const unmodeledPath = join(directory, "unmodeled");
const consoleReplies = Object.fromEntries(
["hold", "admitted-after-drain", "after-drain"].map((command) => [
`-interpreter-exec console "${command}"`,
const tracePath = join(directory, "commands");
const replies = Object.fromEntries(
["hold", "first-follower", "second-follower", "after-drain"].map((name) => [
consoleLine(name),
"^done",
]),
);
@@ -51,120 +49,150 @@ it("bounds active and queued GDB commands, including cancelled placeholders", as
provider,
`#!${process.execPath}
${fakeGdbStartupPreamble}
${fakeGdbModeledReplies(unmodeledPath, { ...FAKE_GDB_SESSION_REPLIES, ...consoleReplies })}
import { access, writeFile } from "node:fs/promises";
${fakeGdbModeledReplies(unmodeledPath, { ...FAKE_GDB_SESSION_REPLIES, ...replies })}
import { appendFileSync, existsSync, writeFileSync } from "node:fs";
const activePath = ${JSON.stringify(activePath)};
const gates = new Map(${JSON.stringify([
[consoleLine("hold"), releasePaths.hold],
[consoleLine("first-follower"), releasePaths.follower],
])});
const tracePath = ${JSON.stringify(tracePath)};
let holding = false;
process.stdout.write("(gdb)\\n");
let input = "";
let firstConsoleCommand = true;
process.stdin.setEncoding("utf8");
process.stdin.on("data", async (chunk) => {
input += chunk;
for (;;) {
const boundary = input.indexOf("\\n");
if (boundary < 0) break;
const line = input.slice(0, boundary); input = input.slice(boundary + 1);
const match = /^(\\d+)/.exec(line);
process.stdin.on("data", (chunk) => {
for (const line of chunk.split("\\n")) {
const match = /^(\\d+)(.*)$/.exec(line);
if (!match) continue;
if (line.includes("-interpreter-exec console") && firstConsoleCommand) {
firstConsoleCommand = false;
await writeFile(${JSON.stringify(activePath)}, "active");
for (;;) {
try { await access(${JSON.stringify(releasePath)}); break; }
catch { await new Promise((resolve) => setTimeout(resolve, 10)); }
}
}
answerModeled(match[1], line.slice(match[1].length));
const [, token, command] = match;
if (command.startsWith("-interpreter-exec console "))
appendFileSync(tracePath, (holding ? "overlap " : "") + command + "\\n");
const releasePath = gates.get(command);
if (releasePath !== undefined) {
holding = true;
writeFileSync(activePath, command);
const release = setInterval(() => {
if (!existsSync(releasePath)) return;
clearInterval(release);
holding = false;
answerModeled(token, command);
}, 10);
} else answerModeled(token, command);
}
});
`,
);
await chmod(provider, 0o700);
return { provider, activePath, releasePaths, unmodeledPath, tracePath };
};
it("serializes live commands and keeps cancelled placeholders within the GDB queue limit", async () => {
directory = await mkdtemp(join(tmpdir(), "rea-gdb-command-queue-limit-"));
const { provider, activePath, releasePaths, unmodeledPath, tracePath } =
await makeQueueProvider(directory);
const manager = new GdbSessionManager({
spawnProcess: fixtureOwner.spawnProcess,
platform: "linux",
environment: { REA_GDB_COMMAND: provider },
});
managers.push(manager);
const started = await manager.start();
expect(started.ok).toBe(true);
if (!started.ok) return;
const active = manager.execute(started.value.session_id, "hold");
let activeSeen = false;
for (let attempt = 0; attempt < 100 && !activeSeen; attempt += 1) {
try {
await readFile(activePath, "utf8");
activeSeen = true;
} catch {
await new Promise((resolve) => setTimeout(resolve, 10));
}
}
expect(activeSeen).toBe(true);
const controllers = Array.from({ length: 15 }, () => new AbortController());
const queued = controllers.map((controller, index) =>
manager.execute(
started.value.session_id,
`queued-${index}`,
controller.signal,
),
);
const safetyRelease = setTimeout(() => {
void writeFile(releasePath, "safety release");
}, 250);
const overflow = await manager.execute(started.value.session_id, "overflow");
clearTimeout(safetyRelease);
expect(overflow).toMatchObject({
ok: false,
error: {
_tag: "AnalysisResourceConstraintError",
operation: "gdb_console",
resource: "transport",
reportedLimits: {
max_pending_commands: 16,
active_or_queued_commands: 16,
},
},
});
for (const controller of controllers) controller.abort();
const cancelled = await Promise.all(queued);
expect(cancelled).toHaveLength(15);
expect(
cancelled.every(
(result) => !result.ok && result.error._tag === "AnalysisCancelledError",
),
).toBe(true);
const cancellationSafetyRelease = setTimeout(() => {
void writeFile(releasePath, "safety release after cancellation");
}, 250);
try {
await expect(
manager.execute(
started.value.session_id,
"cannot-bypass-cancelled-placeholders",
const {
value: { session_id: sessionId },
} = await expectGdbSessionStarted(manager);
const active = manager.execute(sessionId, "hold");
const pending: Promise<unknown>[] = [active];
const contend = async (command: string) => {
const controller = new AbortController();
const response = manager.execute(sessionId, command, controller.signal);
pending.push(response);
// If the request queued instead of refusing at capacity, cancel it
// without releasing the active command.
await new Promise<void>((resolve) => setImmediate(resolve));
controller.abort();
return response;
};
const awaitHeld = (command: string, operation: Promise<unknown>) =>
awaitAcknowledgement(
waitFor(async () =>
expect(await readFile(activePath, "utf8")).toBe(consoleLine(command)),
),
).resolves.toMatchObject({
operation,
`held GDB command ${command}`,
);
const trace = async () =>
(await readFile(tracePath, "utf8")).trimEnd().split("\n");
const expectedTrace = [
"hold",
"first-follower",
"second-follower",
"after-drain",
].map(consoleLine);
try {
await awaitHeld("hold", active);
const controllers = Array.from({ length: 13 }, () => new AbortController());
const queued = controllers.map((controller, index) =>
manager.execute(sessionId, `queued-${index}`, controller.signal),
);
const first = manager.execute(sessionId, "first-follower");
const second = manager.execute(sessionId, "second-follower");
pending.push(...queued, first, second);
const constraint = {
ok: false,
error: {
_tag: "AnalysisResourceConstraintError",
reportedLimits: { active_or_queued_commands: 16 },
operation: "gdb_console",
resource: "transport",
reportedLimits: {
max_pending_commands: 16,
active_or_queued_commands: 16,
},
},
};
expect(await contend("overflow")).toMatchObject(constraint);
const reason = { source: "cancelled while queued" };
for (const controller of controllers) controller.abort(reason);
expect(await Promise.all(queued)).toEqual(
Array.from({ length: 13 }, () =>
expect.objectContaining({
ok: false,
error: expect.objectContaining({
_tag: "AnalysisCancelledError",
cause: reason,
}),
}),
),
);
expect(await contend("overflow-with-cancelled-placeholders")).toMatchObject(
constraint,
);
await writeFile(releasePaths.hold, "");
expect((await active).ok).toBe(true);
await awaitHeld("first-follower", first);
expect(await trace()).toEqual(expectedTrace.slice(0, 2));
await writeFile(releasePaths.follower, "");
expect(
(await Promise.all([first, second])).map((result) => result.ok),
).toEqual([true, true]);
expect(await manager.execute(sessionId, "after-drain")).toMatchObject({
ok: true,
value: { result: { completion_status: "complete" } },
});
expect(await trace()).toEqual(expectedTrace);
expect(await manager.close(sessionId)).toMatchObject({
ok: true,
value: { target_state: "none_observed" },
});
expect(manager.status(sessionId)).toBeUndefined();
await expectNoUnmodeledGdbCommands(unmodeledPath);
} finally {
clearTimeout(cancellationSafetyRelease);
try {
await Promise.all(
Object.values(releasePaths).map((path) => writeFile(path, "")),
);
} finally {
await Promise.allSettled(pending);
}
}
await writeFile(releasePath, "release");
expect((await active).ok).toBe(true);
const admittedAfterDrain = await manager.execute(
started.value.session_id,
"admitted-after-drain",
);
expect(admittedAfterDrain.ok).toBe(true);
await expect(
manager.execute(started.value.session_id, "after-drain"),
).resolves.toMatchObject({
ok: true,
value: { result: { completion_status: "complete" } },
});
await expectNoUnmodeledGdbCommands(unmodeledPath);
});
@@ -912,38 +912,6 @@ it("rejects a duplicate GDB close queued behind a session close", async () => {
expect(stopCalls).toBe(1);
});
it("enforces the per-session queue capacity while a GDB command is active", async () => {
let heldToken = "";
const fake = createFakeSpawner((child, token, command) => {
if (command.includes("held-for-capacity")) heldToken = token;
else if (command.includes("list-thread-groups"))
child.stdout.write(`${token}^done,groups=[]\n`);
else child.stdout.write(`${token}^done\n`);
});
const manager = managerWithFakeSpawner(fake.spawnProcess);
const started = await expectGdbSessionStarted(manager);
const held = manager.execute(started.value.session_id, "held-for-capacity");
await expect.poll(() => heldToken).not.toBe("");
const queued = Array.from({ length: 15 }, (_, index) =>
manager.execute(started.value.session_id, `queued-${index}`),
);
const overCapacity = await manager.execute(
started.value.session_id,
"over-capacity",
);
expect(overCapacity).toMatchObject({
ok: false,
error: { _tag: "AnalysisResourceConstraintError" },
});
fake.children[0]?.stdout.write(`${heldToken}^done\n`);
expect((await held).ok).toBe(true);
expect((await Promise.all(queued)).every((result) => result.ok)).toBe(true);
expect((await manager.close(started.value.session_id)).ok).toBe(true);
expect(manager.status(started.value.session_id)).toBeUndefined();
});
it("reports invalid UTF-8 in retained GDB stderr status", async () => {
const fake = createFakeSpawner();
const manager = managerWithFakeSpawner(fake.spawnProcess);
@@ -705,69 +705,6 @@ setTimeout(() => process.exit(0), 250);
},
);
it.skipIf(process.platform === "win32")(
"keeps queued GDB commands serialized when a waiting request is cancelled",
async () => {
directory = await mkdtemp(join(tmpdir(), "rea-gdb-queue-cancel-"));
const provider = join(directory, "fake-gdb-queue-cancel.mjs");
const overlapPath = join(directory, "overlap");
await writeFile(
provider,
`#!${process.execPath}
${fakeGdbStartupPreamble}
${fakeGdbModeledReplies(unmodeledPath(), { ...FAKE_GDB_SESSION_REPLIES, '-interpreter-exec console "third"': "^done" })}
process.stdout.write("(gdb)\\n");
let input = "";
let firstActive = false;
process.stdin.setEncoding("utf8");
process.stdin.on("data", (chunk) => {
input += chunk;
for (;;) {
const boundary = input.indexOf("\\n"); if (boundary < 0) break;
const line = input.slice(0, boundary); input = input.slice(boundary + 1);
const match = /^(\\d+)/.exec(line); if (!match) continue;
if (line.slice(match[1].length) === "-gdb-set auto-load off") process.stdout.write(match[1] + "^done\\n");
else if (line.slice(match[1].length) === '-interpreter-exec console "first"') {
firstActive = true;
setTimeout(() => { firstActive = false; process.stdout.write(match[1] + "^done\\n"); }, 100);
} else {
if (line.slice(match[1].length) === '-interpreter-exec console "third"' && firstActive) import("node:fs/promises").then((fs) => fs.writeFile(${JSON.stringify(overlapPath)}, "overlap"));
answerModeled(match[1], line.slice(match[1].length));
}
}
});
`,
);
await chmod(provider, 0o700);
const manager = createGdbManagerForInjectedPlatform({
environment: { REA_GDB_COMMAND: provider },
});
managers.push(manager);
const started = await manager.start();
expect(started.ok).toBe(true);
if (!started.ok) return;
const first = manager.execute(started.value.session_id, "first");
const controller = new AbortController();
const second = manager.execute(
started.value.session_id,
"second",
controller.signal,
);
await new Promise((resolveDelay) => setTimeout(resolveDelay, 20));
const reason = { source: "cancelled while queued" };
controller.abort(reason);
const third = manager.execute(started.value.session_id, "third");
const cancelled = await second;
expect(cancelled).toMatchObject({
ok: false,
error: { _tag: "AnalysisCancelledError", cause: reason },
});
expect((await first).ok).toBe(true);
expect((await third).ok).toBe(true);
await expect(readFile(overlapPath, "utf8")).rejects.toThrow();
},
);
it.skipIf(process.platform === "win32")(
"preserves active GDB exit Evidence beyond the recent MI history cap",
async () => {