docker: close five failure paths the review found

Notebook sync, in-place publish. entrypoint.sh runs sync_notebooks and then
execs the container command, so the detached refresh child is still copying
while JupyterLab serves the same tree. cp -a writes through the destination
inode, so a reader can catch half-written JSON and a save made after the
recorded-hash check is destroyed and then recorded as pristine. Publish through
a same-dir dot-prefixed temp plus an atomic rename, and re-read the hash once
the staging copy is complete (the earlier check sits before middle_unchanged, a
python subprocess, so the window was most of the loop). A single-file bind mount
cannot be renamed over, so that path falls back to the previous copy.

Notebook sync, first boot. A pre-existing file whose bytes already match the
baked template fell through to cp -a, which is --preserve=all: as root that
stamps root:root, the baked mode and the build mtime onto a bind-mounted host
file and locks its owner out of editing it. Record it as managed instead. The
hash is identical, so the state file is byte-for-byte what the copy wrote.

unsloth-studio-update. The post-update import check only warned, then the
default restart replaced a process that was serving fine with one known not to
import. supervisord retries startretries times, lands in FATAL and never leaves
it on its own, so the container serves nothing until someone execs in. Keep the
running service and exit non-zero with the remedy.

unsloth-llama-update --check. resolve_latest swallows every failure into an
empty string, which fell into the "up to date" branch and exited 0, so the
command reported a state it could not observe. Report UNKNOWN and fail.

unsloth-llama-update rollback. The in-place restore iterates the backup's
entries, so a file the new release introduced survives it and the restored tree
is mixed-version; ggml dlopens every libggml-*.so next to the binaries. Clear
the install dir before restoring, gated on the drain having completed, because
before that an entry there can still be the only copy of an old file.

docker-publish ref freeze. git ls-remote exits 0 whether or not a ref matched,
so a non-zero exit means the remote was never reached. That exit was lost twice
over: first element of a pipeline, and a run step with no explicit shell runs
under bash -e without pipefail. The step exited 0 and published ref=main, which
the amd64, arm64 and Studio builds each resolve again, so one multi-arch tag
could carry different revisions. Fail the prepare job instead, keeping the
passthrough for the reachable-but-no-match case it was written for.

Jupyter output select. lastPointerOutput was only replaced by another
pointer-down, but J/K/arrow cell navigation fires none, so Ctrl/Cmd+A on a later
cell selected the previously clicked output and suppressed notebook:select-all;
after a re-run the node is detached and the chord did nothing at all. Revalidate
the remembered output (still in the document, still in the active cell) before
using it as the fallback.

Tests: four static guards in test_docker_nb_sync_race.py, a new behavioural
test_docker_update_helpers.py driving both helpers with stub pip, supervisorctl
and mv, a new test_docker_publish_ref_freeze.py that executes each resolver step
under bash -e with a failing ls-remote, and a source check in
validate_studio_features.py. Each fails against the code before this change; the
interrupted-drain case also fails against the unconditional form of the rollback
fix.
This commit is contained in:
Daniel Han 2026-07-27 14:02:16 +00:00
commit 837b09122e
9 changed files with 558 additions and 13 deletions

View file

@ -135,3 +135,59 @@ def test_the_lock_lives_beside_the_state_it_protects(sync: str):
"keeping the lock in $DEST also serialises two containers sharing the "
"notebooks volume, which /tmp would not"
)
# --- concurrent-publish safety ------------------------------------------------
# The detach above is deliberate, but entrypoint.sh runs `sync_notebooks` and then
# `exec "$@"`, so the child is still copying while JupyterLab serves the same tree.
# `cp -a` writes THROUGH the destination inode, so it both exposes half-written
# JSON to a reader and destroys a save made after the recorded-hash check. The
# publish therefore has to go via a same-dir temp plus an atomic rename.
def test_the_refresh_publishes_each_notebook_atomically(sync: str):
block = sync[sync.index("while IFS= read -r -d '' f; do") :]
block = block[: block.index("done < <(find")]
assert re.search(r'cp -a "\$f" "\$new"', block), (
"the refresh must copy into a staging file, not onto the live notebook"
)
assert re.search(r'mv -f "\$new" "\$dst"', block), (
"the staged copy must be published with an atomic rename"
)
def test_the_staging_file_is_hidden_and_beside_the_destination(sync: str):
assert re.search(r'new="\$\(dirname "\$dst"\)/\.unsloth_nb_new\.\$\$"', sync), (
"the staging file must be dot-prefixed (invisible in the file browser), "
"per-PID (two containers on one volume) and in the destination directory "
"(a rename cannot cross filesystems)"
)
def test_the_recorded_hash_is_rechecked_immediately_before_publishing(sync: str):
block = sync[sync.index("while IFS= read -r -d '' f; do") :]
block = block[: block.index("done < <(find")]
recheck = block.index('cp -a "$f" "$new"')
assert re.search(
r'if \[ -e "\$dst" \] && \[ "\$\(hash_of "\$dst"\)" != "\$\{LAST\[\$rel\]:-\}" \]',
block[recheck:],
), (
"the earlier check sits before middle_unchanged (a python subprocess), so "
"the hash has to be re-read once the staging copy is complete or a save "
"made in between is silently overwritten"
)
def test_a_pristine_pre_existing_file_is_not_rewritten_on_first_boot(sync: str):
block = sync[sync.index('if [ ! -f "$STATE" ]; then') :]
block = block[: block.index('mv "$STATE.tmp" "$STATE"')]
assert "kept existing user file" in block
# A bind-mounted file whose bytes already match the template used to fall
# through to `cp -a`, i.e. --preserve=all stamping root:root, the baked mode
# and the build mtime onto the host user's own file. Record, don't copy.
same = block.index("kept existing user file")
tail = block[same:]
assert tail.index("$STATE.tmp") < tail.index('cp -a "$TEMPLATE/$rel"'), (
"an existing file with the template's exact bytes must be recorded as "
"managed without being copied over"
)

View file

@ -0,0 +1,132 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-Present the Unsloth team. See /studio/LICENSE.AGPL-3.0
"""The docker publish workflow must never forward an unfrozen ref.
`prepare` resolves unsloth, unsloth-zoo and notebooks to ONE commit each so the
amd64 leg, the arm64 leg and the Studio build all bake identical source; that is
the whole reason the job exists. Each resolver was
SHA="$(git ls-remote <repo> "$REF" | awk 'NR==1{print $1}')"
[ -n "$SHA" ] || SHA="$REF"
`git ls-remote` exits 0 whether or not a ref matched, so a non-zero exit means
the remote was never reached. That exit was lost twice over: it is the first
element of a pipeline, and a `run:` step with no explicit `shell:` runs under
`bash -e` WITHOUT pipefail, so the step exited 0 and published `ref=main`. Each
build then resolved `main` independently, and a branch advance between them
would ship one multi-arch tag containing different revisions. The stable-tag
gates key off the inputs, not off whether resolution worked, so `:latest` would
still be moved onto it.
Static plus behavioural: the resolver `run:` blocks are executed under `bash -e`
with a `git` stub. No docker, no network.
"""
from __future__ import annotations
import os
import re
import shutil
import subprocess
from pathlib import Path
import pytest
import yaml
REPO_ROOT = Path(__file__).resolve().parents[2]
WORKFLOW = REPO_ROOT / ".github" / "workflows" / "docker-publish.yml"
RESOLVER_STEPS = ("unsloth_ref", "zoo_ref", "notebooks")
pytestmark = pytest.mark.skipif(
shutil.which("bash") is None, reason = "needs bash",
)
@pytest.fixture(scope = "module")
def steps() -> dict:
assert WORKFLOW.is_file(), f"missing {WORKFLOW}"
doc = yaml.safe_load(WORKFLOW.read_text(encoding = "utf-8"))
found = {}
for step in doc["jobs"]["prepare"]["steps"]:
if step.get("id") in RESOLVER_STEPS:
found[step["id"]] = step["run"]
missing = set(RESOLVER_STEPS) - set(found)
assert not missing, f"resolver steps missing from the prepare job: {missing}"
return found
def test_the_workflow_never_pins_a_shell_so_bash_e_has_no_pipefail(steps: dict):
# If someone later adds `shell: bash` the runner switches to
# `bash --noprofile --norc -eo pipefail`, which would make the guards below
# redundant rather than wrong -- but until then they are the only protection.
doc = yaml.safe_load(WORKFLOW.read_text(encoding = "utf-8"))
assert "shell" not in doc.get("defaults", {}).get("run", {}), (
"this test models the default `bash -e` shell; update it if a default "
"shell with pipefail is introduced"
)
@pytest.mark.parametrize("step_id", RESOLVER_STEPS)
def test_an_unreachable_remote_fails_the_step(steps: dict, step_id: str, tmp_path: Path):
script = _expand(steps[step_id])
res = _run_with_failing_ls_remote(script, tmp_path)
assert res.returncode != 0, (
"a transport failure must fail the prepare job, not fall through to the "
f"mutable ref:\nstdout={res.stdout}\nstderr={res.stderr}"
)
@pytest.mark.parametrize("step_id", RESOLVER_STEPS)
def test_an_unreachable_remote_never_emits_a_mutable_ref(
steps: dict, step_id: str, tmp_path: Path,
):
script = _expand(steps[step_id])
res = _run_with_failing_ls_remote(script, tmp_path)
emitted = (tmp_path / "github_output").read_text(encoding = "utf-8") \
if (tmp_path / "github_output").exists() else ""
for line in emitted.splitlines():
key, _, value = line.partition("=")
assert re.fullmatch(r"[0-9a-f]{40}", value), (
f"{step_id} published {key}={value!r}, which the three builds each "
"resolve again, so they can bake different revisions"
)
assert res.returncode != 0
def _expand(run: str) -> str:
"""Replace the `${{ ... }}` expressions with the empty string the default
(push to main, no dispatch inputs) trigger produces."""
return re.sub(r"\$\{\{[^}]*\}\}", "", run)
def _run_with_failing_ls_remote(script: str, tmp_path: Path):
bin_dir = tmp_path / "bin"
bin_dir.mkdir(parents = True, exist_ok = True)
stub = bin_dir / "git"
stub.write_text(
"#!/usr/bin/env bash\n"
'if [ "$1" = "ls-remote" ]; then\n'
' echo "fatal: unable to access: Could not resolve host" >&2\n'
" exit 128\n"
"fi\n"
"exit 0\n",
encoding = "utf-8",
)
stub.chmod(0o755)
out = tmp_path / "github_output"
out.write_text("", encoding = "utf-8")
env = dict(os.environ)
env["PATH"] = f"{bin_dir}{os.pathsep}" + env["PATH"]
env["GITHUB_OUTPUT"] = str(out)
# Whatever the expansions above blanked out; the resolvers default to "main".
for name in ("INPUT_REF", "TAG_REF", "PUSH_SHA"):
env[name] = ""
path = tmp_path / "step.sh"
path.write_text(script, encoding = "utf-8")
# Exactly how the runner invokes a `run:` step with no explicit `shell:`.
return subprocess.run(
["bash", "-e", str(path)],
capture_output = True, text = True, env = env, timeout = 60,
)

