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: find the backend's child processes from /proc on Linux for /api/system (#15592)
Daniel Han7b81949674c3
diff --git a/studio/backend/main.py b/studio/backend/main.pyindex 6a2affe266c..4591d11742e 100644--- a/studio/backend/main.py+++ b/studio/backend/main.py@@ -98,10 +98,25 @@ def _process_tree_rss_mb(process: Any) -> int:import psutiltotal = process.memory_info().rss- try:- children = process.children(recursive = True)- except (psutil.NoSuchProcess, psutil.AccessDenied):- children = []+ children = None+ pid = getattr(process, "pid", None)+ if isinstance(pid, int):+ from utils.process_tree import linux_descendant_pids++ # Linux walks the tree itself; psutil's recursive children() reads every process on the host.+ descendants = linux_descendant_pids(pid)+ if descendants is not None:+ children = []+ for child_pid in descendants:+ try:+ children.append(psutil.Process(child_pid))+ except psutil.Error:+ continue+ if children is None:+ try:+ children = process.children(recursive = True)+ except (psutil.NoSuchProcess, psutil.AccessDenied):+ children = []for child in children:try:total += child.memory_info().rssdiff --git a/studio/backend/tests/test_process_tree_fast_path.py b/studio/backend/tests/test_process_tree_fast_path.pynew file mode 100644index 00000000000..ddcdd28d116--- /dev/null+++ b/studio/backend/tests/test_process_tree_fast_path.py@@ -0,0 +1,131 @@+# 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 /proc walk finds what psutil finds, and psutil answers whenever the walk cannot."""++from __future__ import annotations++import os+import subprocess+import sys+import threading+import time++import pytest++from utils import process_tree++linux_only = pytest.mark.skipif(not sys.platform.startswith("linux"), reason = "Linux /proc walk")+++def _sleeper():+ return subprocess.Popen([sys.executable, "-c", "import time; time.sleep(60)"])+++def tree():+ nested = (+ "import subprocess, sys, time; "+ "[subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(60)']) for _ in range(2)]; "+ "time.sleep(60)"+ )+ psutil = pytest.importorskip("psutil")+ me = psutil.Process()+ # Earlier tests in this session can leave children of their own; count only ours.+ before = {child.pid for child in me.children(recursive = True)}+ procs = [_sleeper(), subprocess.Popen([sys.executable, "-c", nested])]+ # A live thread lists its own children (they move to the main task once it exits): keep it alive.+ forked = threading.Event()+ release = threading.Event()++ def fork_and_wait():+ procs.append(_sleeper())+ forked.set()+ release.wait(60)++ worker = threading.Thread(target = fork_and_wait, daemon = True)+ worker.start()+ forked.wait(30)+ deadline = time.monotonic() + 30++ def ours():+ return {child.pid for child in me.children(recursive = True)} - before++ while len(ours()) < 5 and time.monotonic() < deadline:+ time.sleep(0.1)+ yield me+ for child in me.children(recursive = True):+ if child.pid in before:+ continue+ try:+ child.kill()+ except psutil.Error:+ pass+ for proc in procs:+ proc.wait()+ release.set()+ worker.join()+++@linux_only+def test_the_walk_finds_what_psutil_finds(tree):+ # Another test's leftover child can exit between the two reads; a real difference persists.+ for _ in range(3):+ expected = sorted(child.pid for child in tree.children(recursive = True))+ walked = sorted(process_tree.linux_descendant_pids(os.getpid()))+ if walked == expected:+ break+ assert len(expected) >= 5+ assert walked == expected+++def test_other_platforms_defer_to_psutil(monkeypatch):+ monkeypatch.setattr(process_tree.sys, "platform", "darwin")+ assert process_tree.linux_descendant_pids(os.getpid()) is None+++@linux_only+def test_a_kernel_without_children_files_defers_to_psutil(tmp_path, monkeypatch):+ (tmp_path / "123" / "task" / "123").mkdir(parents = True)+ monkeypatch.setattr(process_tree, "_PROC", str(tmp_path))+ assert process_tree.linux_descendant_pids(123) is None+++@linux_only+def test_a_vanished_root_defers_to_psutil(tmp_path, monkeypatch):+ monkeypatch.setattr(process_tree, "_PROC", str(tmp_path))+ assert process_tree.linux_descendant_pids(123) is None+++@linux_only+def test_a_descendant_exiting_mid_walk_is_skipped(tmp_path, monkeypatch):+ root = tmp_path / "1" / "task" / "1"+ root.mkdir(parents = True)+ (root / "children").write_text("2 3 ", encoding = "utf-8")+ # 2 has exited (no /proc entry); 3 is alive with a child of its own.+ alive = tmp_path / "3" / "task" / "3"+ alive.mkdir(parents = True)+ (alive / "children").write_text("4", encoding = "utf-8")+ monkeypatch.setattr(process_tree, "_PROC", str(tmp_path))+ assert sorted(process_tree.linux_descendant_pids(1)) == [2, 3, 4]+++@linux_only+def test_a_thread_vanishing_mid_walk_defers_to_psutil(tmp_path, monkeypatch):+ # Thread 7 exited mid-walk; its children may have moved to thread 1, already read.+ root = tmp_path / "1" / "task"+ (root / "1").mkdir(parents = True)+ (root / "1" / "children").write_text("2", encoding = "utf-8")+ (root / "7").mkdir()+ monkeypatch.setattr(process_tree, "_PROC", str(tmp_path))+ assert process_tree.linux_descendant_pids(1) is None+++@linux_only+def test_tree_memory_uses_the_walk_and_falls_back_to_psutil(tree, monkeypatch):+ import main++ walked = main._process_tree_rss_mb(tree)+ monkeypatch.setattr(process_tree, "linux_descendant_pids", lambda pid: None)+ scanned = main._process_tree_rss_mb(tree)+ assert abs(walked - scanned) <= max(8, scanned // 20)diff --git a/studio/backend/utils/process_tree.py b/studio/backend/utils/process_tree.pynew file mode 100644index 00000000000..6076ed8e02e--- /dev/null+++ b/studio/backend/utils/process_tree.py@@ -0,0 +1,49 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Descendants of one process from /proc/<pid>/task/<tid>/children, so /api/system polls skip+psutil's recursive children(), which reads every process on the host (~180 ms at ~10k processes)."""++import os+import sys+from typing import Optional++_PROC = "/proc"+++def linux_descendant_pids(pid: int) -> Optional[list[int]]:+ """Every descendant of ``pid``, or None (not Linux, no CONFIG_PROC_CHILDREN, root gone, any error)+ so the caller asks psutil. Exited descendants are skipped, as psutil skips them."""+ if not sys.platform.startswith("linux"):+ return None+ try:+ root_tasks = os.listdir(f"{_PROC}/{pid}/task")+ # Unsupported kernel: the file is absent for a live task.+ if not root_tasks or not os.path.exists(f"{_PROC}/{pid}/task/{root_tasks[0]}/children"):+ return None+ found: list[int] = []+ seen = {pid}+ pending = [pid]+ while pending:+ parent = pending.pop()+ try:+ tasks = root_tasks if parent == pid else os.listdir(f"{_PROC}/{parent}/task")+ except (FileNotFoundError, ProcessLookupError):+ continue+ for tid in tasks:+ try:+ with open(f"{_PROC}/{parent}/task/{tid}/children", "rb") as handle:+ listed = handle.read().split()+ except (FileNotFoundError, ProcessLookupError):+ # A thread that exits mid-walk reparents its live children to a sibling task whose+ # file may already be read, so this walk could miss them: psutil answers instead.+ return None+ for raw in listed:+ child = int(raw)+ if child not in seen:+ seen.add(child)+ found.append(child)+ pending.append(child)+ return found+ except Exception: # noqa: BLE001 -- psutil answers instead+ return None