Repository navigation
Quit anyway: don't wait for the abandoned work before shutting down #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5373c6d
f550ff1
a2bfdb7
406d27b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,8 @@ export interface QuitRunner { | |
| resume(name: string): Promise<unknown> | ||
| shutdown(name: string): Promise<unknown> | ||
| waitForExit(name: string, timeoutMs: number): Promise<boolean> | ||
| /** while held, the VM can't be started (by unfinished work the quit no longer waits for) */ | ||
| holdStarts(name: string, hold: boolean): void | ||
| } | ||
|
|
||
| /** What the quit flow needs from the app around it: the window, the tray and the dialogs. */ | ||
|
|
@@ -74,6 +76,10 @@ const FORCE = 'Force off and quit' | |
| const MORE = 'Wait another minute' | ||
| const MINUTE = 60_000 | ||
|
|
||
| /** VMs whose work the user chose to leave unfinished. Quitting doesn't wait for theirs */ | ||
| type Abandoned = ReadonlySet<string> | ||
| const NONE: Abandoned = new Set() | ||
|
|
||
| /** | ||
| * Closing the window and quitting: the questions about running VMs and unfinished work, the | ||
| * waiting that follows, and what the tray offers meanwhile. It keeps no Electron objects of its | ||
|
|
@@ -128,19 +134,21 @@ export class QuitFlow { | |
| if (this.shutting || this.endWorkWait) return | ||
| // a question is already up: show it instead of a second copy, so the click isn't a no-op | ||
| if (this.asking) return host.show() | ||
| if (this.hasWork() && !(await this.workFirst())) return | ||
| const abandoned = this.hasWork() ? await this.workFirst() : NONE | ||
| if (!abandoned) return | ||
| if (host.runner.runningNames().length === 0) return host.quit() | ||
| host.show() | ||
| await this.askAndQuit(false) | ||
| if (!(await this.askAndQuit(false, abandoned))) this.release(abandoned) | ||
| } | ||
|
|
||
| /** closing the window when it doesn't go to the tray */ | ||
| private async quitFromClose(): Promise<void> { | ||
| const host = this.host | ||
| if (this.hasWork() && !(await this.workFirst())) return | ||
| const abandoned = this.hasWork() ? await this.workFirst() : NONE | ||
| if (!abandoned) return | ||
| if (host.runner.runningNames().length === 0) return host.quit() | ||
| if (host.closeBehavior() === 'quit') return this.shutDownAllAndQuit() | ||
| await this.askAndQuit(true) | ||
| if (host.closeBehavior() === 'quit') return this.shutDownAllAndQuit(abandoned) | ||
| if (!(await this.askAndQuit(true, abandoned))) this.release(abandoned) | ||
| } | ||
|
|
||
| private hasWork(): boolean { | ||
|
|
@@ -160,9 +168,11 @@ export class QuitFlow { | |
|
|
||
| /** | ||
| * Asked before quitting while a profile save or disk work runs, none of which survives the app | ||
| * quitting. True to go on with the quit (right away, or once the work has finished), false to stay. | ||
| * quitting. False to stay. Otherwise the quit goes on, right away or once the work has finished, | ||
| * and the result names the VMs whose work was left unfinished (Quit anyway, or Quit now in the | ||
| * tray), so the rest of the quit doesn't wait for it after all. | ||
| */ | ||
| private async workFirst(): Promise<boolean> { | ||
| private async workFirst(): Promise<Abandoned | false> { | ||
| const host = this.host | ||
| const work = Object.entries(host.vmWork()) | ||
| host.show() | ||
|
|
@@ -176,16 +186,34 @@ export class QuitFlow { | |
| defaultId: 0, | ||
| noLink: true | ||
| }) | ||
| if (choice === NOW) return true | ||
| // only the VMs the question named: work that started while it was up wasn't asked about | ||
| if (choice === NOW) return this.abandon(new Set(work.map(([n]) => n))) | ||
| if (choice !== WAIT) return false | ||
| // the work may have finished while the question was up | ||
| if (!this.hasWork()) return true | ||
| if (!this.hasWork()) return NONE | ||
| host.toTray(false) | ||
| host.balloon({ | ||
| title: 'Quitting when the work is done', | ||
| content: 'The app quits once it has finished. Right-click the tray icon to quit now or to stay.' | ||
| }) | ||
| return this.allWorkDone() | ||
| const quit = await this.allWorkDone() | ||
| if (!quit) return false | ||
| // Quit now in the tray leaves whatever is still running unfinished | ||
| return this.abandon(new Set(Object.keys(host.vmWork()))) | ||
| } | ||
|
|
||
| /** | ||
| * The quit no longer waits for these VMs' work, which keeps running until the app is gone. A | ||
| * session save boots the guest again at its end, only for the quit to kill it, so hold that off. | ||
| */ | ||
| private abandon(names: Abandoned): Abandoned { | ||
| for (const n of names) this.host.runner.holdStarts(n, true) | ||
| return names | ||
| } | ||
|
|
||
| /** the quit didn't happen after all, the abandoned work may finish normally again */ | ||
| private release(names: Abandoned): void { | ||
| for (const n of names) this.host.runner.holdStarts(n, false) | ||
| } | ||
|
|
||
| /** resolves true once no VM has work running or Quit now was picked in the tray, false for Don't quit */ | ||
|
|
@@ -205,9 +233,10 @@ export class QuitFlow { | |
| }) | ||
| } | ||
|
|
||
| private async askAndQuit(fromClose: boolean): Promise<void> { | ||
| /** true once the app is on its way out (shutting down the VMs), false if it stays */ | ||
| private async askAndQuit(fromClose: boolean, abandoned: Abandoned): Promise<boolean> { | ||
| const host = this.host | ||
| if (this.shutting || this.asking) return | ||
| if (this.shutting || this.asking) return false | ||
| // after Quit when it's done the window is in the tray | ||
| host.show() | ||
| const names = host.runner.runningNames() | ||
|
|
@@ -228,11 +257,16 @@ export class QuitFlow { | |
| if (checked && (choice === KEEP || choice === SHUT)) { | ||
| host.rememberClose(choice === KEEP ? 'tray' : 'quit') | ||
| } | ||
| if (choice === KEEP) return host.toTray() | ||
| if (choice === SHUT) return this.shutDownAllAndQuit() | ||
| if (choice === SHUT) { | ||
| // it lets go of the abandoned VMs itself if the quit is cancelled later on | ||
| await this.shutDownAllAndQuit(abandoned) | ||
| return true | ||
| } | ||
| if (choice === KEEP) host.toTray() | ||
| return false | ||
| } | ||
|
|
||
| private async shutDownAllAndQuit(): Promise<void> { | ||
| private async shutDownAllAndQuit(abandoned: Abandoned): Promise<void> { | ||
| const host = this.host | ||
| const runner = host.runner | ||
| const names = runner.runningNames() | ||
|
|
@@ -245,24 +279,22 @@ export class QuitFlow { | |
| content: `Waiting for ${names.length === 1 ? names[0] : `${names.length} VMs`} to shut down, then the app quits.` | ||
| }) | ||
| // a paused guest can't react to the power button, so let it run first. A VM in the middle of a | ||
| // profile save or delete gets the power button press once that's done, not while the guest hibernates | ||
| // profile save or delete gets the power button press once that's done, not while the guest | ||
| // hibernates. Unless the user already said to leave that work unfinished: then waiting for it | ||
| // only makes the quit look stuck, and a hibernate the power button turns into a shutdown is fine | ||
| await Promise.all( | ||
| names.map(async (n) => { | ||
| if (abandoned.has(n)) return this.pressPower(n) | ||
| const saving = host.vmWork()[n] === 'saving' | ||
| await this.workDone(n) | ||
| if (!runner.isRunning(n)) return | ||
| // a session save boots the VM again right before it ends, and the guest misses a power button | ||
| // press while it's still resuming from hibernate. It has the agent, so wait for that to answer | ||
| if (saving) await host.guestBack(n, 120_000) | ||
| try { | ||
| if (runner.state(n).status === 'paused') await runner.resume(n) | ||
| await runner.shutdown(n) | ||
| } catch { | ||
| // no QMP connection: it ends up in the force off question below | ||
| } | ||
| await this.pressPower(n) | ||
| }) | ||
| ) | ||
| if (!(await this.waitForShutdown(names))) return | ||
| if (!(await this.waitForShutdown(names))) return this.release(abandoned) | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Letting go here is right, but by the time the Force off question gets its Cancel (at least a minute in), the abandoned save has usually already reached its |
||
| host.quit() | ||
| } | ||
|
|
||
|
|
@@ -303,6 +335,17 @@ export class QuitFlow { | |
| } | ||
| } | ||
|
|
||
| private async pressPower(name: string): Promise<void> { | ||
| const runner = this.host.runner | ||
| if (!runner.isRunning(name)) return | ||
| try { | ||
| if (runner.state(name).status === 'paused') await runner.resume(name) | ||
| await runner.shutdown(name) | ||
| } catch { | ||
| // no QMP connection: it ends up in the force off question | ||
| } | ||
| } | ||
|
|
||
| /** resolves once the VM has no profile operation or disk work running */ | ||
| private workDone(name: string): Promise<void> { | ||
| return new Promise((resolve) => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { mkdtempSync, rmSync } from 'node:fs' | ||
| import { tmpdir } from 'node:os' | ||
| import { join } from 'node:path' | ||
| import { afterAll, describe, expect, it } from 'vitest' | ||
| import { defaultConfig } from '@shared/config' | ||
| import { dataPaths } from '../src/main/paths' | ||
| import { VmRunner } from '../src/main/vmRunner' | ||
|
|
||
| describe('VmRunner.holdStarts', () => { | ||
| const root = mkdtempSync(join(tmpdir(), 'vmm-hold-')) | ||
| afterAll(() => rmSync(root, { recursive: true, force: true })) | ||
| const paths = dataPaths(root) | ||
| // no QEMU: a start that gets past the hold fails on that instead, without booting anything | ||
| const runner = new VmRunner(paths, () => null) | ||
| const cfg = defaultConfig('held', paths.disks) | ||
|
|
||
| it('refuses to start a held VM and says why', async () => { | ||
| runner.holdStarts('held', true) | ||
| await expect(runner.start(cfg)).rejects.toThrow('was not started again because the app is quitting') | ||
| }) | ||
|
|
||
| it('starts it normally again once let go', async () => { | ||
| runner.holdStarts('held', false) | ||
| await expect(runner.start(cfg)).rejects.not.toThrow('app is quitting') | ||
| }) | ||
|
|
||
| it('only holds the VM it was asked to', async () => { | ||
| runner.holdStarts('other', true) | ||
| await expect(runner.start(cfg)).rejects.not.toThrow('app is quitting') | ||
| runner.holdStarts('other', false) | ||
| }) | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The abandoned work keeps running in the main process; only the quit stops waiting for it. For a session profile save that matters:
takeSessionwaits for the guest to power off and then callsrunner.start(cfg)to boot it again. With the power button pressed mid-hibernate, the guest powers off (a plain shutdown reads as'guest'too, see #120),waitForExithere returns true, and at about the same time the save snapshots the disk and starts the VM again. Thenhost.quit()runs andbefore-quitcallskillAll(), so a guest that is just booting gets a hard power off. Before this PR the quit waited for the save, so that couldn't happen.The save shouldn't be able to start the VM again once the quit has given up on it. Something like a per-VM hold on
startthat the quit sets for the abandoned VMs (and lifts if the user cancels the Force off question) would do it.