Files
OBLITERATUS/tests/test_distributed_launcher_edges.py
Joseph MaglyandClaude Opus 5.5 ee9b3ac1bc fix(distributed): restore progress-bar state exactly and on entry failure
Review of 91227c3: toggling Transformers progress bars also flips
huggingface_hub global and per-group progress state, and the disable ran
outside the restore boundary. The guard now captures the Transformers
flag and the Hub global and group state, disables inside try/finally, and
restores each piece exactly (best effort per piece, never masking the
original exception). Only the resource tracker is pre-started; forkserver
is not claimed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 14:32:54 -04:00

816 lines
31 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Single-process fail-closed edges of fixed-membership launch and fd containment (#304)."""
from __future__ import annotations
import os
import socket
import subprocess
import sys
import textwrap
import threading
from contextlib import nullcontext
from importlib import import_module
from types import SimpleNamespace
import pytest
import obliteratus.distributed.launcher as launcher
from obliteratus.distributed.contracts import ContractError, RuntimeContractError
from obliteratus.distributed.launcher import (
TorchrunEnvironment,
control_group,
runtime_diagnostic_guard,
validate_network_interface,
)
from tests.test_distributed_launcher import _environment, _profile
pytestmark = pytest.mark.cpu
_NETWORK = {
"master_addr": "10.10.0.10",
"master_port": 29500,
"interface": "eth0",
"allowed_master_cidrs": ["10.10.0.0/24"],
}
# --- TorchrunEnvironment --------------------------------------------------------------
@pytest.mark.parametrize(
"master_addr",
["rendezvous.internal", "8.8.8.8", "10.20.0.5"],
ids=["hostname", "public", "outside-allowlist"],
)
def test_master_endpoint_must_be_private_numeric_and_allowlisted(tmp_path, master_addr):
config = _profile(tmp_path, network={**_NETWORK, "master_addr": master_addr})
with pytest.raises(RuntimeContractError) as raised:
TorchrunEnvironment.from_environ(_environment(MASTER_ADDR=master_addr), config)
assert raised.value.code == "LMS_NETWORK_PROFILE_DENIED"
def test_environment_outside_the_allowlist_is_forbidden_unless_profiled(tmp_path):
environ = _environment(PATH="/usr/bin")
with pytest.raises(RuntimeContractError) as raised:
TorchrunEnvironment.from_environ(environ, _profile(tmp_path))
assert raised.value.code == "LMS_FORBIDDEN_RUNTIME_CAPABILITY"
execution = {
"device_kind": "cuda",
"device_name": "NVIDIA Test GPU",
"compute_capability": "10.0",
"evidence_tier": "candidate_preflight",
"allowed_environment_keys": ["PATH"],
"local_files_only": True,
"trust_remote_code": False,
"allow_runtime_install": False,
"allow_plugins": False,
"allow_compilation": False,
"allow_adapters": False,
"allow_quantization": False,
}
allowed = _profile(tmp_path, execution=execution)
assert TorchrunEnvironment.from_environ(environ, allowed).rank == 0
@pytest.mark.parametrize(
("overrides", "message"),
[
({"LOCAL_WORLD_SIZE": "2"}, "local_world_size disagrees"),
({"GROUP_RANK": "2"}, "group_rank must be smaller"),
({"GROUP_RANK": "1"}, "global, group, and local ranks disagree"),
({"ROLE_RANK": "1"}, "role_world_size disagrees"),
({"MASTER_PORT": "29501"}, "master endpoint disagrees"),
({"NCCL_SOCKET_IFNAME": "eth1"}, "interface disagrees"),
({"RANK": "-1"}, "unsigned decimal integer"),
({"RANK": ""}, "unsigned decimal integer"),
({"RANK": "١"}, "unsigned decimal integer"),
],
)
def test_torchrun_membership_must_match_the_fixed_profile(tmp_path, overrides, message):
with pytest.raises(ContractError, match=message):
TorchrunEnvironment.from_environ(_environment(**overrides), _profile(tmp_path))
# --- validate_network_interface -------------------------------------------------------
def _interfaces(monkeypatch, entries):
monkeypatch.setattr(launcher.psutil, "net_if_addrs", lambda: {"eth0": entries})
def test_missing_interface_is_denied(monkeypatch):
_interfaces(monkeypatch, [])
with pytest.raises(RuntimeContractError, match="unavailable") as raised:
validate_network_interface("eth1", ("10.10.0.0/24",), "10.10.0.10", coordinator=False)
assert raised.value.code == "LMS_NETWORK_PROFILE_DENIED"
def test_interface_without_an_allowlisted_inet_address_is_denied(monkeypatch):
_interfaces(
monkeypatch,
[
SimpleNamespace(family=getattr(socket, "AF_PACKET", -1), address="00:11:22:33:44:55"),
SimpleNamespace(family=socket.AF_INET, address="not-an-address"),
SimpleNamespace(family=socket.AF_INET, address="192.168.1.10"),
SimpleNamespace(family=socket.AF_INET6, address="fd00::10%eth0"),
],
)
with pytest.raises(RuntimeContractError, match="no private allowlisted") as raised:
validate_network_interface("eth0", ("10.10.0.0/24",), "10.10.0.10", coordinator=False)
assert raised.value.code == "LMS_NETWORK_PROFILE_DENIED"
def test_scoped_ipv6_interface_address_binds_the_coordinator(monkeypatch):
_interfaces(
monkeypatch,
[
SimpleNamespace(family=socket.AF_INET, address="10.10.0.10"),
SimpleNamespace(family=socket.AF_INET6, address="fd00::10%eth0"),
],
)
validate_network_interface("eth0", ("fd00::/64",), "fd00::10", coordinator=True)
# A worker only needs some allowlisted address, not the coordinator endpoint.
validate_network_interface("eth0", ("fd00::/64",), "fd00::99", coordinator=False)
with pytest.raises(RuntimeContractError, match="not bound"):
validate_network_interface("eth0", ("fd00::/64",), "fd00::99", coordinator=True)
# --- control_group --------------------------------------------------------------------
def test_control_group_refuses_an_existing_process_group(tmp_path, monkeypatch):
config = _profile(tmp_path)
launch = TorchrunEnvironment.from_environ(_environment(), config)
monkeypatch.setattr("torch.distributed.is_available", lambda: True)
monkeypatch.setattr("torch.distributed.is_initialized", lambda: True)
with pytest.raises(ContractError, match="already initialized"):
with control_group(config, launch):
pytest.fail("an existing process group must never be reused")
@pytest.mark.parametrize(
("error", "code"),
[
(RuntimeError("private endpoint"), "LMS_COLLECTIVE_FAILED"),
(type("DistStoreError", (RuntimeError,), {})("private endpoint"), "LMS_MEMBERSHIP_TIMEOUT"),
],
)
def test_control_group_initialization_failure_maps_to_stable_codes(
tmp_path, monkeypatch, error, code
):
config = _profile(tmp_path)
launch = TorchrunEnvironment.from_environ(_environment(), config)
destroyed = []
monkeypatch.setattr(launcher, "validate_network_interface", lambda *a, **k: None)
monkeypatch.setattr("torch.distributed.is_available", lambda: True)
monkeypatch.setattr("torch.distributed.is_initialized", lambda: False)
def fail_init(*_args, **_kwargs):
raise error
monkeypatch.setattr("torch.distributed.init_process_group", fail_init)
monkeypatch.setattr("torch.distributed.destroy_process_group", lambda: destroyed.append(1))
with pytest.raises(RuntimeContractError) as raised:
with control_group(config, launch):
pytest.fail("body must not run after failed initialization")
assert raised.value.code == code
assert "private" not in str(raised.value)
assert destroyed == []
# --- _backend_diagnostic_guard --------------------------------------------------------
def test_backend_guard_reports_unavailable_containment(monkeypatch):
def no_pipe():
raise OSError("no descriptors")
monkeypatch.setattr(launcher.os, "pipe", no_pipe)
with pytest.raises(RuntimeContractError, match="containment is unavailable") as raised:
with launcher._backend_diagnostic_guard():
pytest.fail("body must not run without containment")
assert raised.value.code == "LMS_DIAGNOSTIC_REDACTION_FAILED"
def test_backend_guard_closes_pipe_when_stderr_cannot_be_saved(monkeypatch):
launcher._prepare_diagnostic_helpers()
created = []
original_pipe = os.pipe
def record_pipe():
pair = original_pipe()
created.extend(pair)
return pair
def no_dup(_descriptor):
raise OSError("dup failed")
monkeypatch.setattr(launcher.os, "pipe", record_pipe)
monkeypatch.setattr(launcher.os, "dup", no_dup)
with pytest.raises(RuntimeContractError, match="containment is unavailable"):
with launcher._backend_diagnostic_guard():
pass
for descriptor in created:
with pytest.raises(OSError):
os.fstat(descriptor)
def test_backend_guard_restore_failure_is_reported(monkeypatch):
original_dup2 = os.dup2
calls = []
armed = [True]
def restore_then_fail(source, target, *args, **kwargs):
result = original_dup2(source, target, *args, **kwargs)
if not armed[0]:
return result
calls.append(target)
if len(calls) == 2:
raise OSError("restore failed")
return result
monkeypatch.setattr(launcher.os, "dup2", restore_then_fail)
try:
with pytest.raises(RuntimeContractError) as raised:
with launcher._backend_diagnostic_guard():
pass
finally:
armed[0] = False
assert raised.value.code == "LMS_DIAGNOSTIC_REDACTION_FAILED"
assert calls == [2, 2]
def test_backend_guard_reader_failure_is_reported(monkeypatch):
def fail_read(_descriptor, _size):
raise OSError("read failed")
monkeypatch.setattr(launcher.os, "read", fail_read)
with pytest.raises(RuntimeContractError) as raised:
with launcher._backend_diagnostic_guard():
pass
assert raised.value.code == "LMS_DIAGNOSTIC_REDACTION_FAILED"
# --- runtime_diagnostic_guard ---------------------------------------------------------
@pytest.mark.parametrize("timeout", [True, 0, -1, 3601, "1", None])
def test_runtime_guard_rejects_unbounded_teardown_timeout(timeout):
with pytest.raises(ContractError, match="outside its bound"):
with runtime_diagnostic_guard(teardown_timeout_seconds=timeout):
pytest.fail("body must not run")
def test_runtime_guard_quiet_body_succeeds_and_body_errors_propagate():
with runtime_diagnostic_guard(teardown_timeout_seconds=1, exit_process=pytest.fail):
value = 1 + 1
assert value == 2
with pytest.raises(KeyError, match="body"):
with runtime_diagnostic_guard(teardown_timeout_seconds=1, exit_process=pytest.fail):
raise KeyError("body")
def test_runtime_guard_reports_unavailable_containment(monkeypatch):
def no_pipe():
raise OSError("no descriptors")
monkeypatch.setattr(launcher.os, "pipe", no_pipe)
with pytest.raises(RuntimeContractError, match="containment is unavailable") as raised:
with runtime_diagnostic_guard(teardown_timeout_seconds=1, exit_process=pytest.fail):
pytest.fail("body must not run without containment")
assert raised.value.code == "LMS_DIAGNOSTIC_REDACTION_FAILED"
def test_runtime_guard_closes_pipe_when_stdout_cannot_be_saved(monkeypatch):
launcher._prepare_diagnostic_helpers()
created = []
original_pipe = os.pipe
def record_pipe():
pair = original_pipe()
created.extend(pair)
return pair
def no_dup(_descriptor):
raise OSError("dup failed")
monkeypatch.setattr(launcher.os, "pipe", record_pipe)
monkeypatch.setattr(launcher.os, "dup", no_dup)
with pytest.raises(RuntimeContractError, match="containment is unavailable"):
with runtime_diagnostic_guard(teardown_timeout_seconds=1, exit_process=pytest.fail):
pass
for descriptor in created:
with pytest.raises(OSError):
os.fstat(descriptor)
@pytest.mark.parametrize("failed_restore", [1, 2])
def test_runtime_guard_restore_failure_is_cleanup_incomplete(monkeypatch, failed_restore):
original_dup2 = os.dup2
calls = []
armed = [True]
def restore_then_fail(source, target, *args, **kwargs):
result = original_dup2(source, target, *args, **kwargs)
if not armed[0]:
return result
calls.append(target)
# Calls 1-2 redirect fds 1/2; calls 3-4 restore them.
if len(calls) > 2 and target == failed_restore:
raise OSError("restore failed")
return result
monkeypatch.setattr(launcher.os, "dup2", restore_then_fail)
try:
with pytest.raises(RuntimeContractError) as raised:
with runtime_diagnostic_guard(teardown_timeout_seconds=1, exit_process=pytest.fail):
pass
finally:
armed[0] = False
assert raised.value.code == "LMS_CLEANUP_INCOMPLETE"
assert calls == [1, 2, 1, 2]
def test_runtime_guard_flush_failure_after_body_is_cleanup_incomplete(monkeypatch):
class Stream:
def __init__(self):
self.calls = 0
def flush(self):
self.calls += 1
if self.calls > 1:
raise ValueError("closed stream")
monkeypatch.setattr(launcher, "sys", SimpleNamespace(stdout=Stream(), stderr=Stream()))
with pytest.raises(RuntimeContractError) as raised:
with runtime_diagnostic_guard(teardown_timeout_seconds=1, exit_process=pytest.fail):
pass
assert raised.value.code == "LMS_CLEANUP_INCOMPLETE"
def test_runtime_guard_reader_failure_is_cleanup_incomplete(monkeypatch):
def fail_read(_descriptor, _size):
raise OSError("read failed")
monkeypatch.setattr(launcher.os, "read", fail_read)
with pytest.raises(RuntimeContractError) as raised:
with runtime_diagnostic_guard(teardown_timeout_seconds=1, exit_process=pytest.fail):
pass
assert raised.value.code == "LMS_CLEANUP_INCOMPLETE"
def test_runtime_guard_exits_when_the_diagnostic_reader_overruns(monkeypatch):
exits = []
joins = []
readers = []
class StuckReader:
def __init__(self, *, target, daemon, name):
assert daemon is True
self.target = target
def start(self):
return None
def join(self, timeout):
joins.append(timeout)
def is_alive(self):
return True
def exit_process(code):
exits.append(code)
# Drain and close the pipe that the stuck reader still owns.
readers[0].target()
original_thread = launcher.threading.Thread
def make_reader(**kwargs):
if kwargs.get("name") != "obliteratus-model-diagnostic-sink":
return original_thread(**kwargs)
readers.append(StuckReader(**kwargs))
return readers[-1]
monkeypatch.setattr(launcher.threading, "Thread", make_reader)
with pytest.raises(RuntimeContractError) as raised:
with runtime_diagnostic_guard(teardown_timeout_seconds=2.5, exit_process=exit_process):
pass
assert raised.value.code == "LMS_CLEANUP_INCOMPLETE"
assert exits == [70]
assert joins == [2.5]
def test_backend_guard_fails_closed_when_the_diagnostic_reader_overruns(monkeypatch):
readers = []
original_thread = launcher.threading.Thread
class StuckReader:
def __init__(self, *, target, daemon, name):
self.target = target
self.timeouts = []
def start(self):
return None
def join(self, timeout):
self.timeouts.append(timeout)
def is_alive(self):
return True
def make_reader(**kwargs):
if kwargs.get("name") != "obliteratus-backend-diagnostic-sink":
return original_thread(**kwargs)
readers.append(StuckReader(**kwargs))
return readers[-1]
monkeypatch.setattr(launcher.threading, "Thread", make_reader)
try:
with pytest.raises(RuntimeContractError) as raised:
with launcher._backend_diagnostic_guard():
pass
finally:
# Drain and close the pipe that the stuck reader still owns.
for reader in readers:
reader.target()
assert raised.value.code == "LMS_DIAGNOSTIC_REDACTION_FAILED"
assert [reader.timeouts for reader in readers] == [[1]]
# --- resident helpers and expected progress output -----------------------------------
@pytest.fixture(params=["backend", "runtime"])
def diagnostic_guard(request):
if request.param == "backend":
return launcher._backend_diagnostic_guard
return lambda: runtime_diagnostic_guard(
teardown_timeout_seconds=1, exit_process=pytest.fail
)
@pytest.fixture
def diagnostic_capture(capfd, monkeypatch):
# capfd's Python streams write to separate temporary-file descriptors.
# Bind print/tqdm to fd 1/2 so they exercise the real containment pipe.
with open(1, "w", closefd=False) as stdout, open(2, "w", closefd=False) as stderr:
with monkeypatch.context() as streams:
streams.setattr(sys, "stdout", stdout)
streams.setattr(sys, "stderr", stderr)
yield capfd
@pytest.fixture
def progress_modules(monkeypatch):
from transformers.utils import logging as transformers_logging
hub_progress = import_module("huggingface_hub.utils.tqdm")
# Isolate the two independent states and avoid environment-dependent warnings.
monkeypatch.setattr(transformers_logging, "_tqdm_active", True)
monkeypatch.setattr(hub_progress, "HF_HUB_DISABLE_PROGRESS_BARS", None)
monkeypatch.setattr(hub_progress, "progress_bar_states", {})
return transformers_logging, hub_progress
@pytest.mark.skipif(os.name != "posix", reason="only POSIX uses the resource tracker")
def test_diagnostic_guard_starts_resource_tracker_before_redirect(monkeypatch, diagnostic_guard):
events = []
original_dup2 = os.dup2
def redirect(source, target, *args, **kwargs):
events.append(target)
return original_dup2(source, target, *args, **kwargs)
monkeypatch.setattr(
launcher.resource_tracker, "ensure_running", lambda: events.append("tracker")
)
monkeypatch.setattr(launcher.os, "dup2", redirect)
with diagnostic_guard():
events.append("body")
assert events[0] == "tracker"
assert events.count("tracker") == 1
assert events.index(2) < events.index("body")
@pytest.mark.skipif(os.name != "posix", reason="only POSIX uses the resource tracker")
def test_diagnostic_guard_fails_closed_if_resource_tracker_cannot_start(
monkeypatch, diagnostic_guard
):
def cannot_start():
raise OSError("private helper startup failure")
monkeypatch.setattr(launcher.resource_tracker, "ensure_running", cannot_start)
with pytest.raises(RuntimeContractError, match="containment is unavailable") as raised:
with diagnostic_guard():
pytest.fail("body must not run without the resident helper")
assert raised.value.code == "LMS_DIAGNOSTIC_REDACTION_FAILED"
assert "private" not in str(raised.value)
@pytest.mark.skipif(os.name != "posix", reason="only POSIX uses the resource tracker")
@pytest.mark.parametrize("guard_kind", ["backend", "runtime"])
def test_diagnostic_guard_shared_memory_tracker_outlives_body_without_holding_sink(guard_kind):
# A fresh interpreter is essential: a tracker left running by another test
# would hide the original lazy-start descriptor-inheritance regression.
script = textwrap.dedent("""\
import os
import sys
import threading
from multiprocessing import resource_tracker, shared_memory
from obliteratus.distributed.launcher import (
_backend_diagnostic_guard, runtime_diagnostic_guard,
)
tracker = resource_tracker._resource_tracker
assert tracker._pid is None
exits = []
block = None
guard = (
_backend_diagnostic_guard() if sys.argv[1] == "backend"
else runtime_diagnostic_guard(
teardown_timeout_seconds=0.5, exit_process=exits.append,
)
)
try:
with guard:
block = shared_memory.SharedMemory(create=True, size=1)
block.buf[0] = 42
tracker_pid = tracker._pid
assert tracker_pid is not None
assert exits == [], exits
assert tracker._pid == tracker_pid
os.kill(tracker_pid, 0) # The helper still lives after containment.
assert block.buf[0] == 42
assert not any(
thread.name in {
"obliteratus-backend-diagnostic-sink",
"obliteratus-model-diagnostic-sink",
}
for thread in threading.enumerate()
)
finally:
if block is not None:
block.close()
block.unlink()
tracker._stop()
""")
completed = subprocess.run(
[sys.executable, "-c", script, guard_kind],
capture_output=True,
text=True,
timeout=60,
)
assert completed.returncode == 0, completed.stdout + completed.stderr
assert completed.stdout == completed.stderr == ""
def test_runtime_guard_still_exits_for_a_live_child_holding_stdout():
exits = []
child = None
readers = []
def exit_process(code):
exits.append(code)
assert child.poll() is None
child.stdin.close()
child.wait(timeout=10)
try:
with pytest.raises(RuntimeContractError) as raised:
with runtime_diagnostic_guard(
teardown_timeout_seconds=0.05, exit_process=exit_process
):
# The open stdin makes lifetime deterministic without sleeps.
# stdout/stderr inherit the containment pipe, just as a helper
# started lazily during model loading did before the fix.
child = subprocess.Popen(
[sys.executable, "-c", "import sys; sys.stdin.buffer.read()"],
stdin=subprocess.PIPE,
)
readers = [
thread for thread in threading.enumerate()
if thread.name == "obliteratus-model-diagnostic-sink"
]
assert raised.value.code == "LMS_CLEANUP_INCOMPLETE"
assert exits == [70]
finally:
if child is not None:
if child.poll() is None:
child.kill()
child.wait(timeout=10)
child.stdin.close()
for reader in readers:
reader.join(1)
assert not reader.is_alive()
@pytest.mark.parametrize("initially_enabled", [True, False])
@pytest.mark.parametrize("body_fails", [False, True])
def test_diagnostic_guard_disables_weight_progress_and_restores_state(
diagnostic_guard, diagnostic_capture, progress_modules, initially_enabled, body_fails
):
transformers_logging, _hub_progress = progress_modules
def set_enabled(enabled):
if enabled:
transformers_logging.enable_progress_bar()
else:
transformers_logging.disable_progress_bar()
original_state = transformers_logging.is_progress_bar_enabled()
try:
set_enabled(initially_enabled)
expected_error = pytest.raises(KeyError, match="body") if body_fails else nullcontext()
with expected_error:
with diagnostic_guard():
assert not transformers_logging.is_progress_bar_enabled()
list(transformers_logging.tqdm(
range(12), desc="Loading weights", file=sys.stderr, mininterval=0
))
if body_fails:
raise KeyError("body")
assert transformers_logging.is_progress_bar_enabled() == initially_enabled
captured = diagnostic_capture.readouterr()
assert "Loading weights" not in captured.out + captured.err
finally:
set_enabled(original_state)
def test_diagnostic_guard_still_rejects_plain_print_with_progress_disabled(
diagnostic_guard, diagnostic_capture, progress_modules
):
transformers_logging, _hub_progress = progress_modules
original_state = transformers_logging.is_progress_bar_enabled()
with pytest.raises(RuntimeContractError) as raised:
with diagnostic_guard():
list(transformers_logging.tqdm(
range(12), desc="Loading weights", file=sys.stderr, mininterval=0
))
# Write to the real descriptor: pytest replaces sys.stderr with a capture object.
os.write(2, b"plain-diagnostic-canary\n")
assert raised.value.code == "LMS_DIAGNOSTIC_REDACTION_FAILED"
assert transformers_logging.is_progress_bar_enabled() == original_state
captured = diagnostic_capture.readouterr()
assert "plain-diagnostic-canary" not in captured.out + captured.err
assert "Loading weights" not in captured.out + captured.err
def test_runtime_guard_still_rejects_plain_stdout_print(diagnostic_capture):
with pytest.raises(RuntimeContractError) as raised:
with runtime_diagnostic_guard(teardown_timeout_seconds=1, exit_process=pytest.fail):
os.write(1, b"stdout-diagnostic-canary\n") # real fd 1, not pytest's sys.stdout
assert raised.value.code == "LMS_DIAGNOSTIC_REDACTION_FAILED"
captured = diagnostic_capture.readouterr()
assert "stdout-diagnostic-canary" not in captured.out + captured.err
@pytest.mark.parametrize("transformers_enabled", [True, False])
@pytest.mark.parametrize("hub_disabled", [True, False])
@pytest.mark.parametrize("body_fails", [False, True])
def test_diagnostic_guard_restores_independent_progress_states(
diagnostic_guard, progress_modules, transformers_enabled, hub_disabled, body_fails
):
transformers_logging, hub_progress = progress_modules
transformers_logging._tqdm_active = transformers_enabled
if hub_disabled:
hub_progress.disable_progress_bars()
else:
hub_progress.enable_progress_bars()
original_hub_states = hub_progress.progress_bar_states.copy()
expected_error = pytest.raises(KeyError, match="body") if body_fails else nullcontext()
with expected_error:
with diagnostic_guard():
assert not transformers_logging.is_progress_bar_enabled()
assert hub_progress.are_progress_bars_disabled()
if body_fails:
raise KeyError("body")
assert transformers_logging.is_progress_bar_enabled() == transformers_enabled
assert hub_progress.are_progress_bars_disabled() == hub_disabled
assert hub_progress.progress_bar_states == original_hub_states
@pytest.mark.parametrize("global_enabled", [None, False, True])
@pytest.mark.parametrize("body_fails", [False, True])
def test_diagnostic_guard_preserves_named_hub_overrides(
diagnostic_guard, progress_modules, global_enabled, body_fails
):
transformers_logging, hub_progress = progress_modules
if global_enabled is not None:
hub_progress.progress_bar_states["_global"] = global_enabled
hub_progress.disable_progress_bars("weights")
hub_progress.enable_progress_bars("weights.visible")
original_hub_states = hub_progress.progress_bar_states.copy()
original_hub_mapping = hub_progress.progress_bar_states
expected_error = pytest.raises(KeyError, match="body") if body_fails else nullcontext()
with expected_error:
with diagnostic_guard():
assert hub_progress.are_progress_bars_disabled("weights.visible")
if body_fails:
raise KeyError("body")
assert transformers_logging.is_progress_bar_enabled()
assert hub_progress.progress_bar_states is original_hub_mapping
assert hub_progress.progress_bar_states == original_hub_states
assert hub_progress.are_progress_bars_disabled("weights.child")
assert not hub_progress.are_progress_bars_disabled("weights.visible.child")
@pytest.mark.parametrize("transformers_enabled", [True, False])
def test_diagnostic_guard_restores_progress_after_partial_entry_failure(
monkeypatch, diagnostic_guard, progress_modules, transformers_enabled
):
transformers_logging, hub_progress = progress_modules
transformers_logging._tqdm_active = transformers_enabled
hub_progress.disable_progress_bars()
hub_progress.enable_progress_bars("weights.visible")
original_hub_states = hub_progress.progress_bar_states.copy()
original_disable = transformers_logging.disable_progress_bar
original_error = RuntimeError("partial progress disable failure")
calls = []
def fail_after_disabling():
calls.append("disable")
original_disable()
raise original_error
def cannot_enable():
pytest.fail("restoration must not call a public progress toggle")
monkeypatch.setattr(transformers_logging, "disable_progress_bar", fail_after_disabling)
monkeypatch.setattr(transformers_logging, "enable_progress_bar", cannot_enable)
with pytest.raises(RuntimeError) as raised:
with diagnostic_guard():
pytest.fail("body must not run after a partial entry failure")
assert raised.value is original_error
assert calls == ["disable"]
assert transformers_logging.is_progress_bar_enabled() == transformers_enabled
assert hub_progress.progress_bar_states == original_hub_states
@pytest.mark.parametrize("failing_piece", ["transformers", "hub"])
def test_progress_restoration_is_independent_and_does_not_mask_entry_error(
monkeypatch, failing_piece
):
original_error = RuntimeError("original entry error")
original_hub_states = {"_global": True, "weights": False, "weights.visible": True}
restoration_attempts = []
class ProgressLogging(SimpleNamespace):
def __setattr__(self, name, value):
if name == "_tqdm_active" and value is True:
restoration_attempts.append("transformers")
if failing_piece == "transformers":
raise RuntimeError("Transformers restoration failure")
super().__setattr__(name, value)
class HubStates(dict):
def update(self, values):
restoration_attempts.append("hub")
if failing_piece == "hub":
raise RuntimeError("Hub restoration failure")
super().update(values)
hub_states = HubStates(original_hub_states)
def fail_after_disabling():
transformers_logging._tqdm_active = False
hub_states.clear()
hub_states["_global"] = False
raise original_error
transformers_logging = ProgressLogging(
_tqdm_active=True, disable_progress_bar=fail_after_disabling
)
monkeypatch.setitem(sys.modules, "transformers.utils.logging", transformers_logging)
monkeypatch.setitem(
sys.modules, "huggingface_hub.utils.tqdm", SimpleNamespace(progress_bar_states=hub_states)
)
with pytest.raises(RuntimeError) as raised:
with launcher._disabled_transformers_progress_bars():
pytest.fail("body must not run after an entry failure")
assert raised.value is original_error
assert restoration_attempts == ["transformers", "hub"]
if failing_piece == "transformers":
assert hub_states == original_hub_states
else:
assert transformers_logging._tqdm_active is True
@pytest.mark.parametrize(
"module_name", ["transformers.utils.logging", "huggingface_hub.utils.tqdm"]
)
@pytest.mark.parametrize("progress_module", [None, SimpleNamespace()])
def test_diagnostic_guard_tolerates_unavailable_transformers_progress_api(
monkeypatch, diagnostic_guard, module_name, progress_module
):
monkeypatch.setitem(sys.modules, module_name, progress_module)
with diagnostic_guard():
pass