@devries-cameron20 / t3code Mirror · read-only / commits / eeae88de5c
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 11:00 UTC

fix(server): service protect and status say when the kernel has no memory controller (#37410)

Julius Marminge committed Nov 15, 2026 at 16:06 UTC eeae88de5c68b2d3c744ad5e7e4964e118856d3f
3 files changed +64 -5
apps/server/src/cli/service.ts
+15 -4
@@ -159,14 +159,19 @@ export function formatServiceChange(
159 return `Service ${result.previouslyInstalled ? "updated" : "installed"} · t3@${packageJson.version}\nLog: ${result.plan.logPath}`;
160 }
161
162 +const MEMORY_CONTROLLER_DISABLED =
163 + "the kernel's memory cgroup controller is disabled. Add `cgroup_enable=memory` to the kernel command line and reboot.";
164 +
165 function formatProtection(
166 protection: ServiceProtection.ServiceProtectionState | undefined,
167 ): ReadonlyArray<string> {
168 if (protection === undefined) return [];
169 return [
167 - protection.unprotected === undefined
168 - ? ` Memory protection: on${protection.setByProtect ? " (t3 service protect)" : ""}`
169 - : ` Memory protection: off, ${protection.unprotected} grants none. Run \`t3 service protect\`.`,
170 + !protection.memoryControllerAvailable
171 + ? ` Memory protection: unavailable, ${MEMORY_CONTROLLER_DISABLED}`
172 + : protection.unprotected === undefined
173 + ? ` Memory protection: on${protection.setByProtect ? " (t3 service protect)" : ""}`
174 + : ` Memory protection: off, ${protection.unprotected} grants none. Run \`t3 service protect\`.`,
175 ...(protection.oomScoreAdj === undefined ? [] : [` OOM score: ${protection.oomScoreAdj}`]),
176 ];
177 }
@@ -471,10 +476,16 @@ const serviceProtectCommand = Command.make("protect", {
476 ...formatDropIns([plan.managerDefaults, ...plan.user]),
477 ].join("\n"),
478 );
479 + const memoryController = yield* ServiceProtection.memoryControllerAvailable;
480 + if (!memoryController) {
481 + yield* Console.log(`Only the OOM scores can apply: ${MEMORY_CONTROLLER_DISABLED}`);
482 + }
483 if (!(yield* confirm(flags.yes, "Apply these settings?"))) return;
484 yield* ServiceProtection.applyProtection(plan);
485 yield* Console.log(
477 - "Protected T3 Code. Memory protection applies now; the server takes its OOM score after `t3 service restart`. Undo with `t3 service protect --undo`.",
486 + memoryController
487 + ? "Protected T3 Code. Memory protection applies now; the server takes its OOM score after `t3 service restart`. Undo with `t3 service protect --undo`."
488 + : "Protected T3 Code's OOM score; the server takes it after `t3 service restart`. Memory protection applies once the kernel's memory controller is enabled. Undo with `t3 service protect --undo`.",
489 );
490 }).pipe(Effect.provide(ProcessRunner.layer)),
491 );
apps/server/src/cloud/serviceProtection.test.ts
+29
@@ -268,3 +268,32 @@ it.effect("counts memory.min as protection and names the first cgroup that grant
268 Effect.provide(NodeServices.layer),
269 );
270 });
271 +
272 +it.effect(
273 + "tells a kernel without the memory controller apart from a cgroup that grants none",
274 + () => {
275 + const root = temporaryDirectory();
276 + NodeFS.mkdirSync(NodePath.join(root, "user.slice"), { recursive: true });
277 + NodeFS.writeFileSync(NodePath.join(root, "cgroup.controllers"), "cpuset cpu io pids\n");
278 + return Effect.gen(function* () {
279 + const fs = yield* FileSystem.FileSystem;
280 + const state = yield* ServiceProtection.readProtection("/home/theo/.config/systemd/user").pipe(
281 + Effect.provideService(FileSystem.FileSystem, {
282 + ...fs,
283 + readFileString: (path, encoding) =>
284 + fs.readFileString(path.replace("/sys/fs/cgroup", root), encoding),
285 + }),
286 + );
287 + expect(state).toMatchObject({ memoryControllerAvailable: false });
288 + }).pipe(
289 + Effect.ensuring(Effect.sync(() => NodeFS.rmSync(root, { recursive: true, force: true }))),
290 + Effect.provideService(
291 + ProcessRunner.ProcessRunner,
292 + runner(
293 + "ControlGroup=/user.slice/user-1000.slice/user@1000.service/app.slice/t3code.service\nMainPID=0\n",
294 + ),
295 + ),
296 + Effect.provide(NodeServices.layer),
297 + );
298 + },
299 +);
apps/server/src/cloud/serviceProtection.ts
+20 -1
@@ -299,7 +299,23 @@ export const installedProtection = Effect.fn("ServiceProtection.installed")(func
299 : { ...plan, system, user };
300 });
301
302 +/**
303 + * False when the kernel lacks the memory cgroup controller (Raspberry Pi OS
304 + * disables it by default), so only OOM scores can apply. True when unknown.
305 + */
306 +export const memoryControllerAvailable = Effect.gen(function* () {
307 + const fs = yield* FileSystem.FileSystem;
308 + const controllers = yield* fs
309 + .readFileString("/sys/fs/cgroup/cgroup.controllers")
310 + .pipe(Effect.option);
311 + return Option.match(controllers, {
312 + onNone: () => true,
313 + onSome: (text) => text.split(/\s+/).includes("memory"),
314 + });
315 +}).pipe(Effect.withSpan("ServiceProtection.memoryControllerAvailable"));
316 +
317 export interface ServiceProtectionState {
318 + readonly memoryControllerAvailable: boolean;
319 /** The first cgroup from the top that grants the server no memory protection. */
320 readonly unprotected: string | undefined;
321 readonly oomScoreAdj: number | undefined;
@@ -325,9 +341,11 @@ export const readProtection = Effect.fn("ServiceProtection.read")(function* (use
341 Effect.map((text) => text.trim()),
342 Effect.orElseSucceed(() => ""),
343 );
344 + const memoryController = yield* memoryControllerAvailable;
345 let unprotected: string | undefined;
346 const grants = (value: string) => value !== "" && value !== "0";
330 - for (let depth = 1; depth <= controlGroup.length && unprotected === undefined; depth++) {
347 + const depths = memoryController ? controlGroup.length : 0;
348 + for (let depth = 1; depth <= depths && unprotected === undefined; depth++) {
349 const directory = path.join("/sys/fs/cgroup", ...controlGroup.slice(0, depth));
350 const low = yield* read(path.join(directory, "memory.low"));
351 const min = yield* read(path.join(directory, "memory.min"));
@@ -336,6 +354,7 @@ export const readProtection = Effect.fn("ServiceProtection.read")(function* (use
354 const pid = Number(values.get("MainPID") ?? 0);
355 const score = pid > 0 ? yield* read(`/proc/${pid}/oom_score_adj`) : "";
356 return {
357 + memoryControllerAvailable: memoryController,
358 unprotected,
359 oomScoreAdj: score === "" ? undefined : Number(score),
360 setByProtect: yield* fs