View file

@ -0,0 +1,250 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-Present the Unsloth team. See /studio/LICENSE.AGPL-3.0
"""Behavioural guards for the two in-container update helpers of the Docker image.
Both are `docker exec` entry points that mutate a running container, so a wrong
answer costs an outage or a mixed-version install:
* `unsloth-studio-update` swaps the Studio Python packages and then restarts the
service. It verifies the new backend imports first, but only warned -- so a
release that pulls in a dependency `--no-deps` did not install got the healthy
old process killed and replaced by one that cannot start. supervisord retries
`startretries` times, lands in FATAL and never leaves it on its own, so the
container serves nothing until someone exec's in.
* `unsloth-llama-update --check` reported "up to date" when it could not reach
the release feed at all, and its in-place rollback only removed entries whose
names the OLD tree also had, leaving new-release-only shared objects beside
the restored files. ggml dlopen()s every `libggml-*.so` next to the binaries,
so that mix is loaded on the next GGUF run.
These drive the real scripts with stub `pip` / `supervisorctl` / `python` /
`mv` on PATH. No docker, no GPU, no network.
"""
from __future__ import annotations
import os
import shutil
import subprocess
from pathlib import Path
import pytest
REPO_ROOT = Path(__file__).resolve().parents[2]
STUDIO_UPDATE = REPO_ROOT / "docker" / "unsloth_studio_update.sh"
LLAMA_UPDATE = REPO_ROOT / "docker" / "unsloth_llama_update.sh"
pytestmark = pytest.mark.skipif(
shutil.which("bash") is None, reason = "needs bash",
)
def _stub(directory: Path, name: str, body: str) -> None:
directory.mkdir(parents = True, exist_ok = True)
path = directory / name
path.write_text("#!/usr/bin/env bash\n" + body, encoding = "utf-8")
path.chmod(0o755)
def _run(script: Path, args, env, cwd = None):
return subprocess.run(
["bash", str(script), *args],
capture_output = True, text = True, env = env, cwd = cwd, timeout = 120,
)
# --- unsloth-studio-update ----------------------------------------------------
def _studio_env(tmp_path: Path, *, import_ok: bool) -> dict:
home = tmp_path / "studio"
venv_bin = home / "unsloth_studio" / "bin"
venv_bin.mkdir(parents = True)
_stub(
venv_bin, "python",
'if [ "$1" = "-c" ]; then\n'
+ (" exit 0\n" if import_ok else ' case "$2" in *studio.backend.main*) exit 1;; esac\n exit 0\n')
+ 'fi\n'
'if [ "$1" = "-m" ] && [ "$2" = "pip" ]; then\n'
' if [ "$3" = "show" ]; then echo "Version: 2026.7.5"; exit 0; fi\n'
' echo "STUB-PIP $*" >> "$STUB_LOG"; exit 0\n'
'fi\n'
'exit 0\n',
)
bin_dir = tmp_path / "bin"
_stub(bin_dir, "supervisorctl",
'echo "STUB-SUPERVISORCTL $*" >> "$STUB_LOG"\n'
'if [ "$1" = "status" ]; then exit 0; fi\nexit 0\n')
env = dict(os.environ)
env["PATH"] = f"{bin_dir}{os.pathsep}" + env["PATH"]
env["UNSLOTH_STUDIO_HOME"] = str(home)
env["STUB_LOG"] = str(tmp_path / "calls.log")
return env
def test_studio_update_restarts_when_the_backend_imports(tmp_path: Path):
env = _studio_env(tmp_path, import_ok = True)
res = _run(STUDIO_UPDATE, [], env)
calls = Path(env["STUB_LOG"]).read_text() if Path(env["STUB_LOG"]).exists() else ""
assert res.returncode == 0, res.stderr
assert "STUB-SUPERVISORCTL restart studio" in calls, calls
def test_studio_update_does_not_restart_into_a_backend_that_cannot_import(tmp_path: Path):
env = _studio_env(tmp_path, import_ok = False)
res = _run(STUDIO_UPDATE, [], env)
calls = Path(env["STUB_LOG"]).read_text() if Path(env["STUB_LOG"]).exists() else ""
assert "STUB-SUPERVISORCTL restart studio" not in calls, (
"restarting into code that cannot import kills a process that is serving "
"fine and parks supervisord's studio program in FATAL:\n" + calls
)
assert res.returncode != 0, "a broken update must not report success"
assert "--with-deps" in res.stderr, "the remedy must still be printed"
# --- unsloth-llama-update -----------------------------------------------------
def _llama_env(tmp_path: Path, *, latest: str | None) -> dict:
install = tmp_path / "llama.cpp"
install.mkdir(parents = True)
(install / "UNSLOTH_PREBUILT_INFO.json").write_text(
'{"tag": "b1111-old"}\n', encoding = "utf-8",
)
fetcher = tmp_path / "fetch_llama_prebuilt.py"
resolve = (
" raise RuntimeError('unreachable')\n" if latest is None
else f" return {latest!r}\n"
)
fetcher.write_text(
"def resolve_latest_tag(repo):\n" + resolve, encoding = "utf-8",
)
env = dict(os.environ)
env["UNSLOTH_LLAMA_CPP_PATH"] = str(install)
env["UNSLOTH_LLAMA_FETCHER"] = str(fetcher)
return env
def _llama_check(tmp_path: Path, latest):
env = _llama_env(tmp_path, latest = latest)
return _run(LLAMA_UPDATE, ["--check"], env)
def test_llama_check_reports_an_available_update(tmp_path: Path):
res = _llama_check(tmp_path, "b2222-new")
assert res.returncode == 0, res.stderr
assert "an update is available" in res.stdout
def test_llama_check_reports_up_to_date(tmp_path: Path):
res = _llama_check(tmp_path, "b1111-old")
assert res.returncode == 0, res.stderr
assert "up to date" in res.stdout
def test_llama_check_does_not_claim_up_to_date_when_it_could_not_look(tmp_path: Path):
res = _llama_check(tmp_path, None)
assert "up to date" not in res.stdout, (
"--check exists to report update status; saying 'up to date' for a lookup "
"that never happened is the one answer it must never give:\n" + res.stdout
)
assert res.returncode != 0, "an unperformed check must not exit 0"
assert "UNKNOWN" in res.stdout + res.stderr
def _llama_inplace_env(tmp_path: Path, old: list[str], new: list[str]) -> dict:
"""An in-place (volume-mounted) install whose activation fails part-way."""
install = tmp_path / "llama.cpp"
install.mkdir(parents = True)
for name in old:
(install / name).write_text("OLD\n", encoding = "utf-8")
(install / "UNSLOTH_PREBUILT_INFO.json").write_text(
'{"tag": "b1111-old"}\n', encoding = "utf-8",
)
fetcher = tmp_path / "fetch_llama_prebuilt.py"
fetcher.write_text(
"import os, sys\n"
"def resolve_latest_tag(repo):\n"
" return 'b2222-new'\n"
"if __name__ == '__main__':\n"
" dest = sys.argv[3]\n"
" os.makedirs(dest, exist_ok = True)\n"
f" for name in {new!r}:\n"
" open(os.path.join(dest, name), 'w').write('NEW\\n')\n"
" open(os.path.join(dest, 'UNSLOTH_PREBUILT_INFO.json'), 'w')"
".write('{\"tag\": \"b2222-new\"}\\n')\n",
encoding = "utf-8",
)
# Fail the ACTIVATION move (-t <install dir>) AFTER it has moved the files, so
# the install dir is populated with the new tree and `find` still reports the
# failure -- the mid-swap abort the rollback exists for. The drain
# (-t <backup>) and the rollback's own per-file moves must keep working, so
# only that one invocation is broken.
bin_dir = tmp_path / "bin"
_stub(
bin_dir, "mv",
'if [ "$1" = "-t" ] && [ "$2" = "$FAIL_MV_TARGET" ]; then\n'
' shift 2\n'
' for _s in "$@"; do /bin/mv "$_s" "$FAIL_MV_TARGET/"; done\n'
' exit 1\n'
'fi\n'
'exec /bin/mv "$@"\n',
)
env = dict(os.environ)
env["PATH"] = f"{bin_dir}{os.pathsep}" + env["PATH"]
env["UNSLOTH_LLAMA_CPP_PATH"] = str(install)
env["UNSLOTH_LLAMA_FETCHER"] = str(fetcher)
env["UNSLOTH_LLAMA_UPDATE_IN_PLACE"] = "1"
env["FAIL_MV_TARGET"] = str(install)
return env
def test_llama_rollback_leaves_no_new_release_files_behind(tmp_path: Path):
# "libggml-hexagon.so" exists only in the new release, so the rollback loop --
# which iterates the BACKUP's entries -- cannot see it. ggml dlopen()s every
# libggml-*.so sitting next to the binaries, so a leftover is loaded against
# the restored older libggml-base.so.
old = ["libggml-base.so", "libggml-cpu-icelake.so", "llama-cli"]
new = ["libggml-base.so", "libggml-cpu-icelake.so", "llama-cli",
"libggml-hexagon.so", "llama-mtmd-cli"]
env = _llama_inplace_env(tmp_path, old, new)
res = _run(LLAMA_UPDATE, [], env)
assert res.returncode != 0, "a failed swap must not report success"
install = tmp_path / "llama.cpp"
present = sorted(p.name for p in install.iterdir())
leftovers = [n for n in ("libggml-hexagon.so", "llama-mtmd-cli") if n in present]
assert not leftovers, (
f"new-release-only files survived the rollback: {leftovers} in {present}"
)
for name in old:
assert (install / name).read_text() == "OLD\n", (
f"{name} was not restored from the backup: {present}"
)
def test_llama_rollback_keeps_every_old_file_when_the_drain_is_interrupted(tmp_path: Path):
# The mirror image: abort while the OLD tree is still being moved into the
# backup. The entries left in the install dir are then the only copy of those
# old files, so clearing the directory before restoring would destroy them.
old = ["libggml-base.so", "libggml-cpu-icelake.so", "llama-cli", "llama-quantize"]
env = _llama_inplace_env(tmp_path, old, old)
install = tmp_path / "llama.cpp"
# Fail the DRAIN (-t <install dir>/.old.<pid>) after moving only the first
# source, so half the old tree is still sitting in the install dir when the
# rollback runs. Those entries are then the only copy there is.
_stub(
tmp_path / "bin", "mv",
'case "${1:-}:${2:-}" in\n'
' -t:*/.old.*)\n'
' _t="$2"; shift 2\n'
' [ $# -gt 0 ] && /bin/mv "$1" "$_t/"\n'
' exit 1;;\n'
'esac\n'
'exec /bin/mv "$@"\n',
)
res = _run(LLAMA_UPDATE, [], env)
assert res.returncode != 0
survivors = sorted(p.name for p in install.rglob("*") if p.is_file())
for name in old:
assert name in survivors, (
f"{name} was lost during an interrupted drain: {survivors}"
)