Skip to content

Commit 32ecd71

Browse files
fix(server): t3 service protect can ask for your sudo password (pingdotgg#27465)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 3cbc982 commit 32ecd71

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

‎apps/server/src/cli/service.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,14 @@ const serviceProtectCommand = Command.make("protect", {
314314
);
315315
}
316316

317+
yield* Console.log(
318+
[
319+
"Keeps T3 Code running when agents fill this machine's memory:",
320+
" - reserves memory for the T3 Code server, so the kernel takes memory from agents first",
321+
" - makes the out-of-memory killer pick agents before the server",
322+
"It adds systemd drop-in files and changes nothing else. Undo with `t3 service protect --undo`.",
323+
].join("\n"),
324+
);
317325
const names = remote.map((unit) => unit.replace(/\.service$/, "")).join(" and ");
318326
const protectRemote =
319327
remote.length > 0 &&
@@ -328,7 +336,7 @@ const serviceProtectCommand = Command.make("protect", {
328336
});
329337
yield* Console.log(
330338
[
331-
"As root, through sudo:",
339+
"Drop-ins it writes, as root through sudo:",
332340
...formatDropIns(plan.system),
333341
"As you:",
334342
...formatDropIns([plan.managerDefaults, ...plan.user]),

‎apps/server/src/cloud/serviceProtection.test.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as Effect from "effect/Effect";
1111
import * as FileSystem from "effect/FileSystem";
1212
import * as Sink from "effect/Sink";
1313
import * as Stream from "effect/Stream";
14+
import type * as ChildProcess from "effect/process/ChildProcess";
1415
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
1516

1617
import * as ProcessRunner from "../processRunner.ts";
@@ -122,13 +123,13 @@ it.each([
122123
expect(ServiceProtection.loadedRemoteAccessUnits(shown)).toEqual(expected);
123124
});
124125

125-
const fakeSudo = (exitCode: number, onRun: () => void) =>
126+
const fakeSudo = (exitCode: number, onRun: (command: ChildProcess.StandardCommand) => void) =>
126127
ChildProcessSpawner.make((command) =>
127128
Effect.gen(function* () {
128129
if (command._tag !== "StandardCommand" || command.command !== "sudo") {
129130
return yield* Effect.die("Unexpected command");
130131
}
131-
onRun();
132+
onRun(command);
132133
return ChildProcessSpawner.makeHandle({
133134
pid: ChildProcessSpawner.ProcessId(123),
134135
stdout: Stream.empty,
@@ -180,19 +181,21 @@ it.effect("lowers no score before the manager default is in place, and undoes in
180181
writesManagerDropIn = false,
181182
) => {
182183
let atSudo: ReturnType<typeof present> | undefined;
184+
let sudoOptions: ChildProcess.CommandOptions | undefined;
183185
return effect.pipe(
184186
Effect.provideService(
185187
ChildProcessSpawner.ChildProcessSpawner,
186-
fakeSudo(exitCode, () => {
188+
fakeSudo(exitCode, (command) => {
187189
atSudo = present();
190+
sudoOptions = command.options;
188191
if (writesManagerDropIn) {
189192
NodeFS.mkdirSync(NodePath.dirname(managerDropIn), { recursive: true });
190193
NodeFS.writeFileSync(managerDropIn, "[Service]\nOOMScoreAdjust=-900\n");
191194
}
192195
}),
193196
),
194197
Effect.exit,
195-
Effect.map((exit) => ({ exit, atSudo })),
198+
Effect.map((exit) => ({ exit, atSudo, sudoOptions })),
196199
);
197200
};
198201
const refused = yield* withSudo(ServiceProtection.applyProtection(plan), 1);
@@ -209,6 +212,8 @@ it.effect("lowers no score before the manager default is in place, and undoes in
209212

210213
const applied = yield* withSudo(ServiceProtection.applyProtection(plan), 0);
211214
expect(applied.atSudo).toEqual({ managerDefaults: true, user: 0 });
215+
// sudo needs the controlling terminal to ask for a password.
216+
expect(applied.sudoOptions).toMatchObject({ detached: false, stdin: "inherit" });
212217
expect(present()).toEqual({ managerDefaults: true, user: plan.user.length });
213218

214219
const removed = yield* withSudo(ServiceProtection.removeProtection(plan), 0);

‎apps/server/src/cloud/serviceProtection.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,10 @@ const runPrivileged = Effect.fn("ServiceProtection.runPrivileged")(function* (sc
240240
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
241241
const exitCode = yield* spawner
242242
.exitCode(
243+
// Effect detaches children on POSIX by default; sudo then has no controlling
244+
// terminal and cannot ask for a password.
243245
ChildProcess.make("sudo", ["/bin/sh", "-c", script], {
246+
detached: false,
244247
stdin: "inherit",
245248
stdout: "inherit",
246249
stderr: "inherit",

0 commit comments

Comments
 (0)