fix: address Codex review on WoA deferral, uninstall port-kill, and build preservation

Three valid findings from the 06-22 Codex review:

1. provision_llama_cuda.sh: when $LLAMA_DIR holds a .git checkout (a prior CPU
   source build), the whole-dir backup was skipped, so a failed CUDA rebuild's
   'rm -rf build' destroyed the working CPU server with nothing to restore --
   leaving NO llama-server despite the 'keeps the existing server' promise (a
   thermal shutdown mid-build is a real failure mode on this hardware). Back up
   build/bin before the rebuild and restore it on total failure; idempotent and
   self-cleaning (never overwrites a freshly built server). Verified both paths.

2. uninstall.ps1: 'fuser -k 8888/tcp' killed ANY listener on 8888 (Jupyter et al.
   default to it), not just Studio. Now only kills a PID whose /proc/cmdline is
   under /root/.unsloth -- matching the adjacent pkill scoping.

3. setup.sh: the 'defer to background CUDA build' branch fired even on a direct
   in-WSL 'unsloth studio update', where install.ps1 never launched a background
   builder -- so the footer claimed a build was running while nothing built. Gate
   it on UNSLOTH_WSL_LLAMA_DEFERRED=1 (set only by install.ps1, and already read
   elsewhere in setup.sh); a direct run now falls through to a real CPU build.

bash -n + PS parse clean; the common install.ps1 WoA path (prebuilt success,
deferred flag set) is unaffected.
This commit is contained in:
Daniel Han 2026-06-21 22:24:41 -07:00
commit fa44cb8c44
3 changed files with 45 additions and 13 deletions

View file

@ -474,12 +474,13 @@ function Uninstall-UnslothStudio {
# Probe candidates by exit code ('' = default distro) since `wsl --list` emits UTF-16 PS
# mis-parses. rm runs FIRST (the kills could SIGKILL this shell) and drops the dangling
# /root/.local/bin/unsloth symlink. Scope STRICTLY to /root (the fallback's install dir);
# /home/*/.unsloth may be another user's. The 8888 kill is gated on an Unsloth install
# existing (checked BEFORE rm deletes the marker) so an unrelated listener survives. pkill
# matches argv containing /root/.unsloth/ (not bare names that would hit a user's own
# llama-server); the backslash + [h]-bracket in '/root/\.unslot[h]/' keep it from matching
# this command's own argv.
$_clean = '_had=0; if [ -d /root/.unsloth ] || [ -L /root/.local/bin/unsloth ]; then _had=1; fi; rm -rf /root/.unsloth /root/llama-cuda /root/provision_llama_cuda.sh /root/llama_cuda_build.log 2>/dev/null; rm -f /root/.local/bin/unsloth 2>/dev/null; if [ $_had -eq 1 ]; then fuser -k 8888/tcp 2>/dev/null; fi; pkill -9 -f ''/root/\.unslot[h]/'' 2>/dev/null; true'
# /home/*/.unsloth may be another user's. The 8888 kill only targets a listener whose
# process cmdline is under /root/.unsloth (Studio's bind), so an unrelated service on 8888
# -- Jupyter et al. default to it -- is NOT killed; it's also gated on an Unsloth install
# having existed (checked BEFORE rm deletes the marker). pkill matches argv containing
# /root/.unsloth/ (not bare names that would hit a user's own llama-server); the backslash
# + [h]-bracket in '/root/\.unslot[h]/' keep it from matching this command's own argv.
$_clean = '_had=0; if [ -d /root/.unsloth ] || [ -L /root/.local/bin/unsloth ]; then _had=1; fi; rm -rf /root/.unsloth /root/llama-cuda /root/provision_llama_cuda.sh /root/llama_cuda_build.log 2>/dev/null; rm -f /root/.local/bin/unsloth 2>/dev/null; if [ $_had -eq 1 ]; then for _p in $(fuser 8888/tcp 2>/dev/null); do grep -qa /root/\.unsloth/ /proc/$_p/cmdline 2>/dev/null && kill -9 $_p 2>/dev/null; done; fi; pkill -9 -f ''/root/\.unslot[h]/'' 2>/dev/null; true'
# Clean only distros with evidence of a fallback install: the wsl-distro.txt marker or an
# explicit UNSLOTH_WSL_DISTRO. The broad candidate probe is only for legacy marker-less
# installs (ARM64 only); on x86 it would delete distros this installer never touched