Skip to content

Commit e08ee97

Browse files
committed
fix(desktop): a fatal main-process error during or before a quit can no longer hang the app
A fatal failure once quitting has begun now records the event and exits instead of opening the recovery prompt (which, during shutdown, can only fall back to a blocking app-modal NSAlert). A quit while the prompt is open answers it as Quit.
1 parent 92c7057 commit e08ee97

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

‎apps/desktop/src/main/observability.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,47 @@ describe('installMainProcessFailureObservers', () => {
9494
await vi.waitFor(() => expect(app.relaunch).toHaveBeenCalledOnce())
9595
expect(app.exit).toHaveBeenCalledWith(1)
9696
})
97+
98+
function emitAppEvent(event: string) {
99+
const handler = vi.mocked(app.on).mock.calls.find(([name]) => name === event)?.[1]
100+
if (!handler) throw new Error(`No ${event} listener`)
101+
;(handler as () => void)()
102+
}
103+
104+
it('exits without prompting when the failure arrives after quitting has begun', () => {
105+
const events = { filePath: '/tmp/events.log', record: vi.fn() }
106+
const { handlers, source } = createProcessSource()
107+
installMainProcessFailureObservers({ events, getWindow: () => null, processSource: source })
108+
109+
emitAppEvent('before-quit')
110+
handlers.get('uncaughtException')?.(new Error('during shutdown') as never)
111+
112+
expect(events.record).toHaveBeenCalledWith('main_uncaught_exception', { valueType: 'Error' })
113+
expect(dialog.showMessageBox).not.toHaveBeenCalled()
114+
expect(app.relaunch).not.toHaveBeenCalled()
115+
expect(app.exit).toHaveBeenCalledWith(1)
116+
})
117+
118+
it('answers an open recovery prompt as Quit when the app starts quitting', async () => {
119+
vi.mocked(dialog.showMessageBox).mockImplementationOnce(
120+
(options: { cancelId?: number; signal?: AbortSignal }) =>
121+
new Promise((resolve) => {
122+
options.signal?.addEventListener('abort', () =>
123+
resolve({ response: options.cancelId ?? 0, checkboxChecked: false })
124+
)
125+
})
126+
)
127+
const events = { filePath: '/tmp/events.log', record: vi.fn() }
128+
const { handlers, source } = createProcessSource()
129+
installMainProcessFailureObservers({ events, getWindow: () => null, processSource: source })
130+
131+
handlers.get('uncaughtException')?.(new Error('while running') as never)
132+
expect(dialog.showMessageBox).toHaveBeenCalledOnce()
133+
expect(app.exit).not.toHaveBeenCalled()
134+
135+
emitAppEvent('will-quit')
136+
137+
await vi.waitFor(() => expect(app.exit).toHaveBeenCalledWith(1))
138+
expect(app.relaunch).not.toHaveBeenCalled()
139+
})
97140
})

‎apps/desktop/src/main/observability.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,20 @@ const FAILURE_DEDUPE_MS = 5_000
7979
* single native recovery surface. Error text is deliberately excluded from
8080
* the structured log because rejected values can contain request payloads or
8181
* credentials; the event kind and crash dumps are enough for triage.
82+
*
83+
* The recovery prompt never outlives a quit: a failure once quitting has begun
84+
* exits without asking (a dialog opened during shutdown can only fall back to
85+
* a blocking native alert), and a quit while the prompt is open answers it as
86+
* Quit.
8287
*/
8388
export function installMainProcessFailureObservers({
8489
events,
8590
getWindow,
8691
processSource = process,
8792
}: MainProcessFailureObserverDeps): void {
8893
let fatalRecoveryOpen = false
94+
let quitStarted = false
95+
const quitSignal = new AbortController()
8996
let lastChildFailure = ''
9097
let lastChildFailureAt = 0
9198

@@ -117,13 +124,18 @@ export function installMainProcessFailureObservers({
117124
fatalRecoveryOpen = true
118125
events.record(name, { valueType: value instanceof Error ? 'Error' : typeof value })
119126
logger.error('Fatal main-process failure', { kind: name })
127+
if (quitStarted) {
128+
app.exit(1)
129+
return
130+
}
120131
const options = {
121132
type: 'error' as const,
122133
buttons: ['Restart Sim', 'Quit Sim'],
123134
defaultId: 0,
124135
cancelId: 1,
125136
message: 'Sim encountered a problem',
126137
detail: 'Restart Sim to recover. Diagnostic details were saved locally.',
138+
signal: quitSignal.signal,
127139
}
128140
const win = getWindow()
129141
const prompt =
@@ -145,7 +157,14 @@ export function installMainProcessFailureObservers({
145157
reportFatal('main_uncaught_exception', error)
146158
}
147159

160+
const onQuitStarted = (): void => {
161+
quitStarted = true
162+
quitSignal.abort()
163+
}
164+
148165
app.on('child-process-gone', onChildProcessGone)
166+
app.on('before-quit', onQuitStarted)
167+
app.on('will-quit', onQuitStarted)
149168
processSource.on('unhandledRejection', onUnhandledRejection)
150169
processSource.on('uncaughtException', onUncaughtException)
151170
}

0 commit comments

Comments
 (0)