Skip to content

fix: correctness fixes and verified cleanups from full-codebase audit - #3

Merged
CMGS merged 1 commit into
masterfrom
fix/audit-findings
Jul 2, 2026
Merged

fix: correctness fixes and verified cleanups from full-codebase audit#3
CMGS merged 1 commit into
masterfrom
fix/audit-findings

Conversation

@CMGS

@CMGS CMGS commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Bug fixes (user-visible)

  • vm console printed wrong endpoints. The VNC line used 590%d (so display 10 rendered as 59010, not 5910) and the disabled defaults produced 590-1 / ssh -p 0. It now derives the VNC endpoint and SSH command from the same vncCol/sshCol helpers vm list uses, printing - when either is disabled.
  • vm create/run/clone with an existing --name silently clobbered a live VM. A second call under the same name truncated the running VM's disk overlay and orphaned its qemu. scaffoldVM now refuses when a record already exists (vm "<name>" already exists; rm it first or pick another --name).
  • vm rm raced concurrent lifecycle ops. It was the one handler not taking the per-VM flock, so a concurrent start could relaunch qemu between terminate and RemoveAll. rm now runs terminate/teardown/RemoveAll under withVMLock (tolerating a missing dir).
  • image pull ignored --force for OCI/ghcr refs. It re-downloaded multi-GiB artifacts already in the store. It now mirrors the URL path's idempotency check and skips the oras download when the ref is present (unless --force).

Hardening

  • teardownNet and disconnectNBD now warn on best-effort failures instead of swallowing them, so a leaked TAP/netns or a still-held qcow2 leaves a diagnostic trail.
  • setVNCPassword and the InjectConfig forward path take ctx (dial-retry via utils.WaitFor; subprocesses via CommandContext). NBD cleanup deliberately stays on plain exec.Command so it still runs after cancellation.

Refactors (no behavior change)

  • isFileHeld uses utils.FindVMMByCmdline (one /proc walk) instead of scanning every process's fd table 10x/s during NBD disconnect.
  • --state-dir registered once on the root command (was duplicated per-subtree with drifted help text); qemu.IsQcow2NVRAM is the single source of the NVRAM-format rule (was duplicated across launch args and snapshot selection).
  • Extracted scaffoldVM (shared create/clone block); hoisted the OS-independent prepareNet head over the per-OS provisionNet; dropped applyNet's derivable param; utils.EnsureDirs; findFreeNBD/isFileHeld renames; cmp.Or for the net column.

Validation

  • gofumpt -l . — clean
  • go build ./... and GOOS=linux go build ./... — ok
  • GOOS=darwin golangci-lint run ./... and GOOS=linux golangci-lint run ./... — 0 issues each
  • go test -race -count=1 ./... — pass
  • Smoke-tested against a temp --state-dir: vm console now prints VNC 127.0.0.1:5910 SSH: ssh -p 2222 cocoon@localhost (and -/- when disabled); the duplicate-name guard refuses create --name <existing> and leaves the record dir untouched (no truncated overlay); vm rm prints and exits 0 for a missing name and terminates + removes the dir under the flock for an existing one.

🤖 Generated with Claude Code

Correctness fixes (all confirmed by independent verification):
- vm console printed wrong endpoints ("590%d": 59010 for display 10, "590-1"/
  "ssh -p 0" for the disabled defaults); derive from the same vncCol/sshCol
  helpers vm list uses
- vm create/run/clone with an existing --name silently truncated the live
  disk overlay and orphaned its qemu; scaffoldVM now refuses an existing record
- vm rm raced concurrent lifecycle ops (the one handler not under the per-VM
  flock); wrap terminate/teardown/RemoveAll in withVMLock, tolerating a
  missing dir
- image pull ignored --force for OCI refs and re-downloaded multi-GiB
  artifacts already in the store; mirror the URL path's idempotency check

Reuse/altitude/consistency:
- setVNCPassword: thread ctx and replace the hand-rolled dial-retry loop with
  utils.WaitFor; InjectConfig forward-path subprocesses use CommandContext
  (cleanup stays plain so it survives cancellation)
- isFileHeld via utils.FindVMMByCmdline (one /proc walk) instead of scanning
  every process's fd table 10x/s during nbd disconnect
- teardownNet and disconnectNBD warn on best-effort failures instead of
  swallowing them (leaked TAP/netns and held-qcow2 now leave a trail)
- --state-dir registered once on the root command (was duplicated per-tree
  with drifted help); qemu.IsQcow2NVRAM is the single source of the NVRAM
  format rule (was duplicated in launch args + snapshot selection)
- extract scaffoldVM (create/clone shared block); hoist the OS-independent
  prepareNet head over per-OS provisionNet; drop applyNet's derivable param;
  utils.EnsureDirs; findFreeNBD/isFileHeld renames; cmp.Or for netCol
@CMGS
CMGS merged commit 8bed8e5 into master Jul 2, 2026
3 checks passed
@CMGS
CMGS deleted the fix/audit-findings branch July 2, 2026 04:54
CMGS added a commit that referenced this pull request Jul 3, 2026
…CLI (#3)

Correctness fixes (all confirmed by independent verification):
- vm console printed wrong endpoints ("590%d": 59010 for display 10, "590-1"/
  "ssh -p 0" for the disabled defaults); derive from the same vncCol/sshCol
  helpers vm list uses
- vm create/run/clone with an existing --name silently truncated the live
  disk overlay and orphaned its qemu; scaffoldVM now refuses an existing record
- vm rm raced concurrent lifecycle ops (the one handler not under the per-VM
  flock); wrap terminate/teardown/RemoveAll in withVMLock, tolerating a
  missing dir
- image pull ignored --force for OCI refs and re-downloaded multi-GiB
  artifacts already in the store; mirror the URL path's idempotency check

Reuse/altitude/consistency:
- setVNCPassword: thread ctx and replace the hand-rolled dial-retry loop with
  utils.WaitFor; InjectConfig forward-path subprocesses use CommandContext
  (cleanup stays plain so it survives cancellation)
- isFileHeld via utils.FindVMMByCmdline (one /proc walk) instead of scanning
  every process's fd table 10x/s during nbd disconnect
- teardownNet and disconnectNBD warn on best-effort failures instead of
  swallowing them (leaked TAP/netns and held-qcow2 now leave a trail)
- --state-dir registered once on the root command (was duplicated per-tree
  with drifted help); qemu.IsQcow2NVRAM is the single source of the NVRAM
  format rule (was duplicated in launch args + snapshot selection)
- extract scaffoldVM (create/clone shared block); hoist the OS-independent
  prepareNet head over per-OS provisionNet; drop applyNet's derivable param;
  utils.EnsureDirs; findFreeNBD/isFileHeld renames; cmp.Or for netCol

(cherry picked from commit 8bed8e5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant