Skip to content

Docker Studio: keep unsloth-studio-update --ref from leaving Studio dead - #10826

Merged
danielhanchen merged 24 commits into
unslothai:mainfrom
oobabooga:docker-studio-update-ref
Sep 14, 2026
Merged

danielhanchen merged 24 commits into
unslothai:mainfrom
oobabooga:docker-studio-update-ref

Conversation

@oobabooga

@oobabooga oobabooga commented Sep 12, 2026 •

Copy link
Copy Markdown
Member

unsloth-studio-update --ref main, the command the helper's own --help shows, kills Studio in the container it was asked to update. Studio never comes back, not even after docker restart, and running the helper again says it cannot help. This makes the command work, and makes every failure put the previous install back instead of leaving a dead container.

Why it breaks

--ref runs pip install git+https://github.com/unslothai/unsloth@main, which replaces the editable install at /opt/unsloth-studio/src with a build in site-packages. Only release wheels carry the built frontend, so unsloth studio exits 1 on start:

[ERROR] Unsloth frontend build not found.
Tried:
  - /opt/unsloth-studio/unsloth_studio/lib/python3.12/site-packages/studio/frontend/dist

The helper only checked import studio.backend.main, which still succeeds, so it restarted Studio into that tree. supervisord retries three times and parks the program in FATAL.

Two things then make it worse:

  • Running the helper again says supervisor not managing 'studio' here; restart Studio yourself. supervisorctl status exits 3 for a program that exists but is not running, and the script read that as "not managed".
  • The venv on disk is already replaced by then, so any update that cannot start leaves the container one restart away from a dead Studio.

The fix

  • --ref now installs the same shape the image was built with instead of a wheel: the ref is fetched into a staging tree next to src, its frontend is built there with the bundled Node (npm ci, falling back to npm install, then npm run build), the oxc-validator runtime is installed, and only then is the tree swapped in and installed editable. Nothing is touched until the build succeeds.
  • The swap follows src to where it really is (readlink -f), so an image that presents it as a link into its own copy of Studio gets the update in that copy rather than a directory written over the link.
  • Before restarting, every path now checks what Studio actually needs: import studio.backend.main and a studio/frontend/dist/index.html next to the imported package. The checks run from /, so a studio directory in the caller's working directory cannot answer for the installed one.
  • A failed check restores the previous install. The helper records how unsloth and unsloth_zoo are installed before it starts (from direct_url.json: an editable path, a git commit, or a released version), puts the old source tree back, reinstalls exactly those, says so, and exits non-zero without restarting.
  • supervisorctl status exit 3 now counts as managed, so a Studio left FATAL by an earlier update gets restarted instead of a "not managing" message.
  • After a restart the helper waits for /api/health and says whether Studio came back. UNSLOTH_STUDIO_UPDATE_HEALTH_WAIT bounds the wait (default 180 seconds, 0 skips it).

Before and after

Both sides are unsloth/unsloth:latest; B carries this branch's helper. Same container recipe, no GPU.

Check A (published) B (this PR)
--ref main exit 7 after 49 s, Studio FATAL, /api/health unreachable exit 0 after 80 s, Studio RUNNING, health 200
Studio after --ref main, Playwright sign-in never loads, 0/1 5/5: sign in, pick a GGUF model, two chat turns
Code actually running after --ref main site-packages copy without a frontend /opt/unsloth-studio/src, inference.py md5 5ff793a5 to 0ad553ae (main)
studio/frontend/dist and oxc-validator/node_modules after the update dist absent in the installed tree both present
Plain update with Studio already FATAL "supervisor not managing 'studio' here", stays FATAL restarts it, health 200
Update that cannot start (--packages unsloth==2025.1.1) exit 1, venv left replaced; the next restart lands in FATAL exit 1, previous install restored, still RUNNING before and after a restart
--ref no-such-ref-xyz exit 1 exit 1, Studio untouched and RUNNING

The --ref main run costs about 80 seconds, most of it the frontend build.

