fix(desktop): bound the Chromium log while the shell is running, not only at launch

The startup reclaim caps the log a previous run left behind, but a shell
that stays up for days writing Chromium ERRORs is unbounded until it
restarts — the case @ehz0ah flagged in review.

Chromium owns that descriptor in append mode for the life of the
process, so rotation is the wrong primitive: renaming leaves the writer
on the renamed inode and the cap silently stops applying. Poll the live
size and truncate in place instead; an O_APPEND writer resumes at offset
0, so a noisy process stays bounded at ~cap plus one interval's output.
The timer is unref'd and every tick is best-effort.

Co-authored-by: ehz0ah <ehz0ah@users.noreply.github.com>

Refs #100573
This commit is contained in:
Austin Pickettandehz0ah committed 2026-09-21 10:23:04 -04:00
1 parent 9a21794450
commit 782886f87d
3 files changed
+131 -4

No files matched your search

+62 -1
View File
@@ -1,8 +1,17 @@
import assert from 'node:assert/strict'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { test } from 'vitest'
import { LOG_DISCARD_BYTES, LOG_MAX_BYTES, logBackupPath, planLogRotation } from './log-rotation'
import {
LOG_DISCARD_BYTES,
LOG_MAX_BYTES,
logBackupPath,
planLogRotation,
reclaimActiveLogIfOversized
} from './log-rotation'
// Regression for #100573 follow-up: the Chromium diagnostic log added for that
// issue is opened with APPEND_TO_OLD_LOG_FILE, so it grows across launches the
@@ -33,3 +42,55 @@ test('a boot-loop log past the discard ceiling is reclaimed, not stranded in .1'
assert.ok(ops.every(([op]) => op === 'rm'))
assert.ok(ops.some(([, src]) => src === base))
})
test('a log a live process keeps appending to is reclaimed in place, not renamed', () => {
const truncated: string[] = []
const io = { size: () => LOG_MAX_BYTES, truncate: (f: string) => truncated.push(f) }
// Chromium holds --log-file open in append mode for the life of the shell:
// renaming it would leave the writer on the renamed inode and the cap would
// silently stop applying, so the only reclamation is truncating in place.
assert.equal(reclaimActiveLogIfOversized('/logs/desktop-chromium.log', io), true)
assert.deepEqual(truncated, ['/logs/desktop-chromium.log'])
})
test('an under-cap or absent active log is left alone', () => {
const touched: string[] = []
const truncate = (f: string) => touched.push(f)
assert.equal(
reclaimActiveLogIfOversized('/logs/x.log', { size: () => LOG_MAX_BYTES - 1, truncate }),
false
)
assert.equal(reclaimActiveLogIfOversized('/logs/x.log', { size: () => null, truncate }), false)
assert.deepEqual(touched, [])
})
test('truncation really frees the file, and an append-mode writer restarts at 0', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'hermes-log-bound-'))
const file = path.join(dir, 'desktop-chromium.log')
try {
// Stand in for Chromium: an O_APPEND handle held across the reclaim.
const handle = fs.openSync(file, 'a')
try {
fs.ftruncateSync(handle, LOG_MAX_BYTES + 1) // Grow without writing GBs.
assert.equal(
reclaimActiveLogIfOversized(file, {
size: f => fs.statSync(f).size,
truncate: f => fs.truncateSync(f, 0)
}),
true
)
fs.writeSync(handle, 'FATAL:after\n')
assert.equal(fs.readFileSync(file, 'utf8'), 'FATAL:after\n')
} finally {
fs.closeSync(handle)
}
} finally {
fs.rmSync(dir, { recursive: true, force: true })
}
})
+26
View File
@@ -18,6 +18,32 @@ export const LOG_DISCARD_BYTES = LOG_MAX_BYTES * 4
export const logBackupPath = (base: string, n: number): string => `${base}.${n}`
// A log another PROCESS owns (Chromium's --log-file) cannot be rotated: it
// holds the descriptor open in append mode, so renaming the file just moves
// the growth to the renamed inode and the cap silently stops applying. The
// only reclamation that works from outside is truncating in place — an
// O_APPEND writer resumes at offset 0 — so a long-lived noisy process is
// bounded at ~cap plus one poll interval's output instead of the whole disk.
export const ACTIVE_LOG_POLL_MS = 5 * 60 * 1000
export interface ActiveLogIo {
/** Live size, or null when the file does not exist yet. */
size(file: string): number | null
truncate(file: string): void
}
export function reclaimActiveLogIfOversized(file: string, io: ActiveLogIo): boolean {
const size = io.size(file)
if (size === null || size < LOG_MAX_BYTES) {
return false
}
io.truncate(file)
return true
}
export type LogRotationOp = ['rm', string] | ['mv', string, string]
// Pure planner: ordered fs ops to bound the live log at `base`. [] = nothing.
+43 -3
View File
@@ -269,10 +269,14 @@ import { resolveHudWindowing } from './hud-windowing'
import { createIntroRevealWindowController } from './intro-reveal-window'
import { isAuthWall, resolveLinkTitle } from './link-title-wall'
import { createLinkTitleWindow, guardLinkTitleSession, readLinkTitleWindowTitle } from './link-title-window'
import { enableLinuxCrashDiagnostics, linuxCrashDiagnostics } from './linux-crash-diagnostics'
import {
CHROMIUM_LOG_FILENAME,
enableLinuxCrashDiagnostics,
linuxCrashDiagnostics
} from './linux-crash-diagnostics'
import { notifyLauncherWindowRevealed } from './linux-launcher-ready'
import { createLocalBackendLifecycle, waitForTeardown } from './local-backend-lifecycle'
import { planLogRotation } from './log-rotation'
import { ACTIVE_LOG_POLL_MS, planLogRotation, reclaimActiveLogIfOversized } from './log-rotation'
import { ensureMainWindow } from './main-window-lifecycle'
import {
assertManagedUpdatePreflightClear,
@@ -986,8 +990,11 @@ const DESKTOP_LOG_BUFFER_MAX_CHARS = 64 * 1024
// way, and never let optional diagnostics fail the shell's startup.
const CRASH_DIAGNOSTICS_LOGS_DIR = path.dirname(DESKTOP_LOG_PATH)
const CRASH_DIAGNOSTICS = linuxCrashDiagnostics(CRASH_DIAGNOSTICS_LOGS_DIR)
const CHROMIUM_LOG_PATH = path.join(CRASH_DIAGNOSTICS_LOGS_DIR, CHROMIUM_LOG_FILENAME)
enableLinuxCrashDiagnostics(
linuxCrashDiagnostics(CRASH_DIAGNOSTICS_LOGS_DIR),
CRASH_DIAGNOSTICS,
CRASH_DIAGNOSTICS_LOGS_DIR,
{
ensureLogsDir: dir => fs.mkdirSync(dir, { recursive: true }),
@@ -1831,6 +1838,35 @@ let bootProgressState = {
timestamp: Date.now()
}
// Chromium owns its --log-file for the life of the process, so the startup
// reclaim above cannot bound a shell that stays up for days writing errors.
// Poll and truncate in place; renaming would leave Chromium appending to the
// renamed inode. Unref'd so it never holds the process open.
function startChromiumLogWatcher(file) {
const io = {
size: f => {
try {
return fs.statSync(f).size
} catch {
return null // Not created yet — nothing has been logged.
}
},
truncate: f => fs.truncateSync(f, 0)
}
const timer = setInterval(() => {
try {
if (reclaimActiveLogIfOversized(file, io)) {
rememberLog(`[diagnostics] truncated oversized Chromium log ${file}`)
}
} catch {
// Best-effort — an unbounded log beats a crashed shell.
}
}, ACTIVE_LOG_POLL_MS)
timer.unref?.()
}
function rotateLogIfNeededSync(base) {
let size
@@ -18535,6 +18571,10 @@ app.whenReady().then(() => {
// before the backend start path awaits the same single-flight promise.
void ensureLoginShellPath()
if (CRASH_DIAGNOSTICS) {
startChromiumLogWatcher(CHROMIUM_LOG_PATH)
}
const systemCa = installWindowsSystemCaTrust(tls)
if (systemCa.applied) {