diff --git a/src/main/index.ts b/src/main/index.ts index 3030dc2..282f3f1 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -381,16 +381,38 @@ async function shutDownAllAndQuit(): Promise { } }) ) - const done = await Promise.all(names.map((n) => runner.waitForExit(n, 60_000))) - const stuck = names.filter((_n, i) => !done[i]) - if (stuck.length) { + if (!(await waitForShutdown(names))) return + quitting = true + app.quit() +} + +const FORCE = 'Force off and quit' +const MORE = 'Wait another minute' +const MINUTE = 60_000 + +/** + * Waits a minute for each guest, then asks about the ones still up. True to go on with the quit, + * false if the user cancelled. A Windows guest installing updates on shutdown regularly needs + * longer than a minute, so the question offers another minute as well as Force off. + */ +async function waitForShutdown(names: string[]): Promise { + const runner = backend!.runner + let waiting = names + // real time, not rounds of the question: it counts a question left unanswered for a while too + const since = Date.now() + for (;;) { + const done = await Promise.all(waiting.map((n) => runner.waitForExit(n, MINUTE))) + waiting = waiting.filter((_n, i) => !done[i]) + if (!waiting.length) return true + const minutes = Math.max(1, Math.round((Date.now() - since) / MINUTE)) const opts = { type: 'warning' as const, title: 'Still running', - message: `${stuck.join(', ')} did not shut down within a minute.`, - detail: 'Force off loses anything unsaved inside the guest.', - buttons: ['Force off and quit', 'Cancel'], - cancelId: 1, + message: `${waiting.join(', ')} ${waiting.length === 1 ? 'has' : 'have'} not shut down ${minutes === 1 ? 'within a minute' : `in ${minutes} minutes`}.`, + detail: `A guest installing updates on shutdown can take a few minutes. Force off loses anything unsaved inside ${waiting.length === 1 ? 'it' : 'them'}.`, + buttons: [MORE, FORCE, 'Cancel'], + cancelId: 2, + defaultId: 0, noLink: true } showWindow() @@ -402,14 +424,16 @@ async function shutDownAllAndQuit(): Promise { } finally { askingAboutVms = false } - if (r.response !== 0) { + const choice = opts.buttons[r.response] + if (choice === FORCE) return true + if (choice !== MORE) { shuttingDown = false updateTrayMenu() - return + return false } + // back out of the way for the next minute, the tray already says what the app is waiting for + hideToTray(false) } - quitting = true - app.quit() } app.on('second-instance', showWindow) diff --git a/src/main/vmRunner.ts b/src/main/vmRunner.ts index 6a32eea..6b72a75 100644 --- a/src/main/vmRunner.ts +++ b/src/main/vmRunner.ts @@ -284,11 +284,17 @@ export class VmRunner extends EventEmitter { const r = this.running.get(name) if (!r) return Promise.resolve(true) return new Promise((resolve) => { - const t = setTimeout(() => resolve(false), timeoutMs) - r.proc.once('exit', () => { + const onExit = (): void => { clearTimeout(t) resolve(true) - }) + } + const t = setTimeout(() => { + // the process can outlive several waits (the quit flow asks and waits again), and a + // listener left behind for each of them trips Node's max listeners warning + r.proc.off('exit', onExit) + resolve(false) + }, timeoutMs) + r.proc.once('exit', onExit) }) }