Verification

  • tests/python/test_docker_update_helpers.py: 25 passed. The Studio helper's cases run the real script against a fake studio package, with pip, supervisorctl, git and the bundled npm as recording stubs, so the import check, the dist check and the rollback are exercised rather than asserted from source text. The linked-src case is covered too: the update must land in the tree the link points at, with no staging directory left in the Studio home.
  • Six of those fail on main: the dist check, the rollback, the FATAL restart, the two --ref build cases and the rollback message. Run them from outside the repository: from the repo root, main's import studio.backend.main picks up the working tree's studio/ and fails for an unrelated reason.
  • The llama.cpp updater cases in the same file are unchanged and still pass.

Not in this PR

This is the in-container helper only. unsloth studio update and the installer are unchanged.

--ref needs the bundled Node and network access for the frontend build. Without $UNSLOTH_STUDIO_HOME/node/bin/npm it refuses, rather than installing a tree Studio cannot serve.

A release-wheel update still installs into site-packages, where the wheel carries frontend/dist but no oxc-validator/node_modules, so code validation in Data Recipes degrades there as it does for any wheel install. Pre-existing, and separate from this failure.

The update still lives in the container's writable layer, so it survives docker restart and not docker rm. Pulling a new image is still the lasting path.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed this hits docker/unsloth_studio_update.sh, where the post-update check only imports studio.backend.main and the supervisorctl exit 3 path reports Studio as unmanaged, so --ref can leave it FATAL. Will get this reviewed.

@danielhanchen

Copy link
Copy Markdown
Member

Verified here with the branch's helper in two images: one built from #10829 (where /opt/unsloth-studio/src is a link into /opt/unsloth-studio-app) and one built from main (real src). Pushed two commits: the merge with main and the four Codex P1s, all of which reproduced.

The bug is real. On both main's image and the #10829 image, unsloth-studio-update --ref <sha> with the current helper installs a wheel without the built frontend and Studio parks in FATAL with Unsloth frontend build not found.

With this branch, before and after the two commits:

