Skip to content

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.

Studio: guard export operations when the Hub is unreachable (#11466)

Lee Jackson8bb43220b419
diff --git a/studio/backend/core/export/worker.py b/studio/backend/core/export/worker.py
index e95cbc90ce3..dd65cefc364 100644
--- a/studio/backend/core/export/worker.py
+++ b/studio/backend/core/export/worker.py
@@ -179,12 +179,12 @@ def _activate_transformers_version(model_name: str, hf_token: str | None = None)
@contextlib.contextmanager
def _offline_window_if_unreachable(step = "loading"):
- """Force HF offline for a network-touching step (transformers version activation, or the
- load preflights that hit the Hub) when the endpoint is unreachable, then restore the prior
- env. Keeps a no-network export from hanging on Hub calls that run before load_checkpoint's
- own probe, while letting this persistent worker re-decide per operation once back online.
+ """Force HF offline for a network-touching step (transformers version activation, the load
+ preflights that hit the Hub, or a local export) when the endpoint is unreachable, then
+ restore the prior env. Keeps a no-network export from hanging or failing on Hub calls, while
+ letting this persistent worker re-decide per operation once back online.
- Post-ML-import (the load preflights), huggingface_hub has already read its in-process
+ Post-ML-import (load preflights, exports), huggingface_hub has already read its in-process
offline constant and cached sessions, so env alone is too late: defer to the loader's
_force_hf_offline (env + in-process flags + session reset). Pre-import (activation),
huggingface_hub is not loaded yet, so setting the env vars suffices for its urllib probes."""
@@ -681,7 +681,15 @@ def run_export_process(*, cmd_queue: Any, resp_queue: Any, config: dict) -> None
_handle_load(backend, cmd, resp_queue)
elif cmd_type == "export":
- _handle_export(backend, cmd, resp_queue)
+ # Re-probed per export: connectivity may change after loading. A push needs the Hub,
+ # so pinning it offline for the whole export would only make the push fail.
+ export_window = (
+ contextlib.nullcontext()
+ if cmd.get("push_to_hub")
+ else _offline_window_if_unreachable(step = "exporting")
+ )
+ with export_window:
+ _handle_export(backend, cmd, resp_queue)
elif cmd_type == "cleanup":
_handle_cleanup(backend, resp_queue)
diff --git a/studio/backend/tests/test_export_offline_window.py b/studio/backend/tests/test_export_offline_window.py
new file mode 100644
index 00000000000..b5bde893fea
--- /dev/null
+++ b/studio/backend/tests/test_export_offline_window.py
@@ -0,0 +1,102 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Exercise the persistent export command loop through a disconnect and recovery."""
+
+import os
+import queue
+import sys
+import threading
+import types
+from unittest.mock import patch
+
+
+def _run_worker(monkeypatch, commands, unreachable):
+ """Run the command loop with a stub export that makes a real Hub client call. Returns
+ (offline flag, request reached the transport) per export, and the export responses."""
+ import httpx
+ from huggingface_hub import HfApi, constants
+ from huggingface_hub.errors import OfflineModeIsEnabled
+ from core.export import worker
+ from core import import_guards
+ from loggers import config as log_config
+ from loggers.config import LogConfig
+ from utils import transformers_version
+ from utils.utils import force_hf_offline
+
+ calls = []
+
+ def handle_request(self, request):
+ return httpx.Response(200, json = {"id": "org/model", "siblings": []}, request = request)
+
+ class Backend:
+ def cleanup_memory(self):
+ pass
+
+ def export_merged_model(self, **kwargs):
+ offline = constants.HF_HUB_OFFLINE
+ try:
+ HfApi(token = False).model_info("org/model")
+ calls.append((offline, True))
+ except OfflineModeIsEnabled:
+ calls.append((offline, False))
+ return True, "saved", "/tmp/export"
+
+ fake_export = types.ModuleType("core.export.export")
+ fake_export.ExportBackend = Backend
+ fake_loader = types.ModuleType("unsloth.models.loader_utils")
+ fake_loader._force_hf_offline = force_hf_offline
+ monkeypatch.setitem(sys.modules, "core.export.export", fake_export)
+ monkeypatch.setitem(sys.modules, "unsloth.models.loader_utils", fake_loader)
+ monkeypatch.setattr(import_guards, "ensure_real_packages", lambda *args: None)
+ monkeypatch.setattr(LogConfig, "setup_logging", lambda **kwargs: None)
+ monkeypatch.setattr(worker, "_setup_log_capture", lambda _: None)
+ monkeypatch.setattr(worker, "_activate_transformers_version", lambda *args: None)
+ monkeypatch.setattr(worker, "_handle_load", lambda *args: None)
+ # Worker-process globals; keep them from leaking into later tests.
+ monkeypatch.setattr(worker, "_log_forward_gate", threading.Event())
+ monkeypatch.setattr(log_config, "_BARS_RESTORED", log_config._BARS_RESTORED)
+ # Below the client's request hook, which is where huggingface_hub enforces offline mode.
+ monkeypatch.setattr(httpx.HTTPTransport, "handle_request", handle_request)
+ monkeypatch.setattr(transformers_version, "hf_endpoint_unreachable", unreachable)
+ monkeypatch.setattr(constants, "HF_HUB_OFFLINE", False)
+ cmd_queue = queue.Queue()
+ for command in [*commands, {"type": "shutdown"}]:
+ cmd_queue.put(command)
+ resp_queue = queue.Queue()
+ # The worker normally owns its process environment; isolate that mutation here.
+ with patch.dict(os.environ):
+ os.environ.pop("HF_HUB_OFFLINE", None)
+ os.environ.pop("TRANSFORMERS_OFFLINE", None)
+ os.environ["UNSLOTH_OFFLINE_PROBE"] = "1"
+ worker.run_export_process(
+ cmd_queue = cmd_queue,
+ resp_queue = resp_queue,
+ config = {"checkpoint_path": "/tmp/ckpt"},
+ )
+ assert "HF_HUB_OFFLINE" not in os.environ
+ assert "TRANSFORMERS_OFFLINE" not in os.environ
+ assert constants.HF_HUB_OFFLINE is False
+ return calls, [m for m in resp_queue.queue if m.get("type") == "export_merged_done"]
+
+
+def test_local_export_rechecks_connectivity_and_restores_hub_state(monkeypatch):
+ # Activation and load online, first export disconnected, next export recovered.
+ verdicts = [False, False, True, False]
+ probes = []
+
+ def unreachable():
+ probes.append(None)
+ return verdicts[len(probes) - 1]
+
+ export = {"type": "export", "export_type": "merged"}
+ calls, done = _run_worker(monkeypatch, [export, export], unreachable)
+ assert len(probes) == len(verdicts)
+ assert calls == [(True, False), (False, True)]
+ assert [m["success"] for m in done] == [True, True]
+
+
+def test_push_export_is_not_pinned_offline(monkeypatch):
+ export = {"type": "export", "export_type": "merged", "push_to_hub": True}
+ calls, _ = _run_worker(monkeypatch, [export], lambda: True)
+ assert calls == [(False, True)]