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: fewer GC passes while the backend imports, RSA key generated alongside startup (#15586)

Daniel Han7362fbc0e8ae
diff --git a/studio/backend/core/inference/key_exchange.py b/studio/backend/core/inference/key_exchange.py
index ae7db920b17..409fe2e0231 100644
--- a/studio/backend/core/inference/key_exchange.py
+++ b/studio/backend/core/inference/key_exchange.py
@@ -7,6 +7,7 @@ memory, new per start; GET /api/providers/public-key"""
import base64
import hashlib
import logging
+import threading
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
@@ -28,9 +29,40 @@ def _compute_fingerprint(pem: str) -> str:
return hashlib.sha256(pem.encode("utf-8")).hexdigest()[:16]
-def init_key_pair() -> None:
- """Generate an RSA-2048 key pair. Called once at server startup."""
+def _generate_private_key() -> rsa.RSAPrivateKey:
+ return rsa.generate_private_key(
+ public_exponent = 65537,
+ key_size = 2048,
+ )
+
+
+def start_key_pair():
+ """Generate the key on a thread (keygen releases the GIL); the returned step joins, re-raises, installs."""
+ result = {}
+
+ def _generate():
+ try:
+ result["key"] = _generate_private_key()
+ except BaseException as exc: # noqa: BLE001 -- re-raised by the install step
+ result["error"] = exc
+
+ thread = threading.Thread(target = _generate, name = "rsa-keygen", daemon = True)
+ thread.start()
+
+ def install() -> None:
+ thread.join()
+ if "error" in result:
+ raise result["error"]
+ init_key_pair(result["key"])
+
+ return install
+
+
+def init_key_pair(private_key: rsa.RSAPrivateKey | None = None) -> None:
+ """Install an RSA-2048 key pair, generated here unless given. Called once at server startup."""
global _private_key, _public_key_pem, _public_key_fingerprint
+ if private_key is None:
+ private_key = _generate_private_key()
if _private_key is not None:
logger.warning(
"init_key_pair called again — replacing existing RSA keypair "
@@ -38,10 +70,7 @@ def init_key_pair() -> None:
"public key will start hitting decryption failures.",
_public_key_fingerprint,
)
- _private_key = rsa.generate_private_key(
- public_exponent = 65537,
- key_size = 2048,
- )
+ _private_key = private_key
_public_key_pem = (
_private_key.public_key()
.public_bytes(
diff --git a/studio/backend/main.py b/studio/backend/main.py
index ebba73103a1..7d401118dd0 100644
--- a/studio/backend/main.py
+++ b/studio/backend/main.py
@@ -792,6 +792,10 @@ async def lifespan(app: FastAPI):
import structlog as _structlog
_lifespan_log = _structlog.get_logger(__name__)
+ # Started first so keygen overlaps the steps below.
+ from core.inference.key_exchange import start_key_pair
+
+ _install_key_pair = start_key_pair()
clear_compiled_cache_unless_shared(app)
# Without a catch-all the engine paths answer 405, read as 'exists'.
@@ -969,9 +973,7 @@ async def lifespan(app: FastAPI):
app.state.idle_unload_task = asyncio.create_task(idle_unload_loop())
# Initialize RSA key pair for API key encryption (external providers).
- from core.inference.key_exchange import init_key_pair
-
- init_key_pair()
+ _install_key_pair()
# Stall thread-dump watchdog (#9712), only with UNSLOTH_STUDIO_STALL_WATCHDOG=1.
from utils.stall_watchdog import stand_down_for_the_warm, start_stall_watchdog
diff --git a/studio/backend/run.py b/studio/backend/run.py
index 495e68792da..b13364eaca5 100644
--- a/studio/backend/run.py
+++ b/studio/backend/run.py
@@ -2427,7 +2427,16 @@ def run_server(
pass
write_startup_marker()
- from main import app, setup_frontend, _desktop_owner, _IS_COLAB
+ import gc
+
+ # The import builds ~330k long-lived objects the default threshold rescans ~270 times; 0 (GC off) is left alone.
+ _gc_threshold = gc.get_threshold()
+ if _gc_threshold[0]:
+ gc.set_threshold(max(_gc_threshold[0], 100_000), *_gc_threshold[1:])
+ try:
+ from main import app, setup_frontend, _desktop_owner, _IS_COLAB
+ finally:
+ gc.set_threshold(*_gc_threshold)
# A callable, re-asked at shutdown. On app.state: main.py must not import this module.
app.state.live_sibling_backend = live_sibling_backend
diff --git a/studio/backend/tests/test_backend_import_gc_threshold.py b/studio/backend/tests/test_backend_import_gc_threshold.py
new file mode 100644
index 00000000000..151efe93e2b
--- /dev/null
+++ b/studio/backend/tests/test_backend_import_gc_threshold.py
@@ -0,0 +1,41 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+from __future__ import annotations
+
+import ast
+from pathlib import Path
+
+_RUN = Path(__file__).resolve().parent.parent / "run.py"
+
+
+def _run_server() -> ast.FunctionDef:
+ tree = ast.parse(_RUN.read_text(encoding = "utf-8"))
+ return next(
+ node
+ for node in ast.walk(tree)
+ if isinstance(node, ast.FunctionDef) and node.name == "run_server"
+ )
+
+
+def _is_main_import(node) -> bool:
+ return isinstance(node, ast.ImportFrom) and node.module == "main"
+
+
+def test_main_is_imported_inside_a_try_that_restores_the_threshold():
+ tries = [
+ node
+ for node in ast.walk(_run_server())
+ if isinstance(node, ast.Try) and any(_is_main_import(n) for n in node.body)
+ ]
+ assert len(tries) == 1
+ restore = ast.unparse(tries[0].finalbody)
+ assert "gc.set_threshold(*_gc_threshold)" in restore
+
+
+def test_the_threshold_is_raised_before_the_import_and_only_when_gc_runs():
+ src = ast.unparse(_run_server())
+ raised = src.index("gc.set_threshold(max(_gc_threshold[0], 100000)")
+ assert src.index("_gc_threshold = gc.get_threshold()") < raised
+ assert src.index("if _gc_threshold[0]:") < raised < src.index("from main import app")
+ assert src.count("gc.set_threshold(") == 2
diff --git a/studio/backend/tests/test_key_pair_startup_overlap.py b/studio/backend/tests/test_key_pair_startup_overlap.py
new file mode 100644
index 00000000000..145595c3688
--- /dev/null
+++ b/studio/backend/tests/test_key_pair_startup_overlap.py
@@ -0,0 +1,71 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+from __future__ import annotations
+
+import inspect
+import threading
+
+import pytest
+
+from core.inference import key_exchange
+
+
[email protected](autouse = True)
+def _restore_key_pair():
+ saved = (
+ key_exchange._private_key,
+ key_exchange._public_key_pem,
+ key_exchange._public_key_fingerprint,
+ )
+ yield
+ (
+ key_exchange._private_key,
+ key_exchange._public_key_pem,
+ key_exchange._public_key_fingerprint,
+ ) = saved
+
+
+def test_install_publishes_the_key_generated_off_thread(monkeypatch):
+ seen = []
+ real = key_exchange._generate_private_key
+
+ def _generate():
+ seen.append(threading.current_thread() is not threading.main_thread())
+ return real()
+
+ monkeypatch.setattr(key_exchange, "_generate_private_key", _generate)
+ install = key_exchange.start_key_pair()
+ install()
+ assert seen == [True]
+ pem = key_exchange.get_public_key_pem()
+ assert pem.startswith("-----BEGIN PUBLIC KEY-----")
+ assert key_exchange.get_public_key_fingerprint() == key_exchange._compute_fingerprint(pem)
+
+
+def test_install_raises_what_generation_raised(monkeypatch):
+ def _fail():
+ raise RuntimeError("no entropy")
+
+ monkeypatch.setattr(key_exchange, "_generate_private_key", _fail)
+ install = key_exchange.start_key_pair()
+ with pytest.raises(RuntimeError, match = "no entropy"):
+ install()
+
+
+def test_init_key_pair_still_generates_synchronously():
+ key_exchange.init_key_pair()
+ first = key_exchange.get_public_key_fingerprint()
+ key_exchange.init_key_pair()
+ assert first is not None and key_exchange.get_public_key_fingerprint() != first
+
+
+def test_lifespan_starts_keygen_first_and_installs_it_before_serving():
+ import main
+
+ source = inspect.getsource(main.lifespan)
+ start = source.index("start_key_pair()")
+ assert start < source.index("clear_compiled_cache_unless_shared(app)")
+ install = source.index("_install_key_pair()")
+ assert start < install < source.index("start_stall_watchdog(")
+ assert install < source.index("yield")