Case PR head Now
--ref <another sha> on both images exit 0, health 200, tree landed in the app dir (#10829 image) or src (main), frontend/dist/index.html and oxc-validator/node_modules present, no .src-* leftovers, survives docker restart same
bad ref exit 1, nothing changed same
--packages unsloth==2025.1.1 exit 1, previous install restored, RUNNING after restart same
Studio stopped first, then --ref exit 0, health 200 same, via start instead of restart
npm run build fails (stubbed) the oxc install still ran after the failure; exit 1 only because of the later dist check stops at ERROR: npm run build failed, nothing changed
supervisorctl restart fails (runner missing) WARNING and exit 0 with Studio stopped exit 1 with the status
SIGTERM during pip install -e after the swap .src-prev.<pid> left in the app dir, and the next start linked it into the Studio home restore runs from the exit trap, no leftovers, RUNNING
--with-deps then a failing package transformers stayed at the moved version after "previous install was restored" dependency snapshot reinstalled, back at the original
wheel-only venv (no src) 80 s frontend build, then mv: cannot stat .../src refused up front, nothing built

Changes: each step in the build subshell fails explicitly (errexit is ignored inside a ( ... ) || return list); [ -d "$SRC" ] before staging; a DONE flag and an exit trap that restores when the swap happened and the run did not finish, with INT and TERM mapped to exits; pip freeze --exclude-editable before a --with-deps install, reinstalled --no-deps -r on restore; start for a status-3 program, and a failed command or an unanswered health wait exits 1 with supervisorctl status. tests/python/test_docker_update_helpers.py: 30 passed (was 25), from the worktree and from outside the repo root. Full tests/python/test_docker_*.py: 510 passed, 2 skipped.

One limit: docker stop ends in SIGKILL and no trap runs, so a .src-prev.<pid> can still be left in the app dir. #10829's linker now skips .src-update.* and .src-prev.* so they never reach the Studio home.

Merge note: this branch and #10829 both touch the failure text near the end of the script and the test that pins it; whichever lands second takes this branch's version of both hunks (the header wording from #10829 is untouched here and survives).

…e path, pin the CUDA stack under --with-deps, honour the npm lockfile and registry
@danielhanchen

Copy link
Copy Markdown
Member

A second pass (d537e03) after three independent reviews of my own changes. All of it is in the same two files; 38 tests now.

  • The interrupt guarantee had two ordering holes: SWAPPED=1 was set after both renames and DONE=1 after the previous tree was deleted, so a signal in either window ran the wrong branch. Both flags now move to where they belong, restore reports a failed move instead of dying silently under set -e, and it cannot run twice.
  • A container killed between the two renames leaves no src in the app dir and only a .src-prev.* copy. The updater now puts that copy back before doing anything, and Docker Studio: keep Studio's data on a volume without pinning its code #10829's linker does the same at container start (bb67d48 there), so a docker stop mid-update no longer strands the install. .src-prev names come from mktemp, and both renames use mv -T, so a stale directory can never swallow the tree.
  • The editable install goes through /opt/unsloth-studio/src when that entry exists, so direct_url.json records the home path and unsloth-studio-home --restore keeps working after an update.
  • --with-deps installs the studio extra (the backend requirement set was never requested before) under a constraints file that pins torch, triton, the nvidia-* wheels, xformers and bitsandbytes from pip freeze, so pip cannot re-resolve the CUDA stack into the base venv. The default --no-deps path is unchanged.
  • npm: UNSLOTH_NPM_REGISTRY is honoured as in the installer, a system npm is used when the bundled one is missing, and with a lockfile present a failed npm ci is an error rather than a silent npm install.
  • The health wait value is validated, --help stops at the header, and the header carries Docker Studio: keep Studio's data on a volume without pinning its code #10829's persistence wording so the merge conflict there resolves to this branch.

Rows re-run on the image built from #10829: --ref to another sha (exit 0, health 200, home path in direct_url.json), a failing package (restored, running), SIGTERM after the swap (restored, no leftovers), and the killed-between-renames state (recovered on the next run).

…and never restore twice or cut the cleanup short
…ne updater at a time

- an exclusive flock on <src dir>/.src-update.lock for the whole run; a second
  updater exits 1 and changes nothing. The name shares the scratch prefix so the
  home linker never links it, and the startup sweep skips it.
- the previous source tree and pins stay until the restarted service answers
  /api/health. A failed restart or a health timeout restores them, starts the
  previous install again and exits 1. HEALTH_WAIT=0 commits once the restart
  command succeeds; --no-restart and an unmanaged Studio commit after the import
  and frontend checks, as before.
- restore reinstalls the recorded package identity with --force-reinstall, since
  pip takes a same-version editable tree as already satisfying the pin.
- the oxc validator runtime follows the frontend's lockfile rule: npm ci when a
  package-lock.json ships with the ref, npm install otherwise, both with the
  registry override.
- the rollback line for an editable install keeps the file:// URI pip recorded;
  the bare, percent-encoded path is not a valid editable requirement.

Tests: lock refusal, restart failure and health timeout put the previous tree
back and start it, wait 0 commits, force-reinstall argv, oxc npm ci with and
without a lockfile, the editable URI in the rollback file.
@danielhanchen

Copy link
Copy Markdown
Member

Pushed d9fc1da on top of the earlier fixes. What changed in unsloth_studio_update.sh:

  • One updater at a time: the run takes flock on <src dir>/.src-update.lock (fd 9) and exits 1 if another update holds it. flock missing is an error, not a silent unlocked run.
  • The previous tree now stays on disk until Studio answers on port 8000 after the swap. A failed supervisorctl restart or a health timeout rolls back to the previous tree and dependency set and exits 1. This is a contract change: before, those cases exited 0 with the new tree in place. UNSLOTH_STUDIO_UPDATE_HEALTH_WAIT=0 keeps the old behaviour (commit right after the swap, no wait).
  • Rollback reinstalls the snapshot with pip install --no-deps --force-reinstall, since pip treats a same-version editable tree as already satisfied and would otherwise leave the new tree registered.
  • The oxc validator uses npm ci when a lockfile exists and fails loudly on lockfile drift instead of falling back to npm install.
  • The editable install keeps the file:// URI through the Studio home path, so direct_url.json matches what the linker in Docker Studio: keep Studio's data on a volume without pinning its code #10829 expects.

Verification: the 43 unit tests in tests/python/test_docker_update_helpers.py pass from outside the repo root, and the fault-injection harness (real script, stubbed git/npm/pip/supervisorctl/curl, failure or INT/TERM at every call index, linked and real src, stale scratch, --with-deps) passes 1389 of 1389 cases with the invariant "exit 0 with the new tree importable, or non-zero with the previous tree back and no .src-* scratch left".

@danielhanchen

Copy link
Copy Markdown
Member

@codex security review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 14, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-14T07:53:12.365262Z 8f68dc2 Manual request
🔒 Security Review ✅ Completed 2026-09-14T03:04:58.130095Z d9fc1da Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: d9fc1da7ac

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

…mmitted, and read the install record from the venv not the cwd
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 64bab1afb5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +26 to +29
# Persistence: the updated packages live in the image's copy of Studio, so they
# survive `docker restart` but not `docker rm`; pull a new image for a lasting update.
# Studio's data (accounts, chats, models) is separate: keep it with
# -v unsloth-studio:/opt/unsloth-studio, which never pins Studio's code.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Correct the Studio-home volume persistence guidance

The documented volume mount does pin Studio's code: docker/Dockerfile.studio places both the venv and editable source beneath /opt/unsloth-studio (notably lines 55 and 68-100), and Docker initializes a new named volume from that directory before the volume hides later images' copies. Consequently updates made here survive docker rm, while pulling a new image with the same volume does not provide the claimed lasting code update and can retain an old venv against a new base image; document that this mount persists code or move persistent data to a separate mount path first.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wording describes the layout this PR lands on. #10829 merges before it and moves Studio's code out of the home into /opt/unsloth-studio-app, linked into the home at start, so a volume at /opt/unsloth-studio then carries data only and never pins code; the failure text and its test in this PR already assume that layout. On today's main the volume does pin code, which is exactly what #10829 fixes, and the merge order is set so this comment is true from the moment it exists on main.

…k, and stop when the dependency snapshot fails
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…illed run's restore can be finished, and say when a restore did not finish
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…us one is back, and say when the recovery finished
# Conflicts:
#	docker/unsloth_studio_update.sh
#	tests/python/test_docker_update_helpers.py
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@danielhanchen

Copy link
Copy Markdown
Member

The update now rolls back and exits 1 when the health wait goes unanswered, where it previously exited 0 with the new tree in place; could you confirm the 180 second default is enough on a slow container so a good update is not undone?

…ck on the restored install; the test site carries its own package metadata
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 04081f503f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +449 to +450
SWAPPED=1
mv -T "$SRC" "$PREV_SRC"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep the old source active until the service restart

During every --ref update while Studio is running, this rename replaces the path already stored in the process's sys.path/studio.__path__ before the subsequent editable and zoo installation completes. Any request that lazily imports a Studio module during that interval can therefore load new source against the old or partially-mutated environment; with --no-restart, that mixed-version risk persists until the operator restarts manually. Stop the service before changing its import path, or keep the old path active until the new environment is ready to switch atomically.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The window is the two renames plus the editable reinstall and the import checks, and it is there by design: the fetch, the frontend build and the zoo install all run before the swap so that what remains after it is short, and the default path restarts Studio as soon as the new tree imports. Stopping the service before the swap would turn every update into an outage for that whole tail, and a request that lazily imports during those seconds loads a module from a complete tree the process is about to restart on: the swap is two renames of whole trees, never a half-mutated one. With --no-restart the operator has chosen to restart at a time of their own, and the script says so in its last lines. Not changing this.

…and the container start runs it before Studio comes up
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 14, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 14, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 14, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 53b3ac0c4d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 8f68dc293e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants