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: prune dead startup records and preserve process identity (#14301)

oobaboogaabfa6c781d49
diff --git a/studio/backend/run.py b/studio/backend/run.py
index e6f553521b7..2502b14ce72 100644
--- a/studio/backend/run.py
+++ b/studio/backend/run.py
@@ -942,6 +942,12 @@ def _find_free_port(
from utils.paths.storage_roots import studio_root as _studio_root
+from utils.pid_records import (
+ RecordedStart,
+ linux_process_identity,
+ recorded_start,
+ pid_start_matches,
+)
# Legacy single-instance file; still read so `stop` finds an older build's server.
_PID_FILE = _studio_root() / "studio.pid"
@@ -969,6 +975,7 @@ def _pid_alive(pid: int) -> bool:
# os.kill(pid, 0) raises OSError for every pid on Windows, so tasklist is
# the only usable probe here.
import subprocess
+
try:
out = subprocess.run(
["tasklist", "/FI", f"PID eq {int(pid)}", "/NH", "/FO", "CSV"],
@@ -977,13 +984,13 @@ def _pid_alive(pid: int) -> bool:
encoding = "utf-8",
errors = "replace",
timeout = 10,
- ).stdout
+ )
except Exception:
- # Unconfirmed means keep, matching the CLI's _pid_alive. Pruning a live server's record is what lets the
- # next launch fall back past it and strand it; a stale record instead costs one clear "already running"
- # message.
+ # An uncertain probe must not erase a live backend's record.
+ return True
+ if out.returncode != 0:
return True
- return f'"{int(pid)}"' in out
+ return f'"{int(pid)}"' in out.stdout
try:
os.kill(pid, 0)
except ProcessLookupError:
@@ -1001,7 +1008,7 @@ def _process_create_time(pid: int) -> "float | None":
return None
-def _read_pid_record(path: Path) -> "tuple[int, float | None, str | None] | None":
+def _read_pid_record(path: Path) -> "tuple[int, RecordedStart, str | None] | None":
try:
lines = path.read_text(encoding = "utf-8").splitlines()
except (OSError, UnicodeDecodeError):
@@ -1016,28 +1023,13 @@ def _read_pid_record(path: Path) -> "tuple[int, float | None, str | None] | None
# kill(0) signals our whole process group; kill(1) is init. Never either.
if pid < 2:
return None
- created = None
- if len(lines) > 1:
- try:
- created = float(lines[1].strip())
- except ValueError:
- created = None
+ created = recorded_start(lines)
address = lines[2].strip() if len(lines) > 2 and lines[2].strip() else None
return pid, created, address
-def _pid_is_studio_backend(pid: int, created_times: "Sequence[float | None]" = ()) -> bool:
- """False only when a recorded start time proves this PID is a different process. Any recorded time matching
- is enough: a stale record must not veto a live server that reused the PID. Untimed records cannot be
- checked at all, so they are trusted, since a legacy `python run.py` has no telltale argv and guessing
- from the command line rejected real servers."""
- known = [c for c in created_times if c is not None]
- if not known:
- return True
- actual = _process_create_time(pid)
- if actual is None:
- return True
- return any(abs(actual - c) < 1.0 for c in known)
+def _pid_is_studio_backend(pid: int, created_times: "Sequence[RecordedStart]" = ()) -> bool:
+ return pid_start_matches(pid, created_times, _process_create_time)
def _own_studio_on_port(port: int, host: str) -> "int | None":
@@ -1090,6 +1082,40 @@ def _legacy_studio_on_port(port: int) -> "int | None":
return pid
+def _pid_record_version(path: Path) -> tuple[int, int, int, int, int]:
+ stat = path.stat()
+ # Reading the record can change atime without replacing or rewriting it.
+ return stat.st_dev, stat.st_ino, stat.st_size, stat.st_mtime_ns, stat.st_ctime_ns
+
+
+def _prune_dead_pid_records() -> None:
+ """Requires the cache lock. Epoch drift is not proof of death; per-port records
+ can be replaced without this lock, so recheck the file and PID before unlinking.
+ """
+ me = os.getpid()
+ for pattern in (STARTUP_MARKER_GLOB, PID_FILE_GLOB):
+ try:
+ paths = list(_studio_root().glob(pattern))
+ except OSError:
+ continue
+ for path in paths:
+ if path in _OWN_STARTUP_MARKERS or path == _OWN_PID_FILE:
+ continue
+ try:
+ before = _pid_record_version(path)
+ record = _read_pid_record(path)
+ if record is None or record[0] == me or _pid_alive(record[0]):
+ continue
+ if path.stem.rsplit("-", 1)[-1] != str(record[0]):
+ continue
+ if _read_pid_record(path) != record or _pid_record_version(path) != before:
+ continue
+ if not _pid_alive(record[0]):
+ path.unlink(missing_ok = True)
+ except OSError:
+ continue
+
+
def write_startup_marker() -> None:
"""Record that this process is coming up, before uvicorn starts. The per-port record cannot be written
until uvicorn reports the bound port, and lifespan startup runs well before that. Two overlapping
@@ -1098,20 +1124,20 @@ def write_startup_marker() -> None:
clear takes, so the marker cannot appear in the gap between a sibling's probe and its rmtree."""
me = os.getpid()
created = _process_create_time(me)
- body = f"{me}\n{created if created is not None else ''}\n"
+ identity = linux_process_identity(me)
+ body = f"{me}\n{created if created is not None else ''}\n\n{identity or ''}\n"
try:
- from utils.cache_cleanup import compiled_cache_lock, LOCK_BUSY
+ from utils.cache_cleanup import compiled_cache_lock, LOCK_BUSY, LOCK_HELD
except Exception:
import contextlib
-
- # No lock available means an unserialized publish, which is what this
- # did before the lock existed. Startup still has to happen.
- compiled_cache_lock = contextlib.nullcontext
- LOCK_BUSY = None
+ compiled_cache_lock = lambda **kwargs: contextlib.nullcontext()
+ LOCK_BUSY, LOCK_HELD = "busy", "held"
# A longer budget than a clear takes: publishing behind a holder that is mid-rmtree is the case worth
# waiting out. Publishing anyway if the wait runs out is still right, because a marker that never appears
# makes this backend invisible.
with compiled_cache_lock(timeout = 30.0) as lock_state:
+ if lock_state == LOCK_HELD:
+ _prune_dead_pid_records()
published_unlocked = lock_state == LOCK_BUSY
if published_unlocked:
logger.warning(
@@ -1206,7 +1232,7 @@ def _record_written_at(path: Path) -> float:
return 0.0
-def _timed_records() -> "list[tuple[tuple[int, float | None, str | None], float]]":
+def _timed_records() -> "list[tuple[tuple[int, RecordedStart, str | None], float]]":
"""Every startup marker and per-port record, each with its write time."""
paths: "list[Path]" = []
for pattern in (STARTUP_MARKER_GLOB, PID_FILE_GLOB):
@@ -1222,7 +1248,7 @@ def _timed_records() -> "list[tuple[tuple[int, float | None, str | None], float]
return found
-def _legacy_record() -> "tuple[tuple[int, float | None, str | None], float] | None":
+def _legacy_record() -> "tuple[tuple[int, RecordedStart, str | None], float] | None":
try:
if not _PID_FILE.is_file():
return None
@@ -1283,7 +1309,7 @@ def live_sibling_backend() -> "int | None":
return _live_sibling(timed + [_legacy_record()], me, timed)
-def _per_port_records() -> "list[tuple[int, float | None, str | None] | None]":
+def _per_port_records() -> "list[tuple[int, RecordedStart, str | None] | None]":
try:
return [_read_pid_record(p) for p in _studio_root().glob(PID_FILE_GLOB)]
except OSError:
@@ -1369,7 +1395,8 @@ def _write_pid_file(port: int, host: str = ""):
# later launch whether this server would actually block it.
created = _process_create_time(os.getpid())
address = ",".join(sorted(_bind_addresses(host, port))) if host else ""
- body = f"{os.getpid()}\n{'' if created is None else repr(created)}\n{address}"
+ identity = linux_process_identity(os.getpid())
+ body = f"{os.getpid()}\n{'' if created is None else repr(created)}\n{address}\n{identity or ''}\n"
# Write-then-rename: `stop` reads these concurrently, and a reader that
# catches the truncate window sees a corrupt record and deletes it.
tmp = path.with_name(path.name + ".tmp")
diff --git a/studio/backend/tests/test_pid_records.py b/studio/backend/tests/test_pid_records.py
new file mode 100644
index 00000000000..92704db2e89
--- /dev/null
+++ b/studio/backend/tests/test_pid_records.py
@@ -0,0 +1,85 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import sys
+from pathlib import Path
+
+import pytest
+
+from utils import pid_records as records
+
+
[email protected]("delta", [-3600.0, -1.0, 0.0, 1.0, 3600.0])
+def test_linux_identity_survives_wall_clock_changes(monkeypatch, delta):
+ monkeypatch.setattr(records, "linux_process_identity", lambda pid: "linux:boot:123")
+ start = records.recorded_start(["4242", "1000.0", "127.0.0.1", "linux:boot:123"])
+ assert records.pid_start_matches(4242, [start], lambda pid: 1000.0 + delta)
+
+
[email protected]("actual", ["linux:next-boot:123", "linux:boot:124"])
+def test_reused_pid_is_rejected_even_if_epoch_time_matches(monkeypatch, actual):
+ monkeypatch.setattr(records, "linux_process_identity", lambda pid: actual)
+ start = records.recorded_start(["4242", "1000.0", "", "linux:boot:123"])
+ assert not records.pid_start_matches(4242, [start, None], lambda pid: 1000.0)
+
+
+def test_a_matching_record_can_corroborate_an_older_stale_one(monkeypatch):
+ monkeypatch.setattr(records, "linux_process_identity", lambda pid: "linux:boot:124")
+ assert records.pid_start_matches(4242, ["linux:boot:123", "linux:boot:124"])
+ # A current legacy writer may reuse the PID of a dead, newer-format backend.
+ assert records.pid_start_matches(4242, ["linux:boot:123", 1000.0], lambda pid: 1000.0)
+
+
+def test_an_unreadable_identity_is_not_evidence_of_pid_reuse(monkeypatch):
+ monkeypatch.setattr(records, "linux_process_identity", lambda pid: None)
+ assert records.pid_start_matches(4242, ["linux:boot:123"], lambda pid: 9999.0)
+
+
+ "delta, matches", [(-1.0, True), (1.0, True), (-1.01, False), (1.01, False)]
+)
+def test_legacy_epoch_tolerance_matches_the_desktop(delta, matches):
+ assert records.pid_start_matches(4242, [1000.0], lambda pid: 1000.0 + delta) is matches
+
+
+ "extra",
+ ["", "linux:boot:garbage", "linux::123", "linux:boot:-1", "linux:boot:18446744073709551616"],
+)
+def test_invalid_identity_falls_back_to_legacy_time(extra):
+ assert records.recorded_start(["4242", "1000.0", "127.0.0.1", extra]) == 1000.0
+
+
[email protected]("lines", [["4242"], ["4242", ""], ["4242", "nan"], ["4242", "inf"]])
+def test_unavailable_start_time_stays_unknown(lines):
+ assert records.recorded_start(lines) is None
+
+
[email protected](sys.platform != "linux", reason = "Linux procfs identity")
+def test_proc_identity_handles_parentheses_and_spaces_in_comm(monkeypatch):
+ def read(path, **kwargs):
+ if str(path).endswith("/stat"):
+ return "4242 (name with ) spaces) S " + "0 " * 18 + "123 0\n"
+ return "boot\n"
+
+ monkeypatch.setattr(Path, "read_text", read)
+ assert records.linux_process_identity(4242) == "linux:boot:123"
+
+
+def test_an_unverifiable_legacy_record_cannot_cancel_proven_pid_reuse(monkeypatch):
+ monkeypatch.setattr(records, "linux_process_identity", lambda pid: "linux:boot:124")
+ assert not records.pid_start_matches(4242, ["linux:boot:123", 1000.0, None], lambda pid: None)
+
+
+def test_only_a_stable_start_proves_pid_reuse(monkeypatch):
+ monkeypatch.setattr(records.sys, "platform", "linux")
+ monkeypatch.setattr(records, "linux_process_identity", lambda pid: "linux:boot:124")
+ drifted = lambda pid: 4600.0
+ assert records.pid_reuse_proven(42, ["linux:boot:123"], drifted)
+ assert not records.pid_reuse_proven(42, ["linux:boot:124"], drifted)
+ assert not records.pid_reuse_proven(42, [1000.0], drifted)
+ assert not records.pid_reuse_proven(42, [None], drifted)
+ monkeypatch.setattr(records, "linux_process_identity", lambda pid: None)
+ assert not records.pid_reuse_proven(42, ["linux:boot:123"], drifted)
+ monkeypatch.setattr(records.sys, "platform", "darwin")
+ assert records.pid_reuse_proven(42, [1000.0], drifted)
diff --git a/studio/backend/tests/test_studio_pid_files.py b/studio/backend/tests/test_studio_pid_files.py
index 43004d17285..8166b5c7cc9 100644
--- a/studio/backend/tests/test_studio_pid_files.py
+++ b/studio/backend/tests/test_studio_pid_files.py
@@ -73,12 +73,15 @@ def test_write_pid_file_records_port_and_pid(tmp_path):
def test_write_pid_file_records_the_start_time(tmp_path):
# Pins the record to this process, so a reused PID isn't mistaken for it.
+ pytest.importorskip("psutil")
run._write_pid_file(8901)
record = run._read_pid_record(tmp_path / f"studio-8901-{os.getpid()}.pid")
assert record[0] == os.getpid()
- assert record[1] == pytest.approx(run._process_create_time(os.getpid()))
+ lines = (tmp_path / f"studio-8901-{os.getpid()}.pid").read_text().splitlines()
+ assert float(lines[1]) == pytest.approx(run._process_create_time(os.getpid()))
+ assert _REAL_IS_STUDIO_BACKEND(os.getpid(), [record[1]])
def test_write_pid_file_keeps_the_legacy_file_a_bare_pid(tmp_path):
@@ -158,7 +161,9 @@ def test_windows_liveness_does_not_call_every_pid_alive(monkeypatch):
monkeypatch.setitem(sys.modules, "psutil", None)
monkeypatch.setattr(sys, "platform", "win32")
monkeypatch.setattr(
- subprocess, "run", lambda *a, **k: SimpleNamespace(stdout = '"python.exe","8550",...')
+ subprocess,
+ "run",
+ lambda *a, **k: SimpleNamespace(stdout = '"python.exe","8550",...', returncode = 0),
)
assert run._pid_alive(8550) is True
@@ -1053,6 +1058,7 @@ def test_a_zombie_backend_is_not_a_live_sibling(tmp_path, monkeypatch):
assert run.live_sibling_backend() is None
[email protected](sys.platform == "win32", reason = "Windows has no unreaped POSIX zombies")
def test_a_real_unreaped_child_is_not_a_live_sibling(tmp_path):
# The same thing without a patch: a killed but unwaited child.
import subprocess
@@ -1377,3 +1383,148 @@ def test_a_marker_that_will_not_delete_stays_retryable(tmp_path, monkeypatch):
assert run._OWN_STARTUP_MARKERS == []
assert not marker.exists()
+
+
+def test_startup_prunes_dead_records_but_preserves_live_and_uncertain_ones(tmp_path, monkeypatch):
+ dead = [tmp_path / "studio-starting-4242.marker", tmp_path / "studio-8888-4242.pid"]
+ for path in dead:
+ path.write_text("4242\n111.5\n", encoding = "utf-8")
+ kept = {
+ "studio-starting-4243.marker": "4243\n111.5\n",
+ "studio-8889-4243.pid": "4243\n111.5\n127.0.0.1",
+ "studio-starting-4244.marker": "partial write",
+ "studio-8890-4244.pid": "partial write",
+ "studio-starting-4245.marker": "4242\n111.5\n",
+ "studio.pid": "4242",
+ }
+ for name, body in kept.items():
+ (tmp_path / name).write_text(body, encoding = "utf-8")
+ monkeypatch.setattr(run, "_pid_alive", lambda pid: pid != 4242)
+ monkeypatch.setattr(run, "_pid_is_studio_backend", lambda *args: False)
+ run.write_startup_marker()
+ assert all(not p.exists() for p in dead)
+ assert {name: (tmp_path / name).read_text() for name in kept} == kept
+
+
+ "state", [cache_cleanup.LOCK_HELD, cache_cleanup.LOCK_BUSY, cache_cleanup.LOCK_UNAVAILABLE]
+)
+def test_startup_prunes_only_inside_the_cache_lock(tmp_path, monkeypatch, state):
+ stale = tmp_path / "studio-starting-4242.marker"
+ stale.write_text("4242\n111.5\n", encoding = "utf-8")
+ held = False
+
+ @contextlib.contextmanager
+ def lock(**kwargs):
+ nonlocal held
+ held = state == cache_cleanup.LOCK_HELD
+ yield state
+ held = False
+
+ def alive(pid):
+ assert held
+ return False
+
+ monkeypatch.setattr(cache_cleanup, "compiled_cache_lock", lock)
+ monkeypatch.setattr(run, "_pid_alive", alive)
+ run.write_startup_marker()
+ assert stale.exists() is (state != cache_cleanup.LOCK_HELD)
+ assert (tmp_path / f"studio-starting-{os.getpid()}.marker").exists()
+
+
[email protected]("replace_same_body", [False, True])
+def test_pruning_keeps_a_pid_record_replaced_during_the_probe(
+ tmp_path, monkeypatch, replace_same_body
+):
+ path = tmp_path / "studio-8888-4242.pid"
+ body = "4242\n111.5\n127.0.0.1"
+ path.write_text(body, encoding = "utf-8")
+ original = path.stat()
+
+ def alive(pid):
+ replacement = tmp_path / "replacement"
+ replacement.write_text(body if replace_same_body else "4242\n999.0\n127.0.0.1")
+ os.utime(replacement, ns = (original.st_atime_ns, original.st_mtime_ns))
+ os.replace(replacement, path)
+ return False
+
+ monkeypatch.setattr(run, "_pid_alive", alive)
+ run.write_startup_marker()
+ assert path.exists()
+
+
+def test_pruning_rechecks_a_pid_that_became_live(tmp_path, monkeypatch):
+ path = tmp_path / "studio-starting-4242.marker"
+ path.write_text("4242\n111.5\n", encoding = "utf-8")
+ answers = iter([False, True])
+ monkeypatch.setattr(run, "_pid_alive", lambda pid: next(answers))
+ run.write_startup_marker()
+ assert path.exists()
+
+
+def test_pruning_failure_does_not_prevent_startup(tmp_path, monkeypatch):
+ path = tmp_path / "studio-starting-4242.marker"
+ path.write_text("4242\n111.5\n", encoding = "utf-8")
+ unlink = Path.unlink
+
+ def fail(stale, *args, **kwargs):
+ if stale == path:
+ raise PermissionError("held by another process")
+ return unlink(stale, *args, **kwargs)
+
+ monkeypatch.setattr(Path, "unlink", fail)
+ monkeypatch.setattr(run, "_pid_alive", lambda pid: False)
+ run.write_startup_marker()
+ assert path.exists()
+ assert (tmp_path / f"studio-starting-{os.getpid()}.marker").exists()
+
+
+def test_stable_identity_protects_siblings_ports_and_legacy_handoff(tmp_path, monkeypatch):
+ from utils import pid_records
+
+ monkeypatch.setattr(run, "_pid_is_studio_backend", _REAL_IS_STUDIO_BACKEND)
+ monkeypatch.setattr(run, "_process_create_time", lambda pid: 4600.0)
+ monkeypatch.setattr(pid_records, "linux_process_identity", lambda pid: "linux:boot:123")
+ path = tmp_path / "studio-8888-4242.pid"
+ path.write_text("4242\n1000.0\n127.0.0.1\nlinux:boot:123\n", encoding = "utf-8")
+ assert run.live_sibling_backend() == 4242
+ assert run._own_studio_on_port(8888, "127.0.0.1") == 4242
+ assert run._legacy_heir() == 4242
+ monkeypatch.setattr(pid_records, "linux_process_identity", lambda pid: "linux:boot:124")
+ assert run.live_sibling_backend() is None
+ assert run._own_studio_on_port(8888, "127.0.0.1") is None
+ assert run._legacy_heir() is None
+
+
+def test_pruning_ignores_access_time_changes_from_reading_the_record(tmp_path, monkeypatch):
+ path = tmp_path / "studio-8888-4242.pid"
+ path.write_text("4242\n111.5\n127.0.0.1", encoding = "utf-8")
+ stat = Path.stat
+ reads = 0
+
+ def accessed(item, *args, **kwargs):
+ nonlocal reads
+ result = stat(item, *args, **kwargs)
+ if item == path:
+ reads += 1
+ fields = {name: getattr(result, name) for name in dir(result) if name.startswith("st_")}
+ fields["st_atime"] += reads
+ fields["st_atime_ns"] += reads * 1_000_000_000
+ return SimpleNamespace(**fields)
+ return result
+
+ monkeypatch.setattr(Path, "stat", accessed)
+ monkeypatch.setattr(run, "_pid_alive", lambda pid: False)
+ run.write_startup_marker()
+ assert not path.exists()
+
+
+def test_windows_liveness_keeps_records_when_tasklist_returns_an_error(monkeypatch):
+ import subprocess
+
+ monkeypatch.setitem(sys.modules, "psutil", None)
+ monkeypatch.setattr(sys, "platform", "win32")
+ monkeypatch.setattr(
+ subprocess, "run", lambda *args, **kwargs: SimpleNamespace(stdout = "", returncode = 1)
+ )
+ assert _REAL_PID_ALIVE(8550) is True
diff --git a/studio/backend/utils/pid_records.py b/studio/backend/utils/pid_records.py
new file mode 100644
index 00000000000..e4a9a6668b5
--- /dev/null
+++ b/studio/backend/utils/pid_records.py
@@ -0,0 +1,93 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Line four adds clock-stable linux:<boot_id>:<start_ticks>; keep the first three
+lines backward compatible and the Rust reader in process_identity.rs aligned.
+"""
+
+from __future__ import annotations
+
+import math
+import sys
+from pathlib import Path
+from typing import Callable, Sequence, Union
+
+RecordedStart = Union[float, str, None]
+
+
+def linux_process_identity(pid: int) -> str | None:
+ if sys.platform != "linux":
+ return None
+ try:
+ # comm may contain spaces and parentheses. Field 22 follows the last ')'.
+ stat = Path(f"/proc/{pid}/stat").read_text(encoding = "utf-8")
+ ticks = int(stat[stat.rindex(")") + 1 :].split()[19])
+ boot = Path("/proc/sys/kernel/random/boot_id").read_text(encoding = "ascii").strip()
+ return f"linux:{boot}:{ticks}" if boot else None
+ except (OSError, ValueError, IndexError, UnicodeError):
+ return None
+
+
+def recorded_start(lines: Sequence[str]) -> RecordedStart:
+ if len(lines) > 3:
+ parts = lines[3].strip().split(":")
+ if len(parts) == 3 and parts[0] == "linux" and parts[1]:
+ try:
+ ticks = int(parts[2])
+ if 0 <= ticks < 2**64:
+ return f"linux:{parts[1]}:{ticks}"
+ except ValueError:
+ pass
+ try:
+ epoch = float(lines[1])
+ return epoch if math.isfinite(epoch) else None
+ except (IndexError, ValueError):
+ return None
+
+
+def process_create_time(pid: int) -> float | None:
+ try:
+ import psutil
+ return psutil.Process(pid).create_time()
+ except Exception:
+ return None
+
+
+def pid_start_matches(
+ pid: int,
+ starts: Sequence[RecordedStart],
+ create_time: Callable[[int], float | None] = process_create_time,
+) -> bool:
+ known = [start for start in starts if start is not None]
+ if not known:
+ return True
+ identity = linux_process_identity(pid) if any(isinstance(s, str) for s in known) else None
+ epoch = create_time(pid) if any(not isinstance(s, str) for s in known) else None
+ compared = False
+ for start in known:
+ if isinstance(start, str):
+ if identity is not None:
+ compared = True
+ if identity == start:
+ return True
+ elif epoch is not None:
+ compared = True
+ if abs(epoch - start) <= 1.0:
+ return True
+ return not compared
+
+
+def pid_reuse_proven(
+ pid: int,
+ starts: Sequence[RecordedStart],
+ create_time: Callable[[int], float | None] = process_create_time,
+) -> bool:
+ """A mismatch proves reuse only with stable starts: Linux epoch times move with boot-time readjustments."""
+ known = [start for start in starts if start is not None]
+ if not known or pid_start_matches(pid, known, create_time):
+ return False
+ if sys.platform != "linux":
+ return True
+ return (
+ all(isinstance(start, str) for start in known) and linux_process_identity(pid) is not None
+ )
diff --git a/studio/src-tauri/src/preflight/pid_records.rs b/studio/src-tauri/src/preflight/pid_records.rs
index 3f6f49d89fa..d11413a040e 100644
--- a/studio/src-tauri/src/preflight/pid_records.rs
+++ b/studio/src-tauri/src/preflight/pid_records.rs
@@ -1,4 +1,4 @@
-use crate::process_identity::ProcessOrigin;
+use crate::process_identity::{ProcessOrigin, RecordedStartTime};
use std::path::Path;
/// The pid of a live backend of THIS install that is serving `port`, if one is
@@ -25,6 +25,7 @@ pub(super) fn live_backend_pid_on_port(port: u16) -> Option<u32> {
},
origin: &|pid| crate::process_identity::origin_of(pid, &root, &interpreters),
started_at: &crate::process_identity::process_start_time_secs,
+ identity: &crate::process_identity::linux_process_identity,
};
live_backend_pid_in(&root, port, &probe)
}
@@ -53,6 +54,7 @@ struct Probe<'a> {
origin: &'a dyn Fn(u32) -> ProcessOrigin,
/// Unix epoch seconds the process started, when the OS will say.
started_at: &'a dyn Fn(u32) -> Option<f64>,
+ identity: &'a dyn Fn(u32) -> Option<String>,
}
/// A record and the process wearing its pid, once the two have been compared.
@@ -71,20 +73,16 @@ enum Recorded {
Opaque,
}
-const START_TIME_TOLERANCE_SECS: f64 =
- crate::process_identity::PID_START_TIME_TOLERANCE_SECS;
-
-fn classify(pid: u32, recorded_start: Option<f64>, probe: &Probe) -> Recorded {
+fn classify(pid: u32, recorded_start: Option<RecordedStartTime>, probe: &Probe) -> Recorded {
if !(probe.is_live)(pid) {
return Recorded::Stale;
}
- // A recorded start time that disagrees is proof the pid was reused, which
- // no amount of executable evidence can override: the process the record
- // described is gone.
- if let (Some(recorded), Some(actual)) = (recorded_start, (probe.started_at)(pid)) {
- if (actual - recorded).abs() > START_TIME_TOLERANCE_SECS {
- return Recorded::Stale;
- }
+ if !crate::process_identity::recorded_start_matches(
+ recorded_start,
+ (probe.started_at)(pid),
+ (probe.identity)(pid),
+ ) {
+ return Recorded::Stale;
}
match (probe.origin)(pid) {
ProcessOrigin::InsideTree => Recorded::OurBackend,
@@ -182,7 +180,7 @@ fn has_a_per_port_record(root: &Path, pid: u32) -> bool {
false
}
-fn recorded_start_time(path: &Path) -> Option<f64> {
+fn recorded_start_time(path: &Path) -> Option<RecordedStartTime> {
crate::process_identity::recorded_pid_start_time(path)
}
@@ -220,14 +218,37 @@ mod tests {
is_live: &|_| true,
origin,
started_at: &|_| None,
+ identity: &|_| None,
}
}
+ #[test]
+ fn a_stable_identity_survives_epoch_drift_and_rejects_reuse() {
+ let home = Home::new();
+ home.record(RECORDED_8888, "4242\n1000.0\n127.0.0.1\nlinux:boot:123\n");
+ let same = Probe {
+ started_at: &|_| Some(4600.0),
+ identity: &|_| Some("linux:boot:123".into()),
+ ..probe(&shared)
+ };
+ assert_eq!(
+ live_backend_pid_in(&home.path(), 8888, &same),
+ Some(RECORDED)
+ );
+ let reused = Probe {
+ started_at: &|_| Some(1000.0),
+ identity: &|_| Some("linux:boot:124".into()),
+ ..probe(&shared)
+ };
+ assert_eq!(live_backend_pid_in(&home.path(), 8888, &reused), None);
+ }
+
fn gone<'a>() -> Probe<'a> {
Probe {
is_live: &|_| false,
origin: &|_| unreachable!("a dead pid is never attributed"),
started_at: &|_| unreachable!("a dead pid is never timed"),
+ identity: &|_| unreachable!("a dead pid is never identified"),
}
}
@@ -288,6 +309,7 @@ mod tests {
is_live: &|_| true,
origin: &shared,
started_at: &|_| Some(9999.0),
+ identity: &|_| None,
};
assert_eq!(live_backend_pid_in(&home.path(), 8888, &reused), None);
@@ -301,6 +323,7 @@ mod tests {
is_live: &|_| true,
origin: &shared,
started_at: &|_| Some(1000.4),
+ identity: &|_| None,
};
assert_eq!(
@@ -319,6 +342,7 @@ mod tests {
is_live: &|_| true,
origin: &shared,
started_at: &|_| Some(9999.0),
+ identity: &|_| None,
};
assert_eq!(
@@ -415,6 +439,7 @@ mod tests {
is_live: &|_| true,
origin: &ours,
started_at: &|_| Some(9999.0),
+ identity: &|_| None,
};
assert_eq!(live_backend_pid_in(&home.path(), 8888, &reused), None);
diff --git a/studio/src-tauri/src/process_identity.rs b/studio/src-tauri/src/process_identity.rs
index 3d179bf4282..9503091f7ff 100644
--- a/studio/src-tauri/src/process_identity.rs
+++ b/studio/src-tauri/src/process_identity.rs
@@ -353,28 +353,68 @@ pub(crate) fn process_start_time_secs(pid: u32) -> Option<f64> {
/// The Python side uses the same one-second window in `_pid_is_studio_backend`.
pub(crate) const PID_START_TIME_TOLERANCE_SECS: f64 = 1.0;
-/// Line two of a `studio-{port}-{pid}.pid` record, written by the server as
-/// `psutil` epoch seconds. A blank or absent line means it could not say.
-pub(crate) fn recorded_pid_start_time(path: &Path) -> Option<f64> {
- std::fs::read_to_string(path)
- .ok()?
- .lines()
- .nth(1)?
- .trim()
- .parse()
- .ok()
-}
-
-/// Whether the process wearing `pid` is the one the record described.
-///
-/// A recorded start time that disagrees is proof the pid was reused, which no
-/// amount of executable evidence can override. Records outlive crashes, so
-/// without this an unrelated process inheriting the pid reads as ours.
-pub(crate) fn pid_start_time_matches(pid: u32, recorded: Option<f64>) -> bool {
- let (Some(recorded), Some(actual)) = (recorded, process_start_time_secs(pid)) else {
- return true;
- };
- (actual - recorded).abs() <= PID_START_TIME_TOLERANCE_SECS
+/// Linux uses a boot ID and raw start ticks, which do not move with the wall clock.
+/// Epoch seconds remain on line two for older readers and other platforms.
+#[derive(Clone, Debug, PartialEq)]
+pub(crate) enum RecordedStartTime {
+ Epoch(f64),
+ Linux(String),
+}
+
+pub(crate) fn recorded_pid_start_time(path: &Path) -> Option<RecordedStartTime> {
+ let body = std::fs::read_to_string(path).ok()?;
+ let lines: Vec<_> = body.lines().collect();
+ if let Some(identity) = lines.get(3) {
+ let parts: Vec<_> = identity.trim().split(':').collect();
+ if parts.len() == 3 && parts[0] == "linux" && !parts[1].is_empty() {
+ if let Ok(ticks) = parts[2].parse::<u64>() {
+ return Some(RecordedStartTime::Linux(format!(
+ "linux:{}:{ticks}",
+ parts[1]
+ )));
+ }
+ }
+ }
+ let epoch: f64 = lines.get(1)?.trim().parse().ok()?;
+ epoch.is_finite().then_some(RecordedStartTime::Epoch(epoch))
+}
+
+pub(crate) fn recorded_start_matches(
+ recorded: Option<RecordedStartTime>,
+ actual_epoch: Option<f64>,
+ actual_identity: Option<String>,
+) -> bool {
+ match recorded {
+ Some(RecordedStartTime::Linux(recorded)) => {
+ actual_identity.is_none_or(|actual| actual == recorded)
+ }
+ Some(RecordedStartTime::Epoch(recorded)) => actual_epoch
+ .is_none_or(|actual| (actual - recorded).abs() <= PID_START_TIME_TOLERANCE_SECS),
+ None => true,
+ }
+}
+
+pub(crate) fn pid_start_time_matches(pid: u32, recorded: Option<RecordedStartTime>) -> bool {
+ recorded_start_matches(
+ recorded,
+ process_start_time_secs(pid),
+ linux_process_identity(pid),
+ )
+}
+
+#[cfg(target_os = "linux")]
+pub(crate) fn linux_process_identity(pid: u32) -> Option<String> {
+ let stat = std::fs::read_to_string(format!("/proc/{pid}/stat")).ok()?;
+ let after_comm = &stat[stat.rfind(')')? + 1..];
+ let ticks: u64 = after_comm.split_whitespace().nth(19)?.parse().ok()?;
+ let boot = std::fs::read_to_string("/proc/sys/kernel/random/boot_id").ok()?;
+ let boot = boot.trim();
+ (!boot.is_empty()).then(|| format!("linux:{boot}:{ticks}"))
+}
+
+#[cfg(not(target_os = "linux"))]
+pub(crate) fn linux_process_identity(_pid: u32) -> Option<String> {
+ None
}
#[cfg(target_os = "linux")]
@@ -553,6 +593,59 @@ fn executable_path_impl(_pid: u32) -> Option<PathBuf> {
mod tests {
use super::*;
+ #[test]
+ fn stable_identity_ignores_epoch_drift_but_rejects_reuse() {
+ let recorded = Some(RecordedStartTime::Linux("linux:boot:123".into()));
+ for (actual, matches) in [
+ ("linux:boot:123", true),
+ ("linux:boot:124", false),
+ ("linux:other-boot:123", false),
+ ] {
+ assert_eq!(
+ recorded_start_matches(recorded.clone(), Some(4600.0), Some(actual.into())),
+ matches
+ );
+ }
+ }
+
+ #[test]
+ fn record_parser_prefers_identity_and_accepts_old_records() {
+ let file = tempfile::NamedTempFile::new().unwrap();
+ std::fs::write(file.path(), "4242\n1000.0\n127.0.0.1\nlinux:boot:123\n").unwrap();
+ assert_eq!(
+ recorded_pid_start_time(file.path()),
+ Some(RecordedStartTime::Linux("linux:boot:123".into()))
+ );
+ for suffix in ["", "linux:boot:-1", "linux::123", "linux:boot:garbage"] {
+ std::fs::write(file.path(), format!("4242\n1000.0\n127.0.0.1\n{suffix}\n")).unwrap();
+ assert_eq!(
+ recorded_pid_start_time(file.path()),
+ Some(RecordedStartTime::Epoch(1000.0))
+ );
+ }
+ }
+
+ #[test]
+ fn missing_identity_keeps_the_record_and_legacy_tolerance_is_inclusive() {
+ assert!(recorded_start_matches(
+ Some(RecordedStartTime::Linux("linux:boot:123".into())),
+ Some(9999.0),
+ None
+ ));
+ for delta in [-1.0, 1.0] {
+ assert!(recorded_start_matches(
+ Some(RecordedStartTime::Epoch(1000.0)),
+ Some(1000.0 + delta),
+ None
+ ));
+ }
+ assert!(!recorded_start_matches(
+ Some(RecordedStartTime::Epoch(1000.0)),
+ Some(1001.01),
+ None
+ ));
+ }
+
fn tree_with_venv(base: Option<&Path>, layout: &str) -> tempfile::TempDir {
let tree = tempfile::tempdir().unwrap();
let venv = tree.path().join("unsloth_studio");
diff --git a/studio/src-tauri/src/staged_update.rs b/studio/src-tauri/src/staged_update.rs
index 293ddc239ac..53b8af95bd5 100644
--- a/studio/src-tauri/src/staged_update.rs
+++ b/studio/src-tauri/src/staged_update.rs
@@ -880,6 +880,21 @@ mod tests {
Some(me)
}
+ #[cfg(target_os = "linux")]
+ #[test]
+ fn stable_records_guard_updates_after_an_epoch_clock_shift() {
+ let home = temp_home("stable-markers");
+ let me = std::process::id();
+ let identity = crate::process_identity::linux_process_identity(me).unwrap();
+ let marker = home.join(format!("studio-starting-{me}.marker"));
+ fs::write(&marker, format!("{me}\n1.0\n\n{identity}\n")).unwrap();
+ assert!(recorded_pids(&home).contains(&me));
+ fs::write(&marker, format!("{me}\n1.0\n\nlinux:another-boot:0\n")).unwrap();
+ fs::write(home.join("studio.pid"), format!("{me}\n")).unwrap();
+ assert!(!recorded_pids(&home).contains(&me));
+ cleanup(home);
+ }
+
#[test]
fn a_startup_marker_counts_as_a_live_tree_record() {
let home = temp_home("markers");
diff --git a/tests/studio/test_studio_pid_file_contract.py b/tests/studio/test_studio_pid_file_contract.py
index 0d400ceab78..e656aeb7a5c 100644
--- a/tests/studio/test_studio_pid_file_contract.py
+++ b/tests/studio/test_studio_pid_file_contract.py
@@ -55,6 +55,7 @@ def test_discovery_reads_the_bind_addresses_the_backend_records(tmp_path, monkey
"_PID_FILE": tmp_path / "studio.pid",
"_pid_file_for_port": lambda port: _backend_pid_path(tmp_path, port),
"_process_create_time": lambda pid: None,
+ "linux_process_identity": lambda pid: None,
"_bind_addresses": lambda host, port: {"::1"},
"_read_pid_record": lambda path: None,
"_pid_alive": lambda pid: False,
@@ -78,6 +79,7 @@ def test_the_legacy_file_stays_a_bare_pid_an_older_cli_can_parse(tmp_path, monke
"_PID_FILE": tmp_path / "studio.pid",
"_pid_file_for_port": lambda port: _backend_pid_path(tmp_path, port),
"_process_create_time": lambda pid: None,
+ "linux_process_identity": lambda pid: None,
"_bind_addresses": lambda host, port: {host},
"_read_pid_record": lambda path: None,
"_pid_alive": lambda pid: False,
diff --git a/tests/test_python39_compatibility.py b/tests/test_python39_compatibility.py
index bbfd3be5f5d..176adae3883 100644
--- a/tests/test_python39_compatibility.py
+++ b/tests/test_python39_compatibility.py
@@ -9,6 +9,7 @@ import pytest
REPO_ROOT = Path(__file__).resolve().parents[1]
PACKAGE_ROOT = REPO_ROOT / "unsloth"
+CLI_ROOT = REPO_ROOT / "unsloth_cli"
# studio/ union debt is ratcheted: only shrink this set; fix new files with the future import.
STUDIO_UNION_DEBT_FILES = frozenset(
@@ -351,8 +352,10 @@ def test_no_pep604_unions_are_evaluated_on_the_declared_floor():
known_typing_names = typing_names(tree)
# The future import defers annotations only; an assigned value still runs.
deferred = has_future_annotations(tree)
- in_unsloth = PACKAGE_ROOT in path.parents
- annotation_sources = [] if (deferred or not in_unsloth) else evaluated_annotations(tree)
+ in_floor_package = PACKAGE_ROOT in path.parents or CLI_ROOT in path.parents
+ annotation_sources = (
+ [] if (deferred or not in_floor_package) else evaluated_annotations(tree)
+ )
for expression in annotation_sources:
for inner in ast.walk(expression):
if isinstance(inner, ast.BinOp) and isinstance(inner.op, ast.BitOr):
diff --git a/unsloth_cli/commands/studio.py b/unsloth_cli/commands/studio.py
index 33fdfe9df4a..9e30f111dfb 100644
--- a/unsloth_cli/commands/studio.py
+++ b/unsloth_cli/commands/studio.py
@@ -32,6 +32,12 @@ from typer.core import TyperCommand
from unsloth_cli import _studio_deps, _studio_runtime_gate, _studio_stage
from unsloth_cli._inference import SpeculativeType
from unsloth_cli.commands import _password_prompt
+from studio.backend.utils.pid_records import (
+ RecordedStart,
+ pid_reuse_proven,
+ pid_start_matches,
+ recorded_start,
+)
studio_app = typer.Typer(help = "Unsloth Studio commands.")
@@ -3213,11 +3219,13 @@ def _pid_alive(pid: int) -> bool:
encoding = "utf-8",
errors = "replace",
timeout = 10,
- ).stdout
+ )
except Exception:
# Cannot determine; assume alive, taskkill no-ops if already gone.
return True
- return f'"{int(pid)}"' in out
+ if out.returncode != 0:
+ return True
+ return f'"{int(pid)}"' in out.stdout
try:
os.kill(pid, 0)
except ProcessLookupError:
@@ -3227,7 +3235,7 @@ def _pid_alive(pid: int) -> bool:
return True
-def _parse_pid_record(text: str) -> "tuple[int, float | None, str | None] | None":
+def _parse_pid_record(text: str) -> "tuple[int, RecordedStart, str | None] | None":
lines = text.splitlines()
if not lines or not lines[0].strip().isdigit():
return None
@@ -3239,18 +3247,13 @@ def _parse_pid_record(text: str) -> "tuple[int, float | None, str | None] | None
# kill(0) signals our whole process group; kill(1) is init. Never either.
if pid < 2:
return None
- created = None
- if len(lines) > 1:
- try:
- created = float(lines[1].strip())
- except ValueError:
- created = None
+ created = recorded_start(lines)
# Third line: the addresses run.py bound, absent in a legacy record.
address = lines[2].strip() if len(lines) > 2 and lines[2].strip() else None
return pid, created, address
-def _read_pid_record(path: Path) -> "tuple[int, float | None, str | None] | None":
+def _read_pid_record(path: Path) -> "tuple[int, RecordedStart, str | None] | None":
try:
text = path.read_text(encoding = "utf-8")
except (OSError, UnicodeDecodeError):
@@ -3279,11 +3282,11 @@ def _report_unreadable(paths: "list[Path]") -> None:
def _pid_file_entries(
unreadable: "list[Path] | None" = None,
-) -> "list[tuple[int, list[float | None], list[Path]]]":
+) -> "list[tuple[int, list[RecordedStart], list[Path]]]":
"""(pid, create_times, files) per recorded server, including the legacy studio.pid. Grouped by
PID: a second SIGTERM would hard-kill a server mid-shutdown. Every recorded time is kept, so a
stale file cannot veto a live server."""
- by_pid: "dict[int, tuple[list[float | None], list[Path]]]" = {}
+ by_pid: "dict[int, tuple[list[RecordedStart], list[Path]]]" = {}
try:
paths = sorted(STUDIO_HOME.glob(PID_FILE_GLOB)) + [_PID_FILE]
except OSError:
@@ -3313,18 +3316,8 @@ def _pid_file_entries(
return [(pid, times, files) for pid, (times, files) in by_pid.items()]
-def _pid_is_studio_server(pid: int, created_times: "Sequence[float | None]" = ()) -> bool:
- """False only when a recorded start time proves this PID is a different process. Untimed records
- are trusted and must not cancel a timed one: every server writes both."""
- known = [c for c in created_times if c is not None]
- if not known:
- return True
- try:
- import psutil
- actual = psutil.Process(pid).create_time()
- except Exception:
- return True
- return any(abs(actual - c) < 1.0 for c in known)
+def _pid_is_studio_server(pid: int, created_times: "Sequence[RecordedStart]" = ()) -> bool:
+ return pid_start_matches(pid, created_times)
def _graceful_shutdown_on_sigterm() -> None:
@@ -3374,12 +3367,23 @@ def stop():
typer.echo("No running Unsloth server found (no PID file).")
raise typer.Exit(0)
- signalled, failed = [], []
+ signalled, failed, unverified = [], [], []
for pid, created_times, paths in entries:
- if not _pid_alive(pid) or not _pid_is_studio_server(pid, created_times):
+ if not _pid_alive(pid):
for path in paths:
_unlink_quietly(path)
continue
+ if not _pid_is_studio_server(pid, created_times):
+ if pid_reuse_proven(pid, created_times):
+ for path in paths:
+ _unlink_quietly(path)
+ continue
+ unverified.append(pid)
+ typer.echo(
+ f"Could not verify Unsloth server identity (PID {pid}); kept its PID files.",
+ err = True,
+ )
+ continue
error = _signal_stop(pid)
if error is not None:
failed.append((pid, error))
@@ -3389,6 +3393,8 @@ def stop():
signalled.append((pid, paths))
if not signalled and not failed:
+ if unverified:
+ raise typer.Exit(1)
if unreadable:
_report_unreadable(unreadable)
raise typer.Exit(1)
@@ -3414,7 +3420,7 @@ def stop():
typer.echo(f"Unsloth server (PID {pid}) is shutting down (may take a few seconds).")
if unreadable:
_report_unreadable(unreadable)
- if failed or unreadable:
+ if failed or unreadable or unverified:
raise typer.Exit(1)
@@ -4981,7 +4987,7 @@ def _managed_llama_runtime_is_the_active_one() -> bool:
return _llama_runtime_to_grade() is not None
-def _llama_runtime_to_grade() -> Path | None:
+def _llama_runtime_to_grade() -> Optional[Path]:
"""The llama.cpp tree preflight should grade, or None when the runtime the
backend would load is one the user chose and this PR does not grade.
diff --git a/unsloth_cli/tests/test_studio_stop.py b/unsloth_cli/tests/test_studio_stop.py
index a18175dd817..c09c776b074 100644
--- a/unsloth_cli/tests/test_studio_stop.py
+++ b/unsloth_cli/tests/test_studio_stop.py
@@ -129,9 +129,9 @@ def test_stop_does_not_signal_a_reused_pid(monkeypatch, tmp_path):
result = _run_stop(studio_mod)
- assert result.exit_code == 0, result.output
+ assert result.exit_code == 1, result.output
assert killed == []
- assert not (tmp_path / "studio-8901-8550.pid").exists()
+ assert (tmp_path / "studio-8901-8550.pid").exists()
def test_stop_signals_a_live_server_whose_pid_has_a_stale_record(monkeypatch, tmp_path):
@@ -237,9 +237,9 @@ def test_stop_does_not_signal_a_reused_pid_recorded_in_both_files(monkeypatch, t
result = _run_stop(studio_mod)
- assert result.exit_code == 0, result.output
+ assert result.exit_code == 1, result.output
assert killed == []
- assert not list(tmp_path.glob("*.pid"))
+ assert len(list(tmp_path.glob("*.pid"))) == 2
def test_pid_identity_check_trusts_the_record_without_psutil(monkeypatch):
@@ -265,7 +265,7 @@ def test_pid_identity_check_uses_the_recorded_start_time(monkeypatch):
assert studio_mod._pid_is_studio_server(8550, [999.0]) is False
-def test_stop_drops_a_record_whose_start_time_no_longer_matches(monkeypatch, tmp_path):
+def test_stop_keeps_a_live_record_whose_start_time_no_longer_matches(monkeypatch, tmp_path):
studio_mod, _live, killed = _install(monkeypatch, tmp_path, alive = {8550})
monkeypatch.setattr(studio_mod, "_pid_is_studio_server", _REAL_IS_STUDIO_SERVER)
@@ -281,9 +281,9 @@ def test_stop_drops_a_record_whose_start_time_no_longer_matches(monkeypatch, tmp
result = _run_stop(studio_mod)
- assert result.exit_code == 0, result.output
+ assert result.exit_code == 1, result.output
assert killed == []
- assert not (tmp_path / "studio-8901-8550.pid").exists()
+ assert (tmp_path / "studio-8901-8550.pid").exists()
assert "invalid pid file" not in result.output.lower()
@@ -359,7 +359,7 @@ def test_stop_continues_after_one_server_fails_to_stop(monkeypatch, tmp_path):
result = _run_stop(studio_mod)
- combined = (result.output or "") + (getattr(result, "stderr", "") or "")
+ combined = result.output or ""
assert result.exit_code == 1, combined
assert 8600 not in live
assert "8550" in combined
@@ -411,7 +411,7 @@ def test_stop_keeps_a_record_it_cannot_read(monkeypatch, tmp_path):
result = _run_stop(studio_mod)
assert path.exists(), "an unreadable record must not be deleted"
- assert "cannot read" in (result.output + (result.stderr or "")).lower()
+ assert "cannot read" in result.output.lower()
def test_stop_does_not_claim_success_when_the_only_record_is_unreadable(monkeypatch, tmp_path):
@@ -430,7 +430,7 @@ def test_stop_does_not_claim_success_when_the_only_record_is_unreadable(monkeypa
result = _run_stop(studio_mod)
assert result.exit_code == 1, "an unreachable server is not a successful stop"
- output = result.output + (result.stderr or "")
+ output = result.output
assert "no running unsloth server" not in output.lower()
assert killed == []
@@ -485,3 +485,56 @@ def test_a_record_whose_pid_is_not_ascii_digits_is_discarded(monkeypatch, tmp_pa
assert result.exit_code == 0, result.output
assert not (tmp_path / "studio-8901-1.pid").exists()
+
+
+ "body, actual",
+ [
+ ("8550\n1000.0\n127.0.0.1\nlinux:boot:123\n", 4600.0),
+ ("8550\n1000.0\n127.0.0.1", 999.0),
+ ],
+ ids = ["stable-clock-shift", "legacy-one-second-boundary"],
+)
+def test_stop_recognizes_a_live_record_after_an_epoch_shift(monkeypatch, tmp_path, body, actual):
+ from studio.backend.utils import pid_records
+
+ studio_mod, _live, killed = _install(monkeypatch, tmp_path, alive = {8550})
+ monkeypatch.setattr(studio_mod, "_pid_is_studio_server", _REAL_IS_STUDIO_SERVER)
+ monkeypatch.setattr(pid_records, "linux_process_identity", lambda pid: "linux:boot:123")
+ monkeypatch.setitem(
+ sys.modules,
+ "psutil",
+ SimpleNamespace(Process = lambda pid: SimpleNamespace(create_time = lambda: actual)),
+ )
+ (tmp_path / "studio-8901-8550.pid").write_text(body, encoding = "utf-8")
+ result = _run_stop(studio_mod)
+ assert result.exit_code == 0, result.output
+ assert killed == [8550]
+
+
+def test_windows_failed_liveness_probe_preserves_the_record(monkeypatch):
+ import subprocess
+
+ studio_mod = _studio()
+ monkeypatch.setattr(sys, "platform", "win32")
+ monkeypatch.setattr(
+ subprocess, "run", lambda *args, **kwargs: SimpleNamespace(stdout = "", returncode = 1)
+ )
+ assert studio_mod._pid_alive(8550) is True
+
+
[email protected](sys.platform != "linux", reason = "needs /proc for the stable identity")
+def test_stop_drops_a_record_whose_stable_identity_proves_pid_reuse(monkeypatch, tmp_path):
+ import os
+
+ studio_mod, _live, killed = _install(monkeypatch, tmp_path, alive = {os.getpid()})
+ monkeypatch.setattr(studio_mod, "_pid_is_studio_server", _REAL_IS_STUDIO_SERVER)
+ boot = Path("/proc/sys/kernel/random/boot_id").read_text(encoding = "ascii").strip()
+ record = tmp_path / f"studio-8901-{os.getpid()}.pid"
+ record.write_text(f"{os.getpid()}\n111.5\n127.0.0.1\nlinux:{boot}:1\n", encoding = "utf-8")
+
+ result = _run_stop(studio_mod)
+
+ assert result.exit_code == 0, result.output
+ assert killed == []
+ assert not record.exists()