Skip to content

Commit 4e4328e

Browse files
fix(server): boot service log is emptied in place once it passes 10 MiB
systemd and launchd append the background service's output to boot-service.log and nothing ever limited it. The launcher now reads the log path from the installed unit or plist (or its Windows --log-file), and at startup and every ten minutes copies an oversized log to boot-service.log.1 and truncates it in place, since the manager keeps it open. Fixes #24755 AI-assisted; verified with the tests and tools listed above.
1 parent 7c25859 commit 4e4328e

3 files changed

Lines changed: 63 additions & 1 deletion

File tree

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
SERVICE_LAUNCHER_PROTOCOL,
3030
SERVICE_RESTART_PENDING_FILE,
3131
serviceStateHasPendingUpdate,
32+
serviceUnitLogPathOf,
3233
} from "./serviceProtocol.ts";
3334

3435
const leaseObserver = vi.hoisted(() => ({ contended: (_destination: string) => {} }));
@@ -55,7 +56,7 @@ const windowsTaskOptions = {
5556
conhostPath: "C:\\Windows\\System32\\conhost.exe",
5657
};
5758

58-
it("reads the served T3 home back out of a rendered unit, plist, or task", () => {
59+
it("reads the served T3 home and log back out of a rendered unit, plist, or task", () => {
5960
const plan = (baseDir: string) => ({
6061
program: [`${baseDir}/runtime/versions/1.2.3/t3`, "__service-launcher"],
6162
baseDir,
@@ -71,6 +72,17 @@ it("reads the served T3 home back out of a rendered unit, plist, or task", () =>
7172
BootService.renderBootServiceUnit(plan("/home/theo/T3 Data/100%")),
7273
),
7374
).toBe("/home/theo/T3 Data/100%");
75+
expect(
76+
serviceUnitLogPathOf(BootService.renderBootServiceUnit(plan("/home/theo/T3 Data/100%"))),
77+
).toBe("/home/theo/T3 Data/100%/userdata/logs/boot-service.log");
78+
expect(
79+
serviceUnitLogPathOf(
80+
BootService.renderBootServicePlist(plan("/Users/theo/a&b"), {
81+
homeDir: "/Users/theo",
82+
environmentPath: "/usr/bin",
83+
}),
84+
),
85+
).toBe("/Users/theo/a&b/userdata/logs/boot-service.log");
7486
expect(
7587
BootService.bootServiceBaseDirOf(
7688
BootService.renderBootServicePlist(plan("/Users/theo/a&b"), {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ export function serviceUnitBaseDirOf(contents: string): string | undefined {
6363
return task === undefined ? undefined : unquoteWindowsArgument(task);
6464
}
6565

66+
/** The file systemd or launchd appends the service's output to. */
67+
export function serviceUnitLogPathOf(contents: string): string | undefined {
68+
const systemd = /^StandardOutput=append:(.*)$/m.exec(contents)?.[1];
69+
if (systemd !== undefined) return unquoteSystemdValue(systemd.trim());
70+
const plist = /<key>StandardOutPath<\/key>\s*<string>([^<]*)<\/string>/.exec(contents)?.[1];
71+
return plist === undefined ? undefined : unescapeXmlText(plist);
72+
}
73+
6674
/** The executable an installed unit, plist, or task starts: the launcher's runtime. */
6775
export function serviceUnitProgramOf(contents: string): string | undefined {
6876
const systemd = /^ExecStart=("(?:\\.|[^"\\])*"|\S+)/m.exec(contents)?.[1];

‎apps/server/src/serviceLauncher.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
parseServiceState,
2727
serviceTaskIsDisabled,
2828
serviceUnitBaseDirOf,
29+
serviceUnitLogPathOf,
2930
SERVICE_ENV_FILE,
3031
SERVICE_LAUNCHER_LOCK_FILE,
3132
SERVICE_LAUNCHER_CONTEXT_ENV,
@@ -186,6 +187,37 @@ function repointServiceUnit(contents: string, version: string): string {
186187
return contents;
187188
}
188189

190+
const SERVICE_LOG_MAX_BYTES = 10 * 1024 * 1024;
191+
const SERVICE_LOG_CHECK_INTERVAL_MS = 10 * 60_000;
192+
193+
/**
194+
* The service manager holds the log open for appending, so it is copied aside and emptied in
195+
* place rather than renamed. The copy is best effort: on a full disk the truncate frees space.
196+
*/
197+
async function boundServiceLog(logPath: string): Promise<void> {
198+
const size = await NodeFSP.stat(logPath).then(
199+
(stats) => stats.size,
200+
() => 0,
201+
);
202+
if (size <= SERVICE_LOG_MAX_BYTES) return;
203+
const archivePath = `${logPath}.1`;
204+
const pendingArchivePath = `${archivePath}.tmp`;
205+
await NodeFSP.copyFile(logPath, pendingArchivePath)
206+
.then(() => NodeFSP.rename(pendingArchivePath, archivePath))
207+
.catch(() => NodeFSP.rm(pendingArchivePath, { force: true }).catch(() => undefined));
208+
await NodeFSP.truncate(logPath, 0).catch(() => undefined);
209+
}
210+
211+
async function serviceLogPath(
212+
options: LauncherOptions,
213+
serviceUnit: ServiceUnit | undefined,
214+
): Promise<string | undefined> {
215+
if (options.logFile !== undefined) return options.logFile;
216+
if (serviceUnit === undefined) return undefined;
217+
const contents = await NodeFSP.readFile(serviceUnit.path, "utf8").catch(() => undefined);
218+
return contents === undefined ? undefined : serviceUnitLogPathOf(contents);
219+
}
220+
189221
const DB_FILE_SUFFIXES = ["", "-wal", "-shm"] as const;
190222
const RESTORE_MARKER = ".restore-pending";
191223

@@ -1265,6 +1297,7 @@ export async function main(options: LauncherOptions = {}): Promise<void> {
12651297
options.logFile === undefined ? undefined : NodeFS.openSync(options.logFile, "a", 0o600);
12661298
// oxlint-disable-next-line t3code/no-global-process-runtime -- Standalone launcher has no Effect runtime.
12671299
const windows = process.platform === "win32";
1300+
let logTimer: NodeJS.Timeout | undefined;
12681301
try {
12691302
// What the service manager started the launcher with. It locates the unit; service.env
12701303
// changes only what the server gets.
@@ -1285,6 +1318,14 @@ export async function main(options: LauncherOptions = {}): Promise<void> {
12851318
}
12861319
}
12871320
const serviceUnit = serviceUnitFromEnvironment(managerEnvironment);
1321+
const logPath = await serviceLogPath(options, serviceUnit);
1322+
if (logPath !== undefined) {
1323+
await boundServiceLog(logPath);
1324+
logTimer = setInterval(
1325+
() => void boundServiceLog(logPath),
1326+
SERVICE_LOG_CHECK_INTERVAL_MS,
1327+
).unref();
1328+
}
12881329
const launcher = new Launcher(baseDir, state, {
12891330
environment,
12901331
...(serviceUnit === undefined ? {} : { serviceUnit }),
@@ -1325,6 +1366,7 @@ export async function main(options: LauncherOptions = {}): Promise<void> {
13251366
}
13261367
throw cause;
13271368
} finally {
1369+
clearInterval(logTimer);
13281370
if (output !== undefined) NodeFS.closeSync(output);
13291371
}
13301372
}

0 commit comments

Comments
 (0)