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.

1.4 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""Trainers must finish saving before should_exit and the force-kill, or the worker dies mid-write."""

from types import SimpleNamespace


class _Server:
    def __init__(self, order):
        self._order = order

    @property
    def should_exit(self):
        return False

    @should_exit.setter
    def should_exit(self, value):
        self._order.append("should_exit")


def test_the_run_is_saved_before_the_server_stops_and_before_the_kill(monkeypatch):
    import run
    import core.training.diffusion_training_service as diffusion
    import core.training.training as training

    order = []
    fake = SimpleNamespace(
        stop_for_shutdown = lambda: order.append("stop_for_shutdown") or True,
        force_terminate = lambda: order.append("force_terminate"),
    )
    fake_diffusion = SimpleNamespace(
        stop_for_shutdown = lambda timeout: order.append(("diffusion", timeout)) or True,
    )
    monkeypatch.setattr(training, "_training_backend", fake)
    monkeypatch.setattr(diffusion, "_service", fake_diffusion)
    run._graceful_shutdown(_Server(order))
    assert order == [
        "stop_for_shutdown",
        ("diffusion", training._SHUTDOWN_STOP_TIMEOUT_S),
        "should_exit",
        "force_terminate",
    ]