fix: bound renderer crash dedupe cache (#4113)

This commit is contained in:
Neil
2026-05-31 04:43:09 -07:00
committed by GitHub
parent e54514243c
commit d6c6b1fca6
2 changed files with 73 additions and 1 deletions
+57 -1
View File
@@ -1,3 +1,4 @@
/* oxlint-disable max-lines -- Why: crash-reporting IPC handlers share one mocked ipcMain registry and store contract. */
import { beforeEach, describe, expect, it, vi } from 'vitest'
import type { CrashReportRecord } from '../../shared/crash-reporting'
@@ -22,7 +23,10 @@ vi.mock('./feedback', () => ({
submitFeedback: submitFeedbackMock
}))
import { registerCrashReportingHandlers } from './crash-reporting'
import {
_resetRendererErrorReportDedupeForTests,
registerCrashReportingHandlers
} from './crash-reporting'
function report(
status: CrashReportRecord['status'] = 'pending',
@@ -52,6 +56,7 @@ describe('registerCrashReportingHandlers', () => {
clipboardWriteTextMock.mockReset()
submitFeedbackMock.mockReset()
submitFeedbackMock.mockResolvedValue({ ok: true })
_resetRendererErrorReportDedupeForTests()
})
it('copies the latest pending diagnostic text to the clipboard', async () => {
@@ -299,4 +304,55 @@ describe('registerCrashReportingHandlers', () => {
).resolves.toEqual({ ok: false, error: 'Invalid renderer error report.' })
expect(recordMock).not.toHaveBeenCalled()
})
it('bounds renderer error dedupe keys by evicting the oldest unique reports', async () => {
let recordCount = 0
const recordMock = vi.fn(async () => report('pending', `react-render-${recordCount++}`))
registerCrashReportingHandlers({
getById: vi.fn(),
dismiss: vi.fn(),
markSent: vi.fn(),
markDismissedSent: vi.fn(),
listRecent: vi.fn(async () => []),
record: recordMock,
formatDiagnosticText: vi.fn()
} as never)
const baseArgs = {
boundaryId: 'terminal.workbench',
surface: 'terminal-workbench',
errorName: 'TypeError',
componentStack: 'at Terminal'
}
for (let i = 0; i < 260; i += 1) {
await handlers.get('crashReports:recordRendererError')?.(null, {
...baseArgs,
errorMessage: `unique-render-error-${i}`
})
}
await expect(
handlers.get('crashReports:recordRendererError')?.(null, {
...baseArgs,
errorMessage: 'unique-render-error-0'
})
).resolves.toEqual({
ok: true,
report: expect.objectContaining({ id: 'react-render-260' }),
deduped: false
})
await expect(
handlers.get('crashReports:recordRendererError')?.(null, {
...baseArgs,
errorMessage: 'unique-render-error-259'
})
).resolves.toEqual({
ok: true,
report: null,
deduped: true
})
expect(recordMock).toHaveBeenCalledTimes(261)
})
})
+16
View File
@@ -17,6 +17,7 @@ const recentRendererErrorReportKeys = new Map<string, number>()
const RENDERER_ERROR_DEDUPE_MS = 10 * 60 * 1000
const MAX_RENDERER_ERROR_KEY_AGE_MS = RENDERER_ERROR_DEDUPE_MS * 2
const MAX_RECENT_RENDERER_ERROR_REPORT_KEYS = 256
const REACT_ERROR_BOUNDARY_SURFACES = new Set<ReactErrorBoundaryReportArgs['surface']>([
'app-root',
@@ -101,6 +102,13 @@ function pruneRendererErrorReportKeys(now: number): void {
recentRendererErrorReportKeys.delete(key)
}
}
while (recentRendererErrorReportKeys.size > MAX_RECENT_RENDERER_ERROR_REPORT_KEYS) {
const oldestKey = recentRendererErrorReportKeys.keys().next().value
if (oldestKey === undefined) {
break
}
recentRendererErrorReportKeys.delete(oldestKey)
}
}
function getRendererErrorReportKey(args: ReactErrorBoundaryReportArgs): string {
@@ -129,6 +137,10 @@ async function recordRendererErrorReport(
return { ok: true, report: null, deduped: true }
}
recentRendererErrorReportKeys.set(key, now)
// Why: renderer error reports are IPC input. A broken renderer can vary the
// component stack/message inside the age window, so bound the main-side
// dedupe map by count as well as time.
pruneRendererErrorReportKeys(now)
const report = await store.record({
source: 'renderer',
@@ -166,6 +178,10 @@ async function recordRendererErrorReport(
return { ok: true, report, deduped: false }
}
export function _resetRendererErrorReportDedupeForTests(): void {
recentRendererErrorReportKeys.clear()
}
async function getLatestPendingReport(
store: CrashReportStore
): Promise<Awaited<ReturnType<CrashReportStore['getLatestPending']>>> {