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.

Tests: stop pytest 9.2 monkeypatch from deleting torch config entries, and name any test that still does (#15626)

Daniel Hanfc03e5377e22
diff --git a/studio/backend/tests/conftest.py b/studio/backend/tests/conftest.py
index ee973a9ac06..692d8212c51 100644
--- a/studio/backend/tests/conftest.py
+++ b/studio/backend/tests/conftest.py
@@ -51,6 +51,11 @@ for _up in Path(__file__).resolve().parents:
sys.path.insert(0, str(_repo_shared))
break
+# pytest 9.2's monkeypatch deletes a torch config entry on undo instead of restoring it; see torch_config_guard.py.
+from torch_config_guard import install_monkeypatch_fix # noqa: E402
+
+install_monkeypatch_fix()
+
# Let the diffusion patch backend lazily import unsloth_zoo on a CPU-only test host: unsloth_zoo runs accelerator
# detection at import and raises without a GPU unless this is set. setdefault so an explicit override wins.
os.environ.setdefault("UNSLOTH_ALLOW_CPU", "1")
@@ -98,6 +103,17 @@ def _no_vae_structural_verdict_cache(monkeypatch):
monkeypatch.setenv("UNSLOTH_VAE_FUSED_STRUCTURAL_CACHE", "0")
[email protected](autouse = True)
+def _no_deleted_torch_config_entries():
+ """A test that leaves a dynamo / inductor config entry deleted errors at its own teardown; see
+ tests/_shared/torch_config_guard.py."""
+ from torch_config_guard import check_after_test, install_delete_recorder
+
+ install_delete_recorder()
+ yield
+ check_after_test()
+
+
@pytest.fixture(autouse = True)
def _zoo_without_gdn_decode_kernel(monkeypatch):
# Whether Qwen3.5 / Qwen3-Next ask for causal-conv1d depends on the installed unsloth_zoo; pin the
diff --git a/tests/_shared/torch_config_guard.py b/tests/_shared/torch_config_guard.py
new file mode 100644
index 00000000000..829ed1fcdf7
--- /dev/null
+++ b/tests/_shared/torch_config_guard.py
@@ -0,0 +1,165 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""Names the test that leaves a torch dynamo / inductor config entry deleted, then puts the entry back.
+
+``del torch._dynamo.config.recompile_limit`` (also what ``unittest.mock.patch`` does on exit before it re-creates
+the attribute) marks the entry hidden, and every later read in that process raises
+``torch._dynamo.config.recompile_limit does not exist``. One leak on an xdist worker used to fail every compile
+test that worker ran afterwards, with no hint of where it came from.
+
+pytest 9.2 made that the normal outcome of ``monkeypatch.setattr(torch._dynamo.config, ...)``: for a non-class target
+it now records the old value from the instance ``__dict__`` (pytest #10644), where a config entry never lives, so
+``undo()`` deletes the entry instead of restoring it. ``install_monkeypatch_fix`` records the real value instead."""
+
+from __future__ import annotations
+
+import sys
+import threading
+import traceback
+from typing import Any
+
+CONFIG_MODULES = (
+ "torch._dynamo.config",
+ "torch._inductor.config",
+ "torch._functorch.config",
+)
+
+_LOCK = threading.Lock()
+# (module, key) -> (user_override before the delete, stack of the delete)
+_DELETES: dict[tuple[str, str], tuple[Any, str]] = {}
+_installed = False
+
+
+def _entries(module_name: str) -> dict[str, Any] | None:
+ module = sys.modules.get(module_name)
+ # vars(): a ConfigModule answers unknown attributes through __getattr__.
+ entries = vars(module).get("_config") if module is not None else None
+ return entries if isinstance(entries, dict) else None
+
+
+def install_delete_recorder() -> bool:
+ """Wrap ConfigModule.__delattr__ to remember who deleted what; a no-op until torch is imported."""
+ global _installed
+ if _installed:
+ return True
+ config_module = sys.modules.get("torch.utils._config_module")
+ cls = getattr(config_module, "ConfigModule", None) if config_module is not None else None
+ if cls is None:
+ return False
+ original = cls.__delattr__
+
+ def __delattr__(self, name: str) -> None:
+ entries = vars(self).get("_config")
+ entry = entries.get(name) if isinstance(entries, dict) else None
+ if entry is not None:
+ with _LOCK:
+ _DELETES[(self.__name__, name)] = (
+ entry.user_override,
+ "".join(traceback.format_stack(limit = 16)[:-1]),
+ )
+ return original(self, name)
+
+ cls.__delattr__ = __delattr__
+ _installed = True
+ return True
+
+
+def _is_torch_config(target: Any) -> bool:
+ config_module = sys.modules.get("torch.utils._config_module")
+ if config_module is None:
+ return False
+ kinds = tuple(
+ k
+ for k in (
+ getattr(config_module, "ConfigModule", None),
+ getattr(config_module, "SubConfigProxy", None),
+ )
+ if k
+ )
+ return bool(kinds) and isinstance(target, kinds)
+
+
+def install_monkeypatch_fix() -> None:
+ """Make MonkeyPatch.setattr on a torch config entry restore the value it replaced rather than delete the entry."""
+ import pytest
+ from _pytest import monkeypatch as mp_module
+
+ cls = pytest.MonkeyPatch
+ if getattr(cls.setattr, "_unsloth_torch_config_fix", False):
+ return
+ notset = getattr(mp_module, "NOTSET", None) or getattr(mp_module, "notset")
+ original = cls.setattr
+
+ def setattr(
+ self,
+ target,
+ name = notset,
+ value = notset,
+ raising = True,
+ ):
+ obj, attr = target, name
+ if isinstance(target, str) and value is notset:
+ try:
+ attr, obj = mp_module.derive_importpath(target, raising)
+ except Exception: # noqa: BLE001 - let the real setattr raise its own error
+ obj = None
+ real = (
+ getattr(obj, attr, notset)
+ if isinstance(attr, str) and _is_torch_config(obj)
+ else notset
+ )
+ result = original(self, target, name, value, raising = raising)
+ if real is not notset and self._setattr:
+ last_obj, last_attr, last_old = self._setattr[-1]
+ if last_obj is obj and last_attr == attr and last_old is notset:
+ self._setattr[-1] = (last_obj, last_attr, real)
+ return result
+
+ setattr._unsloth_torch_config_fix = True
+ cls.setattr = setattr
+
+
+def hidden_entries() -> list[tuple[str, str]]:
+ """Config entries that are currently deleted (read as ``... does not exist``)."""
+ found = []
+ for module_name in CONFIG_MODULES:
+ entries = _entries(module_name)
+ for key, entry in (entries or {}).items():
+ if getattr(entry, "hide", False):
+ found.append((module_name, key))
+ return found
+
+
+def restore(hidden: list[tuple[str, str]]) -> str:
+ """Un-hide ``hidden`` with the value each held before its delete; returns a report naming the deleters."""
+ lines = []
+ for module_name, key in hidden:
+ entry = (_entries(module_name) or {}).get(key)
+ if entry is None:
+ continue
+ with _LOCK:
+ prior = _DELETES.pop((module_name, key), None)
+ if prior is not None:
+ entry.user_override = prior[0]
+ entry.hide = False
+ module = sys.modules.get(module_name)
+ if module is not None:
+ vars(module)["_is_dirty"] = True
+ lines.append(f"{module_name}.{key} was deleted and never re-created")
+ lines.append(
+ prior[1] if prior is not None else " (deleted before the recorder was installed)\n"
+ )
+ return "\n".join(lines)
+
+
+def check_after_test() -> None:
+ """Raise if the test just run left a config entry deleted; the entries are restored first."""
+ install_delete_recorder()
+ hidden = hidden_entries()
+ if hidden:
+ report = restore(hidden)
+ raise AssertionError(
+ "this test left torch config entries deleted, which breaks every later compile in the process:\n"
+ + report
+ )
diff --git a/tests/conftest.py b/tests/conftest.py
index 5565d47bc31..c95c9b8a9fe 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -50,6 +50,11 @@ _shared_dir = _iso.parent / "_shared"
if _shared_dir.is_dir() and str(_shared_dir) not in _sys.path:
_sys.path.insert(0, str(_shared_dir))
+# pytest 9.2's monkeypatch deletes a torch config entry on undo instead of restoring it; see torch_config_guard.py.
+from torch_config_guard import install_monkeypatch_fix # noqa: E402
+
+install_monkeypatch_fix()
+
import importlib.util
import os
import sys
@@ -70,6 +75,17 @@ def _contain_installer_venv_root(tmp_path_factory, monkeypatch):
contain_installer_venv_root(monkeypatch, tmp_path_factory)
[email protected](autouse = True)
+def _no_deleted_torch_config_entries():
+ """A test that leaves a dynamo / inductor config entry deleted errors at its own teardown; see
+ tests/_shared/torch_config_guard.py."""
+ from torch_config_guard import check_after_test, install_delete_recorder
+
+ install_delete_recorder()
+ yield
+ check_after_test()
+
+
def _has_real_accelerator() -> bool:
"""Calls the probe before any spoof; the name is pinned by a test that reads this file's AST."""
from real_accelerator import has_real_accelerator
diff --git a/tests/test_torch_config_guard.py b/tests/test_torch_config_guard.py
new file mode 100644
index 00000000000..b4ac940e064
--- /dev/null
+++ b/tests/test_torch_config_guard.py
@@ -0,0 +1,118 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""tests/_shared/torch_config_guard.py: a deleted dynamo / inductor config entry is found, named and restored."""
+
+from __future__ import annotations
+
+import ast
+from pathlib import Path
+from unittest import mock
+
+import pytest
+
+torch = pytest.importorskip("torch")
+import torch._dynamo.config # noqa: E402
+import torch._inductor.config # noqa: E402
+
+import torch_config_guard as guard # noqa: E402
+
+REPO = Path(__file__).resolve().parents[1]
+_LIMIT = (
+ "recompile_limit" if hasattr(torch._dynamo.config, "recompile_limit") else "cache_size_limit"
+)
+
+
+def test_a_deleted_entry_is_found_named_and_restored(monkeypatch):
+ assert guard.install_delete_recorder()
+ monkeypatch.setattr(torch._dynamo.config, _LIMIT, 37)
+ delattr(torch._dynamo.config, _LIMIT)
+ with pytest.raises(AttributeError, match = "does not exist"):
+ getattr(torch._dynamo.config, _LIMIT)
+
+ hidden = guard.hidden_entries()
+ assert ("torch._dynamo.config", _LIMIT) in hidden
+ report = guard.restore(hidden)
+
+ assert f"torch._dynamo.config.{_LIMIT} was deleted" in report
+ assert "test_a_deleted_entry_is_found_named_and_restored" in report
+ assert getattr(torch._dynamo.config, _LIMIT) == 37
+ assert guard.hidden_entries() == []
+
+
+def test_check_after_test_raises_and_leaves_the_process_clean():
+ guard.install_delete_recorder()
+ before = torch._inductor.config.emulate_precision_casts
+ delattr(torch._inductor.config, "emulate_precision_casts")
+ with pytest.raises(AssertionError, match = "emulate_precision_casts was deleted"):
+ guard.check_after_test()
+ assert torch._inductor.config.emulate_precision_casts == before
+ guard.check_after_test()
+
+
+def test_mock_patch_round_trip_is_not_flagged():
+ # mock.patch deletes on exit and re-creates right after; only a delete left in place is a leak.
+ with mock.patch.object(torch._dynamo.config, _LIMIT, 5):
+ assert getattr(torch._dynamo.config, _LIMIT) == 5
+ assert guard.hidden_entries() == []
+
+
[email protected]("conftest", ["tests/conftest.py", "studio/backend/tests/conftest.py"])
+def test_both_suites_run_the_guard_after_every_test(conftest):
+ tree = ast.parse((REPO / conftest).read_text(encoding = "utf-8"))
+ fixture = next(
+ (
+ n
+ for n in tree.body
+ if isinstance(n, ast.FunctionDef) and n.name == "_no_deleted_torch_config_entries"
+ ),
+ None,
+ )
+ assert fixture is not None, f"{conftest} lost the torch config guard fixture"
+ decorator = ast.unparse(fixture.decorator_list[0])
+ assert "autouse = True" in decorator.replace("autouse=True", "autouse = True")
+ calls = [ast.unparse(n.func) for n in ast.walk(fixture) if isinstance(n, ast.Call)]
+ assert "check_after_test" in calls and "install_delete_recorder" in calls
+ assert any(isinstance(n, ast.Expr) and isinstance(n.value, ast.Yield) for n in fixture.body)
+
+
+ "target, name",
+ [
+ ("torch._dynamo.config", "suppress_errors"),
+ ("torch._inductor.config", "emulate_precision_casts"),
+ ("torch._inductor.config.triton", "cudagraphs"),
+ ],
+)
+def test_monkeypatch_undo_restores_a_config_entry_instead_of_deleting_it(target, name):
+ # pytest 9.2 records a non-class target's old value from its __dict__, which a config entry is never in.
+ owner = torch
+ for part in target.split(".")[1:]:
+ owner = getattr(owner, part)
+ before = getattr(owner, name)
+ mp = pytest.MonkeyPatch()
+ mp.setattr(owner, name, not before)
+ assert getattr(owner, name) is (not before)
+ mp.undo()
+ assert guard.hidden_entries() == []
+ assert getattr(owner, name) == before
+
+
+def test_monkeypatch_undo_restores_a_config_entry_named_by_import_path():
+ before = torch._dynamo.config.suppress_errors
+ mp = pytest.MonkeyPatch()
+ mp.setattr("torch._dynamo.config.suppress_errors", not before)
+ mp.undo()
+ assert guard.hidden_entries() == []
+ assert torch._dynamo.config.suppress_errors == before
+
+
+def test_monkeypatch_on_an_ordinary_object_is_unchanged():
+ class Target:
+ pass
+
+ target = Target()
+ mp = pytest.MonkeyPatch()
+ mp.setattr(target, "flag", True, raising = False)
+ mp.undo()
+ assert not hasattr(target, "flag")