gstack icon indicating copy to clipboard operation
gstack copied to clipboard

ubicloud: reap and up die with "invalid VM name '-'" on a project with no VMs

Open yolo-jared opened this issue 1 month ago • 1 comments

What happened

On a Ubicloud project with no VMs, gstack-ubi reap exits 1, and gstack-ubi up exits 1 before any create request (the reap runs first, UBI_REAP defaults on):

ubi-runner: reaping expired ./- (owner -; its TTL ended 29890577 min ago)
ubi-runner: invalid VM name '-'

Nothing here is macOS-specific (it is bash parsing plus the API's response); it reproduces with bash 3.2.57 and 5.3.9. The workaround is UBI_REAP=0.

Cause

For vm list -N -f location,name on a project with no VMs, Ubicloud returns a single newline: format_rows emits nothing for no rows with headers off, and finalize_response appends "\n" to an empty body.

vm_rows reads that blank line as a VM with an empty location and name and echoes " . - -" (two leading spaces). cmd_reap re-reads the row with read -r loc name owner expires created; word splitting drops the two empty leading fields, so it sees loc=. name=- owner=- expires="". [ -n "$name" ] passes, expires is not -, and $(( expires + REAP_GRACE )) treats the empty string as 0, so the row counts as expired. cmd_down - then hits die "invalid VM name", which exits the script, so the || log "reap failed; continuing" in cmd_up never runs.

The same blank row also makes usage send a vm ./- show request for a VM that does not exist.

Steps to reproduce

From a gstack checkout at 047dad78 (no Ubicloud account needed; a local stand-in answers POST /cli the way Ubicloud does for an empty project):

cat > /tmp/fake_ubi_empty.py <<'EOF'
# Answers every POST /cli with "\n", which is what Ubicloud returns for
# `vm list -N` on a project with no VMs.
import json, sys
from http.server import BaseHTTPRequestHandler, HTTPServer
class H(BaseHTTPRequestHandler):
    def do_POST(self):
        argv = json.loads(self.rfile.read(int(self.headers["Content-Length"])))["argv"]
        print("fake api argv:", argv, file=sys.stderr)
        self.send_response(200); self.send_header("Content-Length", "1"); self.end_headers(); self.wfile.write(b"\n")
    def log_message(self, *a): pass
HTTPServer(("127.0.0.1", 18731), H).serve_forever()
EOF
python3 /tmp/fake_ubi_empty.py & srv=$!; sleep 1
UBICLOUD_API_URL=http://127.0.0.1:18731 UBICLOUD_API_KEY=offline UBI_RUNNER_STATE=$(mktemp -d) \
  bash scripts/ubicloud/ubi-runner.sh reap; echo "exit=$?"
kill $srv

Expected: ubi-runner: reap: no expired VMs, exit 0. Actual: the two lines above, exit 1.

Proposed fix

Skip blank rows in vm_rows. With that guard, an empty project leaves cmd_usage with no per-VM files, so cat "$tmp"/* fails and usage exits 1 under set -euo pipefail; the second hunk keeps usage at exit 0 there (it prints total 0 0).

--- a/scripts/ubicloud/ubi-runner.sh
+++ b/scripts/ubicloud/ubi-runner.sh
@@ -304,4 +304,5 @@
   local loc name owner created expires
   cli vm list -N -f location,name | while read -r loc name; do
+    [ -n "$name" ] || continue
     owner=. created="" expires=-
     if [[ "$name" =~ ^${PREFIX}-([a-z][a-z0-9]{0,11})-([0-9]{10})- ]]; then
@@ -377,5 +378,5 @@
   done < <(vm_rows)
   wait
-  cat "$tmp"/* 2>/dev/null | awk -v me="$OWNER" '
+  { cat "$tmp"/* 2>/dev/null || true; } | awk -v me="$OWNER" '
     { n = $2; sub(/^.*-/, "", n); o = $1 == "-" ? "(untagged)" : $1 == "." ? "(other)" : $1
       vms[o]++; cpu[o] += n; total += n; count++ }

Verified with the stand-in above under bash 3.2.57 and 5.3.9: reap prints reap: no expired VMs and exits 0. With the usage awk fix from #3679 also applied (needed on macOS only), usage prints total 0 0 and exits 0 on an empty project, and the same table as before on a project with VMs. Suggested regression test: a fake API whose vm list body is "\n", asserting reap exits 0 and issues no destroy.

Readiness report

The bug was hit on the 1.91.333.0 install below, during a live up. There vm_rows emitted four fields, so the log read reaping expired ./- (owner ; its TTL ended 29890519 min ago). The repro above is against main at 047dad78 (1.91.357.0), where #3652 reworked vm_rows and cmd_reap but the blank-row path still fails the same way. Home paths are shortened to ~, and the paths of local CLAUDE.md files are removed from the last check line.

gstack doctor
gstack doctor 1.91.333.0
ok              install root    ~/.claude/skills/gstack: claude global current (1.91.333.0), codex global current (1.91.333.0)
ok              rescue refs     none in ~/.claude/skills/gstack
ok              state root      ~/.gstack durable=yes (selected by default)
ok              bun             1.4.4 at /opt/homebrew/bin/bun (floor 1.3.3, tested 1.4.2)
ok              hooks           9 hook(s) parse
ok              codex           codex-cli 0.163.0; self-locate ok; model gpt-6.1-sol (source: ~/.codex/config.toml model)
ok              codex probe     cached MODEL_OK, 18m ago (not re-run; --live probes now)
not configured  artifacts sync  off; cross-machine artifacts sync is unavailable
ok              browse bundle   browse/dist built
ok              binaries        find-browse, design, pdf, gstack-global-discover launch (4 of 4)
ok              cso             native helper 1.91.333.0 (11c17d466af3)
ok              claude code     2.1.303 (Claude Code) at ~/.local/bin/claude
ok              session journal largest of 2: 1.6 MiB
ok              guard log       816 decisions (61 denied)
ok              CLAUDE.md       no gstack section lists skills
14 ok, 0 warn, 1 not configured, 0 fail

Environment

  • Host: Claude Code 2.1.303
  • OS: macOS 26.6.2 (25G83), arm64; reproduced with /bin/bash 3.2.57 and Homebrew bash 5.3.9
  • gstack: hit on 1.91.333.0, reproduced on main 047dad78 (1.91.357.0)

yolo-jared avatar Oct 31 '26 09:10 yolo-jared

Fixed in #3746, merged to main as 74836e41 and released in v1.92.41.0. vm_rows now skips the blank row Ubicloud returns for a project with no VMs, so reap and up no longer try to use - as a VM name. The runner tests cover the empty-project case against the fake API.

capy-ai[bot] avatar Nov 02 '26 03:11 capy-ai[bot]