Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: run diffusion dataset upload file work on a worker thread (#16361)
Daniel Hand148c97869f7
diff --git a/studio/backend/routes/training.py b/studio/backend/routes/training.pyindex 7910c00691b..308911993e5 100644--- a/studio/backend/routes/training.py+++ b/studio/backend/routes/training.py@@ -4326,11 +4326,6 @@ async def upload_diffusion_dataset(_interlock: None = Depends(diffusion_dataset_interlock),):"""repeat uploads accumulate in one Unsloth dataset folder; pass its name as ``data_dir`` to /diffusion/start."""- import os- import tempfile-- from utils.upload_limits import get_upload_limit_bytes, get_upload_limit_label-_require_diffusion_dataset_mutable()cleaned = _clean_diffusion_dataset_name(name)if cleaned.casefold() in _reserved_diffusion_dataset_names():@@ -4349,8 +4344,63 @@ async def upload_diffusion_dataset("then upload again."),)+ return await _run_holding_import_lock(+ _lock, _store_diffusion_dataset_upload, folder, cleaned, files, create_only+ )+++async def _run_holding_import_lock(_lock: "threading.Lock", fn, *args):+ """Runs ``fn(*args, _lock, cancelled)`` on a worker that releases ``_lock``, so a cancel never frees it mid-import."""+ cancelled = threading.Event()+ guard = threading.Lock()+ started = False++ def run():+ nonlocal started+ with guard:+ if cancelled.is_set():+ return None+ started = True+ return fn(*args, _lock, cancelled)++ worker = asyncio.ensure_future(asyncio.to_thread(run))+ try:+ return await asyncio.shield(worker)+ except BaseException:+ with guard:+ cancelled.set()+ if not started:+ _lock.release()+ raise+ # Keep the request (interlock, open uploads) alive until the worker stops at its next chunk read.+ while not worker.done():+ try:+ await asyncio.shield(worker)+ except BaseException:+ pass+ raise+++def _read_upload_chunk(f: UploadFile, cancelled: "threading.Event") -> bytes:+ """A cancelled request stops staging at the next chunk."""+ if cancelled.is_set():+ raise asyncio.CancelledError()+ return f.file.read(1024 * 1024)+++def _store_diffusion_dataset_upload(+ folder: Path,+ cleaned: str,+ files: list[UploadFile],+ create_only: bool,+ _lock: "threading.Lock",+ cancelled: "threading.Event",+) -> "DiffusionDatasetUploadResponse":+ """Validates, stages and commits one upload slice; called with ``_lock`` held and releases it."""created_folder = Falsetry:+ from utils.upload_limits import get_upload_limit_bytes, get_upload_limit_label+if create_only:occupied = (next(@@ -4495,7 +4545,7 @@ async def upload_diffusion_dataset(tmp = folder / f".upload-{_uuid.uuid4().hex}.part"staged.append((tmp, dest))with open(tmp, "wb") as out:- while chunk := await f.read(1024 * 1024):+ while chunk := _read_upload_chunk(f, cancelled):total_bytes += len(chunk)if total_bytes > limit_bytes:raise HTTPException(diff --git a/studio/backend/tests/test_diffusion_dataset_upload_worker_thread.py b/studio/backend/tests/test_diffusion_dataset_upload_worker_thread.pynew file mode 100644index 00000000000..0bfcb5f78b0--- /dev/null+++ b/studio/backend/tests/test_diffusion_dataset_upload_worker_thread.py@@ -0,0 +1,186 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""The dataset upload's file work runs on a worker thread, which owns the import lock until it ends."""++from __future__ import annotations++import asyncio+import io+import threading+import time+from concurrent.futures import ThreadPoolExecutor++import httpx+import pytest+from fastapi import FastAPI+from PIL import Image++import routes.training as training+from auth.authentication import get_current_subject+++def _png_bytes() -> bytes:+ buf = io.BytesIO()+ Image.new("RGB", (4, 4), (10, 20, 30)).save(buf, format = "PNG")+ return buf.getvalue()+++def app():+ app = FastAPI()+ app.include_router(training.router, prefix = "/api/train")+ app.dependency_overrides[get_current_subject] = lambda: "test-user"++ @app.get("/tick")+ async def tick():+ return {"ok": True}++ return app+++def ds_root(monkeypatch, tmp_path):+ import utils.paths as up++ root = tmp_path / "assets" / "datasets"+ root.mkdir(parents = True)+ monkeypatch.setattr(up, "datasets_root", lambda: root)+ return root+++def _parts(names):+ png = _png_bytes()+ return [+ ("files", (n, b"a caption" if n.endswith(".txt") else png, "application/octet-stream"))+ for n in names+ ]+++def test_other_requests_are_served_while_an_upload_works(app, ds_root, monkeypatch):+ real_summary = training._diffusion_dataset_summary++ def slow_summary(folder):+ time.sleep(0.4)+ return real_summary(folder)++ monkeypatch.setattr(training, "_diffusion_dataset_summary", slow_summary)++ async def main():+ transport = httpx.ASGITransport(app = app)+ async with httpx.AsyncClient(transport = transport, base_url = "http://t") as client:+ upload = asyncio.ensure_future(+ client.post(+ "/api/train/diffusion/dataset",+ data = {"name": "styleset"},+ files = _parts(["a.png", "a.txt"]),+ )+ )+ ticks = 0+ while not upload.done():+ await client.get("/tick")+ ticks += 1+ await asyncio.sleep(0.01)+ return await upload, ticks++ res, ticks = asyncio.run(main())+ assert res.status_code == 200, res.text+ assert res.json()["image_count"] == 1 and res.json()["caption_count"] == 1+ # Blocking on the loop would serve no tick during the 0.4 s summary.+ assert ticks >= 5+++def test_cancelled_request_keeps_lock_until_worker_finishes():+ lock = threading.Lock()+ assert lock.acquire(blocking = False)+ entered = threading.Event()+ proceed = threading.Event()+ seen = {}++ def work(x, _lock, cancelled):+ try:+ entered.set()+ proceed.wait(5)+ seen["cancelled"] = cancelled.is_set()+ return x+ finally:+ _lock.release()++ async def main():+ task = asyncio.ensure_future(training._run_holding_import_lock(lock, work, 7))+ while not entered.is_set():+ await asyncio.sleep(0.01)+ task.cancel()+ await asyncio.sleep(0.05)+ # Pending request = training interlock and import lock still held.+ assert not task.done()+ assert lock.locked()+ proceed.set()+ with pytest.raises(asyncio.CancelledError):+ await task+ assert not lock.locked()++ asyncio.run(main())+ assert not lock.locked()+ assert seen == {"cancelled": True}+++def test_cancel_before_worker_starts_releases_lock():+ lock = threading.Lock()+ assert lock.acquire(blocking = False)+ ran = []+ block = threading.Event()++ async def main():+ loop = asyncio.get_running_loop()+ loop.set_default_executor(ThreadPoolExecutor(max_workers = 1))+ busy = loop.run_in_executor(None, block.wait, 5)+ task = asyncio.ensure_future(+ training._run_holding_import_lock(lock, lambda _lock, c: ran.append(1))+ )+ await asyncio.sleep(0.05)+ task.cancel()+ with pytest.raises(asyncio.CancelledError):+ await task+ assert not lock.locked()+ block.set()+ await busy+ await asyncio.sleep(0.05)++ asyncio.run(main())+ assert ran == []+ assert not lock.locked()+++def test_cancel_during_staging_rolls_back(ds_root):+ from starlette.datastructures import UploadFile++ folder = ds_root / "newset"+ lock = threading.Lock()+ assert lock.acquire(blocking = False)+ cancelled = threading.Event()+ cancelled.set()+ files = [UploadFile(io.BytesIO(_png_bytes()), filename = "a.png")]+ with pytest.raises(asyncio.CancelledError):+ training._store_diffusion_dataset_upload(folder, "newset", files, True, lock, cancelled)+ assert not lock.locked()+ assert not folder.exists()+++def test_worker_releases_lock_on_refusal(app, ds_root):+ from fastapi.testclient import TestClient++ client = TestClient(app)+ folder = ds_root / "styleset"+ folder.mkdir()+ (folder / "a.jpg").write_bytes(_png_bytes())+ res = client.post(+ "/api/train/diffusion/dataset", data = {"name": "styleset"}, files = _parts(["a.png"])+ )+ assert res.status_code == 400+ assert not training._dataset_import_lock(folder).locked()+ res = client.post(+ "/api/train/diffusion/dataset", data = {"name": "styleset"}, files = _parts(["b.png"])+ )+ assert res.status_code == 200+ assert sorted(p.name for p in folder.iterdir()) == ["a.jpg", "b.png"]