diff --git a/src/main/crash-reporting/gpu-crash-fallback-decision.test.ts b/src/main/crash-reporting/gpu-crash-fallback-decision.test.ts new file mode 100644 index 000000000..9b84562c6 --- /dev/null +++ b/src/main/crash-reporting/gpu-crash-fallback-decision.test.ts @@ -0,0 +1,114 @@ +import { describe, expect, it } from 'vitest' +import { + DEFAULT_GPU_CRASH_FALLBACK_THRESHOLD, + DEFAULT_GPU_CRASH_FALLBACK_WINDOW_MS, + GpuCrashFallbackTracker, + isGpuChildProcessType, + isGpuFallbackCrashCandidate +} from './gpu-crash-fallback-decision' + +describe('GpuCrashFallbackTracker', () => { + it('engages fallback once GPU crashes hit the threshold inside the window', () => { + const tracker = new GpuCrashFallbackTracker({ windowMs: 30_000, threshold: 3 }) + // F0BDNADU79Q / F0BDNRZ5MDG: GPU child dies within seconds of launch. + expect(tracker.recordGpuCrash(500)).toEqual({ + shouldEngageFallback: false, + crashesInWindow: 1 + }) + expect(tracker.recordGpuCrash(8_000)).toEqual({ + shouldEngageFallback: false, + crashesInWindow: 2 + }) + expect(tracker.recordGpuCrash(16_000)).toEqual({ + shouldEngageFallback: true, + crashesInWindow: 3 + }) + }) + + it('engages at most once so the relaunch cannot loop', () => { + const tracker = new GpuCrashFallbackTracker({ windowMs: 30_000, threshold: 2 }) + tracker.recordGpuCrash(100) + expect(tracker.recordGpuCrash(200).shouldEngageFallback).toBe(true) + expect(tracker.hasEngaged()).toBe(true) + expect(tracker.recordGpuCrash(300)).toEqual({ + shouldEngageFallback: false, + crashesInWindow: 2 + }) + }) + + it('ignores GPU crashes after the post-launch window', () => { + const tracker = new GpuCrashFallbackTracker({ windowMs: 30_000, threshold: 3 }) + tracker.recordGpuCrash(1_000) + tracker.recordGpuCrash(2_000) + // A late hiccup well into the session is normal Chromium churn. + expect(tracker.recordGpuCrash(45_000)).toEqual({ + shouldEngageFallback: false, + crashesInWindow: 2 + }) + expect(tracker.hasEngaged()).toBe(false) + }) + + it('ignores impossible timestamps', () => { + const tracker = new GpuCrashFallbackTracker({ windowMs: 30_000, threshold: 1 }) + expect(tracker.recordGpuCrash(-1).shouldEngageFallback).toBe(false) + expect(tracker.recordGpuCrash(Number.NaN).shouldEngageFallback).toBe(false) + expect(tracker.hasEngaged()).toBe(false) + }) + + it('classifies GPU child process types case-insensitively', () => { + expect(isGpuChildProcessType('GPU')).toBe(true) + expect(isGpuChildProcessType('gpu')).toBe(true) + expect(isGpuChildProcessType('Utility')).toBe(false) + expect(isGpuChildProcessType(undefined)).toBe(false) + }) + + it('ships conservative defaults', () => { + expect(DEFAULT_GPU_CRASH_FALLBACK_WINDOW_MS).toBe(30_000) + expect(DEFAULT_GPU_CRASH_FALLBACK_THRESHOLD).toBe(3) + }) +}) + +describe('isGpuFallbackCrashCandidate', () => { + it('tracks only crash-shaped Windows GPU child failures', () => { + for (const reason of ['abnormal-exit', 'crashed', 'launch-failed']) { + expect( + isGpuFallbackCrashCandidate({ + platform: 'win32', + processType: 'GPU', + reason + }) + ).toBe(true) + } + }) + + it('ignores normal GPU exits and non-Windows platforms', () => { + expect( + isGpuFallbackCrashCandidate({ + platform: 'win32', + processType: 'GPU', + reason: 'clean-exit' + }) + ).toBe(false) + expect( + isGpuFallbackCrashCandidate({ + platform: 'win32', + processType: 'GPU', + reason: 'killed' + }) + ).toBe(false) + expect( + isGpuFallbackCrashCandidate({ + platform: 'linux', + processType: 'GPU', + reason: 'crashed' + }) + ).toBe(false) + expect( + isGpuFallbackCrashCandidate({ + platform: 'win32', + processType: 'Utility', + reason: 'crashed' + }) + ).toBe(false) + }) +}) diff --git a/src/main/crash-reporting/gpu-crash-fallback-decision.ts b/src/main/crash-reporting/gpu-crash-fallback-decision.ts new file mode 100644 index 000000000..ab1373189 --- /dev/null +++ b/src/main/crash-reporting/gpu-crash-fallback-decision.ts @@ -0,0 +1,87 @@ +export type GpuCrashFallbackOptions = { + /** Window after launch in which clustered GPU crashes indicate a broken driver. */ + windowMs: number + /** GPU child crashes within the window that trigger software-rendering fallback. */ + threshold: number +} + +const GPU_FALLBACK_CRASH_REASONS = new Set(['abnormal-exit', 'crashed', 'launch-failed']) + +// Why: on old/flaky GPU drivers the GPU child process crashes (STATUS_BREAKPOINT +// / ANGLE-D3D init failure) within seconds of launch, repeatedly - Windows +// clusters F0BDNADU79Q and F0BDNRZ5MDG. GPU child deaths are intentionally +// suppressed as recoverable churn, so Orca never reacted. A burst right after +// launch is the signal that hardware acceleration is unusable on this machine. +export const DEFAULT_GPU_CRASH_FALLBACK_WINDOW_MS = 30_000 +export const DEFAULT_GPU_CRASH_FALLBACK_THRESHOLD = 3 + +/** + * Tracks GPU child-process crashes relative to launch and decides when to fall + * back to software rendering on the next launch. Pure and deterministic: + * callers pass `now` (ms since launch) so behavior is testable without timers. + * + * Only crashes inside the post-launch window count: a one-off GPU hiccup hours + * into a session is normal Chromium churn, not a broken-driver signal. + */ +export class GpuCrashFallbackTracker { + private readonly windowMs: number + private readonly threshold: number + private crashesInWindow = 0 + private engaged = false + + constructor(options: GpuCrashFallbackOptions) { + this.windowMs = options.windowMs + this.threshold = options.threshold + } + + /** + * Records a GPU child crash at `msSinceLaunch` and reports whether this crash + * just pushed the count over the threshold (i.e. fallback should engage now). + * Returns false for crashes outside the window or after fallback already + * engaged, so the caller relaunches at most once. + */ + recordGpuCrash(msSinceLaunch: number): { + shouldEngageFallback: boolean + crashesInWindow: number + } { + if ( + this.engaged || + !Number.isFinite(msSinceLaunch) || + msSinceLaunch < 0 || + msSinceLaunch > this.windowMs + ) { + return { shouldEngageFallback: false, crashesInWindow: this.crashesInWindow } + } + this.crashesInWindow += 1 + if (this.crashesInWindow >= this.threshold) { + this.engaged = true + return { shouldEngageFallback: true, crashesInWindow: this.crashesInWindow } + } + return { shouldEngageFallback: false, crashesInWindow: this.crashesInWindow } + } + + hasEngaged(): boolean { + return this.engaged + } +} + +/** True for the Chromium child process types whose crashes should count here. */ +export function isGpuChildProcessType(processType: string | undefined): boolean { + return (processType ?? '').toLowerCase() === 'gpu' +} + +export function isGpuFallbackCrashCandidate({ + platform, + processType, + reason +}: { + platform: NodeJS.Platform + processType: string | undefined + reason: string +}): boolean { + return ( + platform === 'win32' && + isGpuChildProcessType(processType) && + GPU_FALLBACK_CRASH_REASONS.has(reason) + ) +} diff --git a/src/main/index.ts b/src/main/index.ts index 7683feac0..692d89322 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -63,6 +63,18 @@ import { shouldInstallManagedHooks } from './startup/configure-process' import { ensureVirtualDisplayForHeadlessServe } from './startup/ensure-virtual-display' +import { + readActiveGpuFallbackMarker, + writeGpuFallbackMarker, + type GpuFallbackEnvironment, + type WindowsGpuFallbackEnvironment +} from './startup/gpu-fallback-marker' +import { + DEFAULT_GPU_CRASH_FALLBACK_THRESHOLD, + DEFAULT_GPU_CRASH_FALLBACK_WINDOW_MS, + GpuCrashFallbackTracker, + isGpuFallbackCrashCandidate +} from './crash-reporting/gpu-crash-fallback-decision' import { shouldSuppressDevEducation, suppressDevEducationForStore @@ -188,6 +200,14 @@ let automations: AutomationService | null = null let keybindings: KeybindingService | null = null let expectedRendererReload: { webContentsId: number; until: number } | null = null let firstWindowStartupServicesReady: Promise = Promise.resolve() +// Why: GPU child crashes clustered right after launch indicate a broken driver; +// track them so Orca can move this build onto software rendering. +const gpuLaunchTimeMs = Date.now() +const gpuCrashFallbackTracker = new GpuCrashFallbackTracker({ + windowMs: DEFAULT_GPU_CRASH_FALLBACK_WINDOW_MS, + threshold: DEFAULT_GPU_CRASH_FALLBACK_THRESHOLD +}) +let gpuFallbackActiveThisLaunch = false let localPtyStartupReady: Promise = Promise.resolve() const AGENT_STATE_CRASH_BREADCRUMB_MIN_INTERVAL_MS = 30_000 const isServeMode = process.argv.includes('--serve') @@ -512,7 +532,10 @@ if (hasSingleInstanceLock) { platform: process.platform }) configureElectronNetworkCompatibility() - enableMainProcessGpuFeatures() + maybeApplyGpuFallbackForThisLaunch() + if (!gpuFallbackActiveThisLaunch) { + enableMainProcessGpuFeatures() + } // Why: headless serve backs browser panes with offscreen BrowserWindows, which // need an X display on Linux. Ensure one (Xvfb) before whenReady; the result // gates whether the offscreen backend is installed so capability stays honest. @@ -981,6 +1004,81 @@ async function presentRendererRecoveryPrompt(recentRecoveryCount: number): Promi } } +function getGpuFallbackEnvironment(): GpuFallbackEnvironment { + return { + appVersion: app.getVersion(), + electronVersion: process.versions.electron ?? '', + platform: process.platform + } +} + +function getWindowsGpuFallbackEnvironment(): WindowsGpuFallbackEnvironment | null { + const environment = getGpuFallbackEnvironment() + if (environment.platform !== 'win32') { + return null + } + return { ...environment, platform: 'win32' } +} + +// Why: the GPU-fallback marker must be read before app.whenReady() resolves so +// app.disableHardwareAcceleration() can take effect. Windows desktop only - +// headless serve already runs software rendering on the platforms that need it. +function maybeApplyGpuFallbackForThisLaunch(): void { + if (isServeMode || process.platform !== 'win32') { + return + } + const marker = readActiveGpuFallbackMarker(app.getPath('userData'), getGpuFallbackEnvironment()) + if (!marker) { + return + } + app.disableHardwareAcceleration() + app.commandLine.appendSwitch('disable-gpu') + gpuFallbackActiveThisLaunch = true + recordCrashBreadcrumb('gpu_fallback_applied', { + crashesInWindow: marker.crashesInWindow + }) +} + +// Why: a burst of crash-shaped Windows GPU child failures right after launch +// means hardware acceleration is unusable on this machine. Persist a build- +// scoped marker and relaunch into software rendering instead of looping crashes. +function handleGpuChildCrash(reason: string, exitCode: number | null): void { + // Software rendering already active or shutting down: nothing more to do. + if (gpuFallbackActiveThisLaunch || isQuitting || isServeMode) { + return + } + const result = gpuCrashFallbackTracker.recordGpuCrash(Date.now() - gpuLaunchTimeMs) + if (!result.shouldEngageFallback) { + return + } + recordCrashBreadcrumb('gpu_fallback_engaged', { + reason, + exitCode, + crashesInWindow: result.crashesInWindow + }) + const engagedAt = Date.now() + const environment = getWindowsGpuFallbackEnvironment() + if (!environment) { + return + } + try { + writeGpuFallbackMarker( + app.getPath('userData'), + { + engagedAt, + crashesInWindow: result.crashesInWindow + }, + environment + ) + } catch (error) { + console.warn('[gpu-fallback] failed to persist marker:', error) + return + } + isQuitting = true + app.relaunch() + app.exit(0) +} + function recordProcessGoneCrash( source: 'renderer' | 'child', processType: string, @@ -1655,6 +1753,15 @@ app.whenReady().then(async () => { serviceName: details.serviceName, type: details.type }) + if ( + isGpuFallbackCrashCandidate({ + platform: process.platform, + processType: details.type, + reason: details.reason + }) + ) { + handleGpuChildCrash(details.reason, details.exitCode ?? null) + } }) logStartupMilestone('services-initialized') diff --git a/src/main/startup/gpu-fallback-marker.test.ts b/src/main/startup/gpu-fallback-marker.test.ts new file mode 100644 index 000000000..c52fc3c1f --- /dev/null +++ b/src/main/startup/gpu-fallback-marker.test.ts @@ -0,0 +1,102 @@ +import { existsSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import os from 'node:os' +import { join } from 'node:path' +import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import { + GPU_FALLBACK_MARKER_FILE, + clearGpuFallbackMarker, + readActiveGpuFallbackMarker, + readGpuFallbackMarker, + writeGpuFallbackMarker +} from './gpu-fallback-marker' + +describe('gpu-fallback-marker', () => { + let userDataPath: string + const environment = { + appVersion: '1.2.3', + electronVersion: '42.3.3', + platform: 'win32' as const + } + + beforeEach(() => { + userDataPath = mkdtempSync(join(os.tmpdir(), 'orca-gpu-fallback-test-')) + }) + + afterEach(() => { + rmSync(userDataPath, { recursive: true, force: true }) + }) + + it('round-trips a written marker', () => { + writeGpuFallbackMarker(userDataPath, { engagedAt: 123, crashesInWindow: 3 }, environment) + expect(readGpuFallbackMarker(userDataPath)).toEqual({ + schemeVersion: 2, + engagedAt: 123, + crashesInWindow: 3, + appVersion: '1.2.3', + electronVersion: '42.3.3', + platform: 'win32' + }) + }) + + it('returns null when no marker exists', () => { + expect(readGpuFallbackMarker(userDataPath)).toBeNull() + expect(readActiveGpuFallbackMarker(userDataPath, environment)).toBeNull() + }) + + it('keeps an active marker for repeated launches on the same build', () => { + writeGpuFallbackMarker(userDataPath, { engagedAt: 1, crashesInWindow: 4 }, environment) + expect(existsSync(join(userDataPath, GPU_FALLBACK_MARKER_FILE))).toBe(true) + + const firstRead = readActiveGpuFallbackMarker(userDataPath, environment) + expect(firstRead?.crashesInWindow).toBe(4) + expect(existsSync(join(userDataPath, GPU_FALLBACK_MARKER_FILE))).toBe(true) + + const secondRead = readActiveGpuFallbackMarker(userDataPath, environment) + expect(secondRead?.crashesInWindow).toBe(4) + }) + + it('clears an active marker when the app build changes', () => { + writeGpuFallbackMarker(userDataPath, { engagedAt: 1, crashesInWindow: 4 }, environment) + + expect( + readActiveGpuFallbackMarker(userDataPath, { + ...environment, + appVersion: '1.2.4' + }) + ).toBeNull() + expect(existsSync(join(userDataPath, GPU_FALLBACK_MARKER_FILE))).toBe(false) + }) + + it('clears an active marker outside Windows', () => { + writeGpuFallbackMarker(userDataPath, { engagedAt: 1, crashesInWindow: 4 }, environment) + + expect( + readActiveGpuFallbackMarker(userDataPath, { + ...environment, + platform: 'linux' + }) + ).toBeNull() + expect(existsSync(join(userDataPath, GPU_FALLBACK_MARKER_FILE))).toBe(false) + }) + + it('clears a corrupt or wrong-version marker', () => { + writeFileSync(join(userDataPath, GPU_FALLBACK_MARKER_FILE), '{ not json') + expect(readGpuFallbackMarker(userDataPath)).toBeNull() + expect(readActiveGpuFallbackMarker(userDataPath, environment)).toBeNull() + expect(existsSync(join(userDataPath, GPU_FALLBACK_MARKER_FILE))).toBe(false) + + writeFileSync( + join(userDataPath, GPU_FALLBACK_MARKER_FILE), + JSON.stringify({ schemeVersion: 999, engagedAt: 1, crashesInWindow: 1 }) + ) + expect(readGpuFallbackMarker(userDataPath)).toBeNull() + expect(readActiveGpuFallbackMarker(userDataPath, environment)).toBeNull() + expect(existsSync(join(userDataPath, GPU_FALLBACK_MARKER_FILE))).toBe(false) + }) + + it('can explicitly clear the marker', () => { + writeGpuFallbackMarker(userDataPath, { engagedAt: 1, crashesInWindow: 4 }, environment) + clearGpuFallbackMarker(userDataPath) + expect(readGpuFallbackMarker(userDataPath)).toBeNull() + }) +}) diff --git a/src/main/startup/gpu-fallback-marker.ts b/src/main/startup/gpu-fallback-marker.ts new file mode 100644 index 000000000..e94e654b3 --- /dev/null +++ b/src/main/startup/gpu-fallback-marker.ts @@ -0,0 +1,117 @@ +import { existsSync, readFileSync, rmSync, writeFileSync } from 'node:fs' +import { join } from 'node:path' + +/** + * Persisted "disable hardware acceleration for this build" marker. + * + * Why a standalone file (not the Store): app.disableHardwareAcceleration() must + * be called before app.whenReady() resolves, but the settings Store is only + * constructed inside whenReady. A tiny JSON marker in userData can be read + * synchronously during early startup, mirroring windows-user-data-acl.ts. + */ + +export const GPU_FALLBACK_MARKER_FILE = 'gpu-fallback.json' +export const GPU_FALLBACK_SCHEME_VERSION = 2 + +export type GpuFallbackEnvironment = { + appVersion: string + electronVersion: string + platform: NodeJS.Platform +} + +export type WindowsGpuFallbackEnvironment = GpuFallbackEnvironment & { platform: 'win32' } + +export type GpuFallbackMarker = { + schemeVersion: number + engagedAt: number + crashesInWindow: number + appVersion: string + electronVersion: string + platform: 'win32' +} + +function markerPath(userDataPath: string): string { + return join(userDataPath, GPU_FALLBACK_MARKER_FILE) +} + +export function readGpuFallbackMarker(userDataPath: string): GpuFallbackMarker | null { + try { + const parsed = JSON.parse(readFileSync(markerPath(userDataPath), 'utf-8')) as Partial< + Record + > + if (parsed.schemeVersion !== GPU_FALLBACK_SCHEME_VERSION) { + return null + } + if ( + typeof parsed.engagedAt !== 'number' || + !Number.isFinite(parsed.engagedAt) || + typeof parsed.crashesInWindow !== 'number' || + !Number.isFinite(parsed.crashesInWindow) || + typeof parsed.appVersion !== 'string' || + typeof parsed.electronVersion !== 'string' || + parsed.platform !== 'win32' + ) { + return null + } + return { + schemeVersion: GPU_FALLBACK_SCHEME_VERSION, + engagedAt: parsed.engagedAt, + crashesInWindow: parsed.crashesInWindow, + appVersion: parsed.appVersion, + electronVersion: parsed.electronVersion, + platform: parsed.platform + } + } catch { + // missing or corrupt means no fallback requested + } + return null +} + +export function writeGpuFallbackMarker( + userDataPath: string, + info: { engagedAt: number; crashesInWindow: number }, + environment: WindowsGpuFallbackEnvironment +): void { + const marker: GpuFallbackMarker = { + schemeVersion: GPU_FALLBACK_SCHEME_VERSION, + engagedAt: info.engagedAt, + crashesInWindow: info.crashesInWindow, + appVersion: environment.appVersion, + electronVersion: environment.electronVersion, + platform: 'win32' + } + writeFileSync(markerPath(userDataPath), JSON.stringify(marker)) +} + +export function clearGpuFallbackMarker(userDataPath: string): void { + try { + rmSync(markerPath(userDataPath), { force: true }) + } catch { + // best effort; a stale marker is revalidated on the next launch + } +} + +export function readActiveGpuFallbackMarker( + userDataPath: string, + environment: GpuFallbackEnvironment +): GpuFallbackMarker | null { + const marker = readGpuFallbackMarker(userDataPath) + if (!marker) { + if (existsSync(markerPath(userDataPath))) { + clearGpuFallbackMarker(userDataPath) + } + return null + } + if ( + environment.platform !== 'win32' || + marker.platform !== environment.platform || + marker.appVersion !== environment.appVersion || + marker.electronVersion !== environment.electronVersion + ) { + // Why: the marker is sticky only for the build that observed the driver + // crash burst; updates get one fresh hardware attempt automatically. + clearGpuFallbackMarker(userDataPath) + return null + } + return marker +}