fix(system): 编译产物读 /proc、/sys 走 cat 兜底,运行状态资源监控与沙箱内存看门狗恢复
Deno 2 不带 --allow-all 时读 /proc、/sys 一律 NotCapable,部署环境资源监控一直无数据。 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Cxwgnn2pbzQ7rXv3rq7fei
This commit is contained in:
1 parent
708e99746c
commit
9268a4d920
9 files changed
+84
-19
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { availableParallelism, cpus } from "node:os";
|
||||
import process from "node:process";
|
||||
import type { ResourceSample, ResourceScope, SystemResources } from "@umt/contracts";
|
||||
import { readProcText } from "../../proc-read.ts";
|
||||
|
||||
// 资源监控(界面第二轮 V 线,参考 MP 仪表盘 CPU / 内存 / 网络组件):api 进程每 5 秒读一次 /proc 与 cgroup v2,
|
||||
// 相邻两次读数的差算速率,采样只放内存(环形,保留 6 小时)。读不到的文件按 null 降级,不报错。
|
||||
@@ -14,13 +15,8 @@ const MAX_POINTS = 360;
|
||||
|
||||
export type ReadText = (path: string) => Promise<string | null>;
|
||||
|
||||
export const defaultReadText: ReadText = async (path) => {
|
||||
try {
|
||||
return await Deno.readTextFile(path);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
/** /proc 与 /sys 需特权读,编译产物走 cat 兜底(见 proc-read.ts) */
|
||||
export const defaultReadText: ReadText = readProcText;
|
||||
|
||||
// ── 解析(纯函数) ──
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
// 读 /proc 与 /sys 的伪文件。
|
||||
// Deno 2 把这两处当特权路径:不带 --allow-all 时 Deno.readTextFile 一律 NotCapable(--allow-read 及按路径授权都不行)。
|
||||
// 开发 / 测试用 -A 能直接读;编译产物按最小权限运行,只能借 `cat` 子进程读(编译与 start 任务的 --allow-run 已含 cat)。
|
||||
// 第一次遇到 NotCapable 后记住,后续直接走 cat。
|
||||
|
||||
let viaCat = false;
|
||||
|
||||
async function catText(path: string): Promise<string | null> {
|
||||
try {
|
||||
const o = await new Deno.Command("cat", { args: [path], stdout: "piped", stderr: "null" }).output();
|
||||
return o.success ? new TextDecoder().decode(o.stdout) : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** 读不到(不存在、无权限、cat 不可用)返回 null */
|
||||
export async function readProcText(path: string): Promise<string | null> {
|
||||
if (viaCat) return await catText(path);
|
||||
try {
|
||||
return await Deno.readTextFile(path);
|
||||
} catch (e) {
|
||||
if (e instanceof Deno.errors.NotCapable) {
|
||||
viaCat = true;
|
||||
return await catText(path);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
// 起不来(权限 / 运行时不支持)时退回进程内执行并记日志:仍有超时,但卡死的同步代码无法收回(只在本机调试时出现)。
|
||||
import { basename } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { readProcText } from "../proc-read.ts";
|
||||
import { prepareJobs, SANDBOX_JOBS, type SandboxInput, type SandboxJobName, type SandboxOutput } from "./jobs.ts";
|
||||
|
||||
export type SandboxErrorKind = "timeout" | "memory" | "crash" | "job" | "spawn";
|
||||
@@ -52,13 +53,9 @@ function command(memoryMb: number): { cmd: string; args: string[] } {
|
||||
|
||||
/** 子进程实际内存(KB);读不到返回 null */
|
||||
async function rssKb(pid: number): Promise<number | null> {
|
||||
try {
|
||||
const s = await Deno.readTextFile(`/proc/${pid}/status`);
|
||||
const m = /VmRSS:\s+(\d+)\s+kB/.exec(s);
|
||||
return m ? Number(m[1]) : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
const s = await readProcText(`/proc/${pid}/status`);
|
||||
const m = s ? /VmRSS:\s+(\d+)\s+kB/.exec(s) : null;
|
||||
return m ? Number(m[1]) : null;
|
||||
}
|
||||
|
||||
export async function runSandbox<J extends SandboxJobName>(
|
||||
|
||||
Reference in new issue
Block a user