Skip to content

Commit fa41a2d

Browse files
committed
fix(desktop): only a committed quit skips the recovery prompt
1 parent be9729f commit fa41a2d

2 files changed

Lines changed: 46 additions & 17 deletions

File tree

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,17 @@ describe('installMainProcessFailureObservers', () => {
101101
;(handler as () => void)()
102102
}
103103

104-
it('exits without prompting when the failure arrives after quitting has begun', () => {
104+
async function settlePrompt() {
105+
for (let turn = 0; turn < 5; turn++) await Promise.resolve()
106+
}
107+
108+
it('exits without prompting when the failure arrives once the quit is committed', () => {
105109
const events = { filePath: '/tmp/events.log', record: vi.fn() }
106110
const { handlers, source } = createProcessSource()
107111
installMainProcessFailureObservers({ events, getWindow: () => null, processSource: source })
108112

109113
emitAppEvent('before-quit')
114+
emitAppEvent('will-quit')
110115
handlers.get('uncaughtException')?.(new Error('during shutdown') as never)
111116

112117
expect(events.record).toHaveBeenCalledWith('main_uncaught_exception', { valueType: 'Error' })
@@ -115,7 +120,24 @@ describe('installMainProcessFailureObservers', () => {
115120
expect(app.exit).toHaveBeenCalledWith(1)
116121
})
117122

118-
it('answers an open recovery prompt as Quit when the app starts quitting', async () => {
123+
it('still offers recovery after a quit request another listener cancelled', () => {
124+
vi.mocked(dialog.showMessageBox).mockImplementationOnce(() => new Promise(() => {}))
125+
const events = { filePath: '/tmp/events.log', record: vi.fn() }
126+
const { handlers, source } = createProcessSource()
127+
installMainProcessFailureObservers({ events, getWindow: () => null, processSource: source })
128+
129+
emitAppEvent('before-quit')
130+
handlers.get('uncaughtException')?.(new Error('after a cancelled quit') as never)
131+
132+
expect(dialog.showMessageBox).toHaveBeenCalledOnce()
133+
const [options] = vi.mocked(dialog.showMessageBox).mock.calls[0] as unknown as [
134+
{ signal?: AbortSignal },
135+
]
136+
expect(options.signal?.aborted).toBe(false)
137+
expect(app.exit).not.toHaveBeenCalled()
138+
})
139+
140+
it('answers an open recovery prompt as Quit when a quit is requested', async () => {
119141
vi.mocked(dialog.showMessageBox).mockImplementationOnce(
120142
(options: { cancelId?: number; signal?: AbortSignal }) =>
121143
new Promise((resolve) => {
@@ -132,9 +154,10 @@ describe('installMainProcessFailureObservers', () => {
132154
expect(dialog.showMessageBox).toHaveBeenCalledOnce()
133155
expect(app.exit).not.toHaveBeenCalled()
134156

135-
emitAppEvent('will-quit')
157+
emitAppEvent('before-quit')
158+
await settlePrompt()
136159

137-
await vi.waitFor(() => expect(app.exit).toHaveBeenCalledWith(1))
160+
expect(app.exit).toHaveBeenCalledWith(1)
138161
expect(app.relaunch).not.toHaveBeenCalled()
139162
})
140163
})

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,20 @@ const FAILURE_DEDUPE_MS = 5_000
8080
* the structured log because rejected values can contain request payloads or
8181
* credentials; the event kind and crash dumps are enough for triage.
8282
*
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.
83+
* The recovery prompt never outlives a quit: a failure once a quit is committed
84+
* (`will-quit`) exits without asking, since a dialog opened during shutdown can
85+
* only fall back to a blocking native alert, and any quit request while the
86+
* prompt is open answers it as Quit. A `before-quit` that another listener
87+
* cancels leaves later failures their prompt.
8788
*/
8889
export function installMainProcessFailureObservers({
8990
events,
9091
getWindow,
9192
processSource = process,
9293
}: MainProcessFailureObserverDeps): void {
9394
let fatalRecoveryOpen = false
94-
let quitStarted = false
95-
const quitSignal = new AbortController()
95+
let quitCommitted = false
96+
let openPrompt: AbortController | null = null
9697
let lastChildFailure = ''
9798
let lastChildFailureAt = 0
9899

@@ -124,18 +125,20 @@ export function installMainProcessFailureObservers({
124125
fatalRecoveryOpen = true
125126
events.record(name, { valueType: value instanceof Error ? 'Error' : typeof value })
126127
logger.error('Fatal main-process failure', { kind: name })
127-
if (quitStarted) {
128+
if (quitCommitted) {
128129
app.exit(1)
129130
return
130131
}
132+
const promptAbort = new AbortController()
133+
openPrompt = promptAbort
131134
const options = {
132135
type: 'error' as const,
133136
buttons: ['Restart Sim', 'Quit Sim'],
134137
defaultId: 0,
135138
cancelId: 1,
136139
message: 'Sim encountered a problem',
137140
detail: 'Restart Sim to recover. Diagnostic details were saved locally.',
138-
signal: quitSignal.signal,
141+
signal: promptAbort.signal,
139142
}
140143
const win = getWindow()
141144
const prompt =
@@ -157,14 +160,17 @@ export function installMainProcessFailureObservers({
157160
reportFatal('main_uncaught_exception', error)
158161
}
159162

160-
const onQuitStarted = (): void => {
161-
quitStarted = true
162-
quitSignal.abort()
163+
const answerOpenPromptAsQuit = (): void => {
164+
openPrompt?.abort()
165+
}
166+
const onWillQuit = (): void => {
167+
quitCommitted = true
168+
answerOpenPromptAsQuit()
163169
}
164170

165171
app.on('child-process-gone', onChildProcessGone)
166-
app.on('before-quit', onQuitStarted)
167-
app.on('will-quit', onQuitStarted)
172+
app.on('before-quit', answerOpenPromptAsQuit)
173+
app.on('will-quit', onWillQuit)
168174
processSource.on('unhandledRejection', onUnhandledRejection)
169175
processSource.on('uncaughtException', onUncaughtException)
170176
}

0 commit comments

Comments
 (0)