Files
rea/src/process/LinuxCgroupMemory.test.ts
T
morluto 428e006f66 fix(filesystem): close file handles only through the owned-handle owners (#10578)
* fix(filesystem): close file handles only through the owned-handle owners

Three sites still closed a fs/promises FileHandle in a finally block, where a
failed close replaced the operation's own failure or result: the agentenv
runner head read, the Linux cgroup kernel-file read and the JavaScript
analysis transfer writer. They now use withOwnedFileHandle, which retains the
operation outcome alongside a failed close.

A lint rule, rea/no-raw-file-handle-close, refuses close() on a handle opened
from fs/promises or declared as FileHandle, and fs close/closeSync on a
descriptor, in src outside src/filesystem/Owned*.

* fix(lint): distinguish owned handle class fields

* fix(filesystem): retain diagnostic file cleanup owners

* test(hopper): retry after confirmed application exit

* fix(filesystem): settle doctor peers, sample cgroup outside admission, and bind lint this

Desktop doctor waits for every client inspection before reporting a retained close failure. Ghidra memory cgroup sampling no longer passes the resource scope's admission gate, so a pending owner cannot mask the provider failure; failed closes are still retained for session cleanup. The raw FileHandle close rule resolves this through arrow functions only, treats static blocks as static, and keeps #name distinct from constructor parameter properties.

* fix(ghidra): join in-flight cgroup samples before session cleanup

ArtifactResourceScope.track runs optional work outside the admission gate while cleanup drains still join it, so a cancelled request's final retry cannot finish before a sample retains a failed close.

* fix(filesystem): join work tracked during a cleanup drain and report doctor file cleanup as typed errors

A cleanup retry now repeats until no tracked work is active and every retained owner has had one attempt, so diagnostic work admitted while an earlier owner is releasing is joined. Doctor reports a retained runner or configuration file close as a ProviderCleanupError bound to its lease instead of rethrowing a raw file failure, so the error document keeps the retained file.

* fix(desktop): read the agentenv runner head through the artifact file owner

The runner read retained a failed close under one lease while the reported failure carried another, so a successful cleanup retry left the doctor error reporting an already-closed file. withArtifactFileHandle binds one lease to both.
2026-11-15 18:08:56 +08:00

227 lines
7.9 KiB
TypeScript

import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it, vi } from "vitest";
import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js";
import { readLinuxCgroupMemoryState } from "./LinuxCgroupMemory.js";
import { OwnedFileHandle } from "../filesystem/OwnedFileHandle.js";
import { RegularFileCleanupFailure } from "../filesystem/OwnedRegularFile.js";
it("retains a failed kernel file close for the caller to retry", async () => {
const directory = await createTestTempDirectory("rea-cgroup-retained-");
const path = join(directory, "cgroup");
await writeFile(path, "0::/jobs/rea\n");
const failure = new Error("close denied");
const close = vi
.spyOn(OwnedFileHandle.prototype, "close")
.mockRejectedValueOnce(failure);
let retained: OwnedFileHandle | undefined;
try {
await expect(
readLinuxCgroupMemoryState({
platform: "linux",
procCgroupPath: path,
retainFailedClose: (_path, owner) => {
retained = owner;
},
}),
).resolves.toBeUndefined();
expect(retained).toBeDefined();
await retained?.close();
close.mockRejectedValueOnce(failure);
let unretained: unknown;
try {
await readLinuxCgroupMemoryState({
platform: "linux",
procCgroupPath: path,
});
} catch (cause: unknown) {
unretained = cause;
}
expect(unretained).toBeInstanceOf(RegularFileCleanupFailure);
if (unretained instanceof RegularFileCleanupFailure)
await unretained.owner.close();
} finally {
close.mockRestore();
await retained?.close();
}
});
const events = (oomKill: number) =>
`low 0\nhigh 0\nmax 772\noom 3\noom_kill ${String(oomKill)}\noom_group_kill 0\n`;
const mountInfoRow = (root: string, mountPoint: string, type = "cgroup2") =>
`35 24 0:30 ${root} ${mountPoint} rw,nosuid,nodev,noexec,relatime shared:9 - ${type} cgroup2 rw,nsdelegate`;
/** A cgroup2 mount whose root is `/jobs`, holding this process's group `/jobs/rea`. */
const cgroupTree = async (
files: Readonly<Record<string, string>>,
membership = "0::/jobs/rea\n",
mountRoot = "/jobs",
) => {
const root = await createTestTempDirectory("rea-cgroup-");
const mountPoint = join(root, "cgroup");
await mkdir(join(mountPoint, "rea"), { recursive: true });
for (const [name, text] of Object.entries(files))
await writeFile(join(mountPoint, "rea", name), text);
const procCgroupPath = join(root, "proc-self-cgroup");
await writeFile(procCgroupPath, membership);
const procMountInfoPath = join(root, "proc-self-mountinfo");
await writeFile(
procMountInfoPath,
`${mountInfoRow(mountRoot, mountPoint)}\n`,
);
return { platform: "linux" as const, procCgroupPath, procMountInfoPath };
};
it.each([
{ membership: "12:memory:/jobs/rea\n", mountRoot: "/jobs" },
{ membership: "0::/jobs/rea\n0::/jobs/rea\n", mountRoot: "/jobs" },
{ membership: "0::jobs/rea\n", mountRoot: "jobs" },
{ membership: "0::/jobs/rea/../rea\n", mountRoot: "/jobs" },
])(
"rejects invalid cgroup membership $membership",
async ({ membership, mountRoot }) => {
const options = await cgroupTree(
{ "memory.events": events(1) },
membership,
mountRoot,
);
await expect(readLinuxCgroupMemoryState(options)).resolves.toBeUndefined();
},
);
it("selects the v2 membership among v1 rows", async () => {
const options = await cgroupTree(
{ "memory.events": events(1) },
"12:memory:/docker/abc\n0::/jobs/rea\n",
);
await expect(readLinuxCgroupMemoryState(options)).resolves.toMatchObject({
path: "/jobs/rea",
oomKills: 1,
});
});
it.each([
"oom 3\noom_group_kill 2\n",
"oom_kill 1\noom_kill 2\n",
"oom_kill 9007199254740992\n",
])("rejects a missing, duplicated, or unsafe kill count: %j", async (value) => {
const options = await cgroupTree({ "memory.events": value });
await expect(readLinuxCgroupMemoryState(options)).resolves.toBeUndefined();
});
it("retains an unknown memory limit when the token is malformed", async () => {
const options = await cgroupTree({
"memory.events": events(0),
"memory.max": "3g\n",
});
await expect(readLinuxCgroupMemoryState(options)).resolves.toMatchObject({
memoryMax: null,
});
});
it("selects the closest covering cgroup2 mount and decodes escaped mount paths", async () => {
const root = await createTestTempDirectory("rea-cgroup-mounts-");
const full = join(root, "full");
const subtree = join(root, "cgroup root");
await mkdir(join(full, "jobs", "rea"), { recursive: true });
await mkdir(join(subtree, "rea"), { recursive: true });
await writeFile(join(full, "jobs", "rea", "memory.events"), events(9));
await writeFile(join(subtree, "rea", "memory.events"), events(1));
const procCgroupPath = join(root, "proc-self-cgroup");
const procMountInfoPath = join(root, "proc-self-mountinfo");
const options = {
platform: "linux" as const,
procCgroupPath,
procMountInfoPath,
};
await writeFile(procCgroupPath, "0::/jobs/rea\n");
await writeFile(
procMountInfoPath,
[
mountInfoRow("/", full),
mountInfoRow("/jobs", subtree.replaceAll(" ", "\\040")),
mountInfoRow("/jobs/rea", join(root, "ignored"), "cgroup"),
].join("\n"),
);
await expect(readLinuxCgroupMemoryState(options)).resolves.toMatchObject({
path: "/jobs/rea",
oomKills: 1,
});
await writeFile(join(subtree, "memory.events"), events(2));
await writeFile(procCgroupPath, "0::/jobs\n");
await expect(readLinuxCgroupMemoryState(options)).resolves.toMatchObject({
path: "/jobs",
oomKills: 2,
});
await writeFile(procMountInfoPath, mountInfoRow("/", full));
await writeFile(procCgroupPath, "0::/jobs/rea\n");
await expect(readLinuxCgroupMemoryState(options)).resolves.toMatchObject({
path: "/jobs/rea",
oomKills: 9,
});
await writeFile(join(full, "memory.events"), events(3));
await writeFile(procCgroupPath, "0::/\n");
await expect(readLinuxCgroupMemoryState(options)).resolves.toMatchObject({
path: "/",
oomKills: 3,
});
await writeFile(procMountInfoPath, mountInfoRow("/jobs", subtree));
await writeFile(procCgroupPath, "0::/jobsite/rea\n");
await expect(readLinuxCgroupMemoryState(options)).resolves.toBeUndefined();
await writeFile(procMountInfoPath, "");
await expect(readLinuxCgroupMemoryState(options)).resolves.toBeUndefined();
});
it("reads the process's own group below a subtree cgroup2 mount", async () => {
const options = await cgroupTree({
"memory.events": events(1),
"memory.max": "3221225472\n",
});
await expect(readLinuxCgroupMemoryState(options)).resolves.toEqual({
path: "/jobs/rea",
oomKills: 1,
memoryMax: 3_221_225_472,
});
});
it("keeps an unlimited group apart from an unreadable limit", async () => {
const unlimited = await cgroupTree({
"memory.events": events(0),
"memory.max": "max\n",
});
await expect(readLinuxCgroupMemoryState(unlimited)).resolves.toMatchObject({
memoryMax: "max",
});
const unreadable = await cgroupTree({ "memory.events": events(0) });
await expect(readLinuxCgroupMemoryState(unreadable)).resolves.toEqual({
path: "/jobs/rea",
oomKills: 0,
memoryMax: null,
});
});
it("is unavailable without memory.events, a covering mount, cgroup v2, or Linux", async () => {
const noEvents = await cgroupTree({ "memory.max": "max\n" });
await expect(readLinuxCgroupMemoryState(noEvents)).resolves.toBeUndefined();
const outside = await cgroupTree(
{ "memory.events": events(0) },
"0::/system.slice/rea.service\n",
);
await expect(readLinuxCgroupMemoryState(outside)).resolves.toBeUndefined();
const v1 = await cgroupTree(
{ "memory.events": events(0) },
"4:memory:/jobs/rea\n",
);
await expect(readLinuxCgroupMemoryState(v1)).resolves.toBeUndefined();
const linux = await cgroupTree({ "memory.events": events(0) });
await expect(
readLinuxCgroupMemoryState({ ...linux, platform: "darwin" }),
).resolves.toBeUndefined();
});