Skip to content

Commit e285b17

Browse files
fix(server): truncate boot-service.log even when its archive copy fails
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 245e27b commit e285b17

2 files changed

Lines changed: 84 additions & 5 deletions

File tree

‎apps/server/src/serviceLauncher.test.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as Path from "effect/Path";
1010
import { vi } from "vite-plus/test";
1111

1212
import {
13+
boundServiceLog,
1314
databaseBackupBlocker,
1415
Launcher,
1516
main,
@@ -28,12 +29,14 @@ import {
2829
} from "./cloud/serviceProtocol.ts";
2930

3031
const spawnedChildren = vi.hoisted((): Array<NodeChildProcess.ChildProcess> => []);
32+
const spawnListeners = vi.hoisted(() => new Set<(child: NodeChildProcess.ChildProcess) => void>());
3133

3234
vi.mock("node:child_process", async (importOriginal) => {
3335
const actual = await importOriginal<typeof NodeChildProcess>();
3436
const spawn = (...args: Parameters<typeof actual.spawn>) => {
3537
const child = actual.spawn(...args);
3638
spawnedChildren.push(child);
39+
for (const listener of spawnListeners) listener(child);
3740
return child;
3841
};
3942
return { ...actual, spawn: spawn as typeof actual.spawn };
@@ -250,6 +253,75 @@ it.layer(NodeServices.layer)("service state persistence", (it) => {
250253
}),
251254
);
252255

256+
it.effect("truncates an oversized boot-service.log when the archive copy fails", () =>
257+
Effect.gen(function* () {
258+
const fs = yield* FileSystem.FileSystem;
259+
const path = yield* Path.Path;
260+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-log-" });
261+
const logPath = path.join(root, "userdata", "logs", "boot-service.log");
262+
yield* fs.makeDirectory(`${logPath}.1`, { recursive: true });
263+
yield* writeSparseFile(logPath, 17 * 1024 * 1024);
264+
265+
yield* Effect.promise(() => boundServiceLog(root));
266+
267+
assert.equal(Number((yield* fs.stat(logPath)).size), 0);
268+
assert.equal((yield* fs.stat(`${logPath}.1`)).type, "Directory");
269+
}),
270+
);
271+
272+
it.effect("keeps bounding boot-service.log while the service runs", () =>
273+
Effect.gen(function* () {
274+
const fs = yield* FileSystem.FileSystem;
275+
const path = yield* Path.Path;
276+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-log-" });
277+
const statePath = path.join(root, "runtime", "service-state.json");
278+
const logPath = path.join(root, "userdata", "logs", "boot-service.log");
279+
const grownSize = 17 * 1024 * 1024;
280+
// The child starts after the startup check, so this growth is only seen by recurring checks.
281+
yield* writeFakeRuntime(
282+
fs,
283+
path,
284+
path.join(root, "runtime", "versions", "1.0.0"),
285+
[
286+
'const fs = require("node:fs");',
287+
`fs.writeFileSync(${JSON.stringify(logPath)}, "");`,
288+
`fs.truncateSync(${JSON.stringify(logPath)}, ${grownSize});`,
289+
'process.send("log-grown");',
290+
"setInterval(() => {}, 1_000);",
291+
"",
292+
].join("\n"),
293+
);
294+
yield* Effect.promise(() =>
295+
writeServiceState(statePath, {
296+
protocol: SERVICE_LAUNCHER_PROTOCOL,
297+
activeVersion: "1.0.0",
298+
}),
299+
);
300+
yield* fs.makeDirectory(path.dirname(logPath), { recursive: true });
301+
vi.useFakeTimers({ toFake: ["setInterval", "clearInterval"] });
302+
yield* Effect.addFinalizer(() => Effect.sync(() => vi.useRealTimers()));
303+
const logGrown = new Promise<void>((resolve) => {
304+
const listener = (child: NodeChildProcess.ChildProcess) => {
305+
spawnListeners.delete(listener);
306+
child.once("message", () => resolve());
307+
};
308+
spawnListeners.add(listener);
309+
});
310+
311+
const launcher = new Launcher(root, yield* Effect.promise(() => readServiceState(statePath)));
312+
const running = launcher.run();
313+
yield* Effect.promise(() => logGrown);
314+
assert.equal(Number((yield* fs.stat(logPath)).size), grownSize);
315+
316+
yield* Effect.promise(() => vi.advanceTimersByTimeAsync(10 * 60_000));
317+
// Stop queues behind the recurring check, so the check has finished once the launcher exits.
318+
yield* Effect.promise(() => launcher.stop("SIGTERM"));
319+
yield* Effect.promise(() => running);
320+
321+
assert.equal(Number((yield* fs.stat(logPath)).size), 0);
322+
}),
323+
);
324+
253325
it.effect("stays idle instead of exiting when another server owns the T3 home", () =>
254326
Effect.gen(function* () {
255327
const fs = yield* FileSystem.FileSystem;

‎apps/server/src/serviceLauncher.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,20 @@ async function pathExists(target: string): Promise<boolean> {
9393
* the copy and the truncate are lost.
9494
*/
9595
export async function boundServiceLog(baseDir: string): Promise<void> {
96-
// The unit and plist render `<home>/userdata/logs`; the service never runs with a dev state dir.
96+
// Installs render `<home>/userdata/logs`; a maintainer install from a dev config renders
97+
// `<home>/dev/logs`, which is left unbounded.
9798
const logPath = NodePath.join(baseDir, "userdata", "logs", SERVICE_LOG_FILE);
99+
const archivePath = `${logPath}.1`;
98100
try {
99101
if ((await NodeFSP.stat(logPath)).size <= SERVICE_LOG_MAX_BYTES) return;
100-
await NodeFSP.copyFile(logPath, `${logPath}.1`);
101-
await NodeFSP.truncate(logPath, 0);
102-
} catch {}
102+
} catch {
103+
return;
104+
}
105+
// The archive is best effort: on a full disk the copy fails, and the truncate is what frees space.
106+
await NodeFSP.copyFile(logPath, archivePath).catch(() =>
107+
NodeFSP.rm(archivePath, { force: true }).catch(() => undefined),
108+
);
109+
await NodeFSP.truncate(logPath, 0).catch(() => undefined);
103110
}
104111

105112
const MANAGED_SERVICE_ENV_NAMES = new Set([
@@ -489,7 +496,7 @@ export class Launcher {
489496
process.once("SIGTERM", onSigterm);
490497
process.once("SIGINT", onSigint);
491498
this.#logTimer = setInterval(
492-
() => void boundServiceLog(this.#baseDir),
499+
() => this.#enqueue(() => boundServiceLog(this.#baseDir)),
493500
SERVICE_LOG_CHECK_INTERVAL_MS,
494501
).unref();
495502
try {

0 commit comments

Comments
 (0)