Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 35 additions & 11 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,16 +381,38 @@ async function shutDownAllAndQuit(): Promise<void> {
}
})
)
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<boolean> {
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`}.`,

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: minutes counts rounds of the question, not time, so it undercounts whenever the question sat unanswered for a while. Leave the dialog up for ten minutes, click Wait another minute, and the next one says "in 2 minutes" for a guest that has been shutting down for twelve. A Date.now() taken before the loop and rounded to minutes would read true in that case, and it is the same one line.

Also in the detail two lines down: "anything unsaved inside it" reads oddly when the message above named three VMs.

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()
Expand All @@ -402,14 +424,16 @@ async function shutDownAllAndQuit(): Promise<void> {
} 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)
Expand Down
12 changes: 9 additions & 3 deletions src/main/vmRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

Expand Down
Loading