Skip to content

Remove unfinished clone and compact files at startup - #151

Merged
A21-devs merged 4 commits into
mainfrom
feat/clean-unfinished-disks
Dec 4, 2026
Merged

A21-devs merged 4 commits into
mainfrom
feat/clean-unfinished-disks

Conversation

@A21-devs

@A21-devs A21-devs commented Dec 4, 2026

Copy link
Copy Markdown
Owner

Closes #145

Quit anyway in the middle of a clone or a compact left a file as big as the disk behind, and nothing cleaned it up.

  • A full clone is now written to <name>.qcow2.vmm-clone and renamed once qemu-img convert is done, so an interrupted copy is recognisable as ours. That matters because a <name>.qcow2 with no VM pointing at it can just as well be a disk the user kept on purpose: deleting a VM leaves its disk.
  • removeUnfinishedDisks(disksDir) deletes *.qcow2.vmm-clone and *.qcow2.compact.tmp and nothing else. It runs at startup next to removeUnfinishedCopies, under the single instance lock, so no other copy of the app can be writing one.
  • Not .part: a browser downloading a disk image into that folder writes <name>.qcow2.part, which is the same reason the ISO copies use .vmm-copy. The test covers that.

Linked clones are left as they are: creating the two overlays takes milliseconds and already rolls back on failure. A compact of a disk the config points at outside the disks folder also keeps its leftover until the next compact of that VM replaces it.

Testing

  • npm test: 180 pass, including two new cases for removeUnfinishedDisks (what it deletes, what it must leave: a .part download, a base disk, a kept disk; and a missing folder)
  • npm run test:qemu: 197 pass. The full-clone case now also asserts no .vmm-clone file is left behind
  • npm run typecheck, npm run build, npm run smoke (SMOKE OK, which runs the startup cleanup for real)

@A21-devs A21-devs added enhancement New feature or request backend labels Dec 4, 2026
A clone the app was quit in the middle of left a half-written
<name>.qcow2 in the disks folder with no VM using it, and nothing could
tell it apart from a disk the user kept on purpose after deleting a VM.
Writing it under a name no VM can use makes it recognisable as ours.

Not .part: a browser downloading a disk image into that folder writes
<name>.qcow2.part, same reason the ISO copies use .vmm-copy.
Quit anyway in the middle of a clone or a compact leaves a file as big
as the disk behind. The app now clears them when it starts, the way it
already clears unfinished ISO copies, and only the two names it writes
itself.

@A21-devs A21-devs left a comment

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.

The suffix choice and the narrow match are right, and I like that the test pins the .part download case. Two things about the write path in cloneVm, both about rmSync on a file something else may be holding.

Comment thread src/main/disks.ts Outdated
// written under a name no VM can use, so a copy the app is killed in the middle of is
// recognisable as ours and cleaned up at the next start instead of looking like a real disk
const part = `${target}${CLONE_SUFFIX}`
rmSync(part, { force: true })

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.

This pre-removal isn't needed and it can fail where the convert wouldn't: qemu-img convert creates or overwrites its output. I checked with the bundled qemu-img (6.2 in the VMs folder) by putting a junk file at the destination and converting over it, which worked and left a valid qcow2.

What it adds is a failure mode: on Windows rmSync on a file another process has open throws EBUSY/EPERM, and force: true only swallows ENOENT. So a clone racing another clone into the same name (two Clone dialogs, same new name, nothing stops that before the config is saved) now fails with an unlinking error instead of just overwriting, and a leftover a crashed qemu-img still holds would block a retry that used to work. Dropping the line keeps the cleanup of a stale leftover, since convert overwrites it.

Comment thread src/main/disks.ts Outdated
renameSync(part, target)
} catch (e) {
rmSync(target, { force: true }) // don't leave a half-written disk that blocks the next try
rmSync(part, { force: true }) // don't leave a half-written disk that blocks the next try

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.

Same EBUSY, worse place: if this throws, it replaces the error from the convert, so the user sees "EBUSY: resource busy or locked, unlink ..." instead of what qemu-img actually said (no space left, source unreadable). Pre-existing shape, but the tidy-up is best-effort by nature, so it belongs in its own try/catch with the original error rethrown. The linked branch below has the same thing with its two rmSync calls and the rename that puts the source disk back.

@A21-devs

A21-devs commented Dec 4, 2026

Copy link
Copy Markdown
Owner Author

Both real, fixed in the last commit:

  • the pre-removal is gone from both the clone and the compact path. Convert overwrites its output (checked with the bundled qemu-img, as you did), so a leftover from a cut-off run is still dealt with.
  • the removals in the failure paths go through a tryRemove helper that swallows its own error, so the error from qemu-img is what surfaces. The linked branch now also catches a failed renameSync(base, cfg.diskPath) and reports where the original disk is, which was the one case there where losing the error would have left the user with a VM whose disk is under a name they have never seen.

npm test 180, npm run test:qemu 197, npm run typecheck, npm run build, npm run smoke OK.

qemu-img convert overwrites its output, so removing the file first was
only a way to fail where the convert wouldn't: on Windows unlinking a
file another process has open throws, and force: true only swallows
ENOENT.

The removals in the failure paths go through a helper that ignores its
own errors, so the error the user sees is the one from qemu-img. The
linked clone also says where the original disk is if putting it back
fails, instead of throwing an unlink error over it.

@A21-devs A21-devs left a comment

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.

Second pass over the fix commit. Both findings are gone: no rmSync before either convert, and every tidy-up removal goes through tryRemove, so the qemu-img error is what propagates. The linked branch keeping the error and appending where the base disk ended up is better than what I asked for. Two small things in that new message.

Comment thread src/main/disks.ts Outdated
renameSync(base, cfg.diskPath) // put the original back
} catch {
// the overlay is still in the way: say where the disk went rather than hide it
throw new Error(`${(e as Error).message}

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.

(e as Error).message is the one place here that assumes what was thrown. Everything in the path throws Error today, but if anything ever rejects with a string the user gets "undefined" followed by the real instruction, which is the worst possible version of this message. e instanceof Error ? e.message : String(e) is what ipc.ts does with caught values.

Also, the blank line in the middle of the message is a real newline in the template literal, so the second line sits at column 0 of the source. \n\n would keep the paragraph break without the source looking like the indentation slipped.

@A21-devs

A21-devs commented Dec 4, 2026

Copy link
Copy Markdown
Owner Author

Taken: the message uses e instanceof Error ? e.message : String(e) like ipc.ts, and the paragraph break is \n\n in the template literal instead of a real newline. typecheck and npm test (180) still clean.

@A21-devs

A21-devs commented Dec 4, 2026

Copy link
Copy Markdown
Owner Author

CI failed once on tests/usage.test.ts > reads CPU time and memory of a real process: the query came back empty after 30 s, which is the PowerShell start timeout from #141 running out on the runner. Nothing to do with this change, and the rerun of the same commit is green. Filing it separately.

@A21-devs
A21-devs merged commit 9807a55 into main Dec 4, 2026
1 of 2 checks passed
@A21-devs
A21-devs deleted the feat/clean-unfinished-disks branch December 4, 2026 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clean up unfinished clone and compact files at startup

1 participant