Repository navigation
Remove unfinished clone and compact files at startup - #151
Conversation
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
left a comment
There was a problem hiding this comment.
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.
| // 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 }) |
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
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.
|
Both real, fixed in the last commit:
|
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
left a comment
There was a problem hiding this comment.
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.
| 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} |
There was a problem hiding this comment.
(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.
|
Taken: the message uses |
|
CI failed once on |
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.
<name>.qcow2.vmm-cloneand renamed onceqemu-img convertis done, so an interrupted copy is recognisable as ours. That matters because a<name>.qcow2with 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-cloneand*.qcow2.compact.tmpand nothing else. It runs at startup next toremoveUnfinishedCopies, under the single instance lock, so no other copy of the app can be writing one..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 forremoveUnfinishedDisks(what it deletes, what it must leave: a.partdownload, 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-clonefile is left behindnpm run typecheck,npm run build,npm run smoke(SMOKE OK, which runs the startup cleanup for real)