Skip to content

The agent that grows with you

fix(desktop): optional Linux crash diagnostics must not be fatal or unbounded

Austin Pickett9a2179445045
diff --git a/apps/desktop/electron/linux-crash-diagnostics.test.ts b/apps/desktop/electron/linux-crash-diagnostics.test.ts
index 68d13b45111..8a31f1d6c94 100644
--- a/apps/desktop/electron/linux-crash-diagnostics.test.ts
+++ b/apps/desktop/electron/linux-crash-diagnostics.test.ts
@@ -3,7 +3,11 @@ import path from 'node:path'
import { test } from 'vitest'
-import { linuxCrashDiagnostics } from './linux-crash-diagnostics'
+import {
+ CHROMIUM_LOG_FILENAME,
+ enableLinuxCrashDiagnostics,
+ linuxCrashDiagnostics
+} from './linux-crash-diagnostics'
// Regression for #100573: the Linux shell died with SIGTRAP at Chromium's
// shared fatal-handler address and no launcher kept the FATAL message. The
@@ -31,3 +35,55 @@ test('other platforms get no Chromium logging switches and no crash reporter', (
assert.equal(linuxCrashDiagnostics('/Users/u/.hermes/logs', 'darwin'), null)
assert.equal(linuxCrashDiagnostics('C:\\Users\\u\\.hermes\\logs', 'win32'), null)
})
+
+test('a logs dir that cannot be created degrades to no logging, never a dead shell', () => {
+ const switches: string[] = []
+ let reporterStarted = false
+
+ // Read-only or invalid HERMES_HOME/logs: mkdir throws before app readiness.
+ enableLinuxCrashDiagnostics(linuxCrashDiagnostics('/read-only/logs', 'linux'), '/read-only/logs', {
+ ensureLogsDir: () => {
+ throw new Error('EROFS: read-only file system')
+ },
+ reclaimChromiumLog: () => assert.fail('must not touch a log dir that does not exist'),
+ appendSwitch: name => switches.push(name),
+ startCrashReporter: () => {
+ reporterStarted = true
+ }
+ })
+
+ // No log-file switch (Chromium could not have opened it anyway), and the
+ // crash reporter — which writes elsewhere — still runs.
+ assert.deepEqual(switches, [])
+ assert.equal(reporterStarted, true)
+})
+
+test('a crash reporter that refuses to start is not fatal either', () => {
+ const switches: string[] = []
+
+ enableLinuxCrashDiagnostics(linuxCrashDiagnostics('/home/u/.hermes/logs', 'linux'), '/home/u/.hermes/logs', {
+ ensureLogsDir: () => {},
+ reclaimChromiumLog: () => {},
+ appendSwitch: name => switches.push(name),
+ startCrashReporter: () => {
+ throw new Error('crashpad handler missing')
+ }
+ })
+
+ assert.ok(switches.includes('log-file'))
+})
+
+test('the Chromium log is bounded before Chromium appends to it', () => {
+ const reclaimed: string[] = []
+
+ enableLinuxCrashDiagnostics(linuxCrashDiagnostics('/home/u/.hermes/logs', 'linux'), '/home/u/.hermes/logs', {
+ ensureLogsDir: () => {},
+ reclaimChromiumLog: file => reclaimed.push(file),
+ appendSwitch: () => {},
+ startCrashReporter: () => {}
+ })
+
+ // Electron opens an explicit --log-file with APPEND_TO_OLD_LOG_FILE, so the
+ // file it is about to append to is exactly the one that must be reclaimed.
+ assert.deepEqual(reclaimed, [path.join('/home/u/.hermes/logs', CHROMIUM_LOG_FILENAME)])
+})
diff --git a/apps/desktop/electron/linux-crash-diagnostics.ts b/apps/desktop/electron/linux-crash-diagnostics.ts
index abd80ee6a6b..66b71be6b92 100644
--- a/apps/desktop/electron/linux-crash-diagnostics.ts
+++ b/apps/desktop/electron/linux-crash-diagnostics.ts
@@ -39,3 +39,61 @@ export function linuxCrashDiagnostics(
crashReporter: { uploadToServer: false, compress: false }
}
}
+
+/** The side effects the plan needs, injected so the failure paths are provable. */
+export interface CrashDiagnosticsHost {
+ /** Create the logs directory. May throw (read-only or invalid HERMES_HOME). */
+ ensureLogsDir(dir: string): void
+ /** Bound the Chromium log before Chromium appends to it (APPEND_TO_OLD_LOG_FILE). */
+ reclaimChromiumLog(file: string): void
+ appendSwitch(name: string, value: string): void
+ startCrashReporter(options: LinuxCrashDiagnostics['crashReporter']): void
+}
+
+// Diagnostics are optional; startup is not. Every step is best-effort, because
+// a read-only or invalid HERMES_HOME/logs must degrade to "no crash log", never
+// to a desktop that dies before app readiness. The existing desktop log path
+// swallows the same failures for the same reason.
+export function enableLinuxCrashDiagnostics(
+ plan: LinuxCrashDiagnostics | null,
+ logsDir: string,
+ host: CrashDiagnosticsHost
+): void {
+ if (!plan) {
+ return
+ }
+
+ let logsDirReady = true
+
+ try {
+ host.ensureLogsDir(logsDir)
+ } catch {
+ // No writable logs dir: Chromium could not open the file anyway. Skip the
+ // logging switches and keep the crash reporter, which writes elsewhere.
+ logsDirReady = false
+ }
+
+ if (logsDirReady) {
+ for (const [name, value] of plan.switches) {
+ if (name === 'log-file') {
+ try {
+ host.reclaimChromiumLog(value)
+ } catch {
+ // Best-effort — an unbounded log beats no app, but try every launch.
+ }
+ }
+
+ try {
+ host.appendSwitch(name, value)
+ } catch {
+ // Ignore: a switch we cannot set only costs us the diagnostic.
+ }
+ }
+ }
+
+ try {
+ host.startCrashReporter(plan.crashReporter)
+ } catch {
+ // Crashpad unavailable (sandbox, missing helper) — not a startup failure.
+ }
+}
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 213cd10586f..845ce08f1e4 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -269,9 +269,10 @@ 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 { linuxCrashDiagnostics } from './linux-crash-diagnostics'
+import { 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 { ensureMainWindow } from './main-window-lifecycle'
import {
assertManagedUpdatePreflightClear,
@@ -973,36 +974,28 @@ const DESKTOP_LOG_BUFFER_MAX_CHARS = 64 * 1024
// (version-skew crash -> backend exits instantly -> renderer keeps hitting
// Retry) appends the full bootstrap transcript every attempt and grows without
// bound — we have seen it reach ~326 GB and exhaust the disk, which then breaks
-// update/install (no room for git/venv/npm temp files).
-//
-// Mirror the Python logs (hermes_logging.py RotatingFileHandler, maxBytes x
-// backupCount): cascade live -> .1 -> .2 -> .3, drop the oldest. Steady-state
-// stays bounded at ~(backupCount + 1) x cap however hard the app loops.
-//
-// Bounding alone never RECLAIMS an already-huge file: a plain rotation just
-// renames the monster to .1 and strands it for a cycle a healthy app may never
-// reach. A multi-GB boot-loop transcript has no diagnostic value, so anything
-// past the discard ceiling is deleted outright — the updated app self-heals a
-// disk a stale build filled, on the next launch.
-const DESKTOP_LOG_MAX_BYTES = 10 * 1024 * 1024
-const DESKTOP_LOG_BACKUP_COUNT = 3
-const DESKTOP_LOG_DISCARD_BYTES = DESKTOP_LOG_MAX_BYTES * 4
-const desktopLogBackupPath = n => `${DESKTOP_LOG_PATH}.${n}`
+// update/install (no room for git/venv/npm temp files). The cap, the cascade
+// and the discard ceiling live in log-rotation.ts, shared with the Chromium
+// log below.
// #100573: keep the FATAL line and a local minidump for the next Linux SIGTRAP.
// Both must be wired before `app` is ready; the log-file switch is inherited by
// every child process, so a zygote or GPU CHECK lands in the same file.
-const CRASH_DIAGNOSTICS = linuxCrashDiagnostics(path.dirname(DESKTOP_LOG_PATH))
-
-if (CRASH_DIAGNOSTICS) {
- fs.mkdirSync(path.dirname(DESKTOP_LOG_PATH), { recursive: true })
-
- for (const [name, value] of CRASH_DIAGNOSTICS.switches) {
- app.commandLine.appendSwitch(name, value)
+// Chromium opens an explicit --log-file with APPEND_TO_OLD_LOG_FILE, so this
+// one accumulates across launches exactly like desktop.log: bound it the same
+// way, and never let optional diagnostics fail the shell's startup.
+const CRASH_DIAGNOSTICS_LOGS_DIR = path.dirname(DESKTOP_LOG_PATH)
+
+enableLinuxCrashDiagnostics(
+ linuxCrashDiagnostics(CRASH_DIAGNOSTICS_LOGS_DIR),
+ CRASH_DIAGNOSTICS_LOGS_DIR,
+ {
+ ensureLogsDir: dir => fs.mkdirSync(dir, { recursive: true }),
+ reclaimChromiumLog: file => rotateLogIfNeededSync(file),
+ appendSwitch: (name, value) => app.commandLine.appendSwitch(name, value),
+ startCrashReporter: options => crashReporter.start(options)
}
-
- crashReporter.start(CRASH_DIAGNOSTICS.crashReporter)
-}
+)
const BOOT_FAKE_MODE = process.env.HERMES_DESKTOP_BOOT_FAKE === '1'
const BOOT_FAKE_ERROR = process.env.HERMES_DESKTOP_BOOT_FAKE_ERROR || ''
@@ -1838,43 +1831,16 @@ let bootProgressState = {
timestamp: Date.now()
}
-// Pure planner: ordered fs ops to bound a live log of `size`. [] = nothing.
-// Each step is ['rm', path] or ['mv', src, dst]; executed best-effort so a
-// missing chain link never aborts the rest.
-function planDesktopLogRotation(size) {
- if (size < DESKTOP_LOG_MAX_BYTES) {
- return []
- }
-
- const backups = n => Array.from({ length: n }, (_, i) => desktopLogBackupPath(i + 1))
-
- // Pathological boot-loop log: reclaim live + every backup outright.
- if (size > DESKTOP_LOG_DISCARD_BYTES) {
- return [DESKTOP_LOG_PATH, ...backups(DESKTOP_LOG_BACKUP_COUNT)].map(p => ['rm', p])
- }
-
- // Cascade: drop oldest, shift each up, live -> .1.
- const ops = [['rm', desktopLogBackupPath(DESKTOP_LOG_BACKUP_COUNT)]]
-
- for (let i = DESKTOP_LOG_BACKUP_COUNT - 1; i >= 1; i--) {
- ops.push(['mv', desktopLogBackupPath(i), desktopLogBackupPath(i + 1)])
- }
-
- ops.push(['mv', DESKTOP_LOG_PATH, desktopLogBackupPath(1)])
-
- return ops
-}
-
-function rotateDesktopLogIfNeededSync() {
+function rotateLogIfNeededSync(base) {
let size
try {
- size = fs.statSync(DESKTOP_LOG_PATH).size
+ size = fs.statSync(base).size
} catch {
return // No live file yet — the append (re)creates it.
}
- for (const [op, src, dst] of planDesktopLogRotation(size)) {
+ for (const [op, src, dst] of planLogRotation(size, base)) {
try {
if (op === 'rm') {
fs.rmSync(src, { force: true })
@@ -1896,7 +1862,7 @@ async function rotateDesktopLogIfNeededAsync() {
return // No live file yet — the append (re)creates it.
}
- for (const [op, src, dst] of planDesktopLogRotation(size)) {
+ for (const [op, src, dst] of planLogRotation(size, DESKTOP_LOG_PATH)) {
try {
if (op === 'rm') {
await fs.promises.rm(src, { force: true })
@@ -1919,7 +1885,7 @@ function flushDesktopLogBufferSync() {
try {
fs.mkdirSync(path.dirname(DESKTOP_LOG_PATH), { recursive: true })
- rotateDesktopLogIfNeededSync()
+ rotateLogIfNeededSync(DESKTOP_LOG_PATH)
fs.appendFileSync(DESKTOP_LOG_PATH, chunk)
} catch {
// Logging must never block app startup/shutdown.