mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
126 lines
5.1 KiB
Python
126 lines
5.1 KiB
Python
"""Model-session failures retain ownership and close every acquired descriptor."""
|
|
|
|
import os
|
|
from contextlib import ExitStack
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
import torch.distributed as dist
|
|
|
|
import obliteratus.distributed.model_session as session
|
|
from obliteratus.distributed.contracts import ContractError, RuntimeContractError
|
|
from tests import test_distributed_model_session_contracts as fixtures
|
|
from tests.test_distributed_rank_local_gloo import _candidate_worker, run_candidate
|
|
|
|
_local_votes = fixtures._local_votes
|
|
|
|
|
|
def test_custom_worker_export_override_refused_before_setup(tmp_path, monkeypatch):
|
|
from tests import test_distributed_rank_local_gloo as gloo
|
|
|
|
export_config = gloo._export_config()
|
|
worker = Mock(side_effect=AssertionError("custom worker must not start"))
|
|
mkdir = Mock(side_effect=AssertionError("filesystem setup must not start"))
|
|
process_context = Mock(side_effect=AssertionError("process setup must not start"))
|
|
with monkeypatch.context() as patches:
|
|
patches.setattr(Path, "mkdir", mkdir)
|
|
patches.setattr(gloo.mp, "get_context", process_context)
|
|
with pytest.raises(ValueError, match="custom workers use the six-argument default-export contract"):
|
|
run_candidate(tmp_path, "success", worker=worker, export_config=export_config)
|
|
worker.assert_not_called()
|
|
mkdir.assert_not_called()
|
|
process_context.assert_not_called()
|
|
assert list(tmp_path.iterdir()) == []
|
|
|
|
|
|
def test_loaded_lifecycle_transport_failure_cannot_commit(monkeypatch):
|
|
gather = Mock(side_effect=ContractError("private malformed lifecycle frame"))
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
fixtures._emit(monkeypatch, gather)
|
|
assert failure.value.code == "LMS_LOAD_MANIFEST_INVALID"
|
|
assert "private" not in str(failure.value)
|
|
gather.assert_called_once()
|
|
|
|
|
|
@pytest.mark.parametrize("allocation", ["stack", "child-registration"])
|
|
def test_evidence_directory_allocation_failure_closes_owned_fds(tmp_path, monkeypatch, allocation):
|
|
opened, closed = [], []
|
|
original_open, original_close = os.open, os.close
|
|
|
|
def open_fd(path, *args, **kwargs):
|
|
fd = original_open(path, *args, **kwargs)
|
|
opened.append(fd)
|
|
return fd
|
|
|
|
def close_fd(fd):
|
|
closed.append(fd)
|
|
original_close(fd)
|
|
|
|
class FailingStack(ExitStack):
|
|
def __init__(self):
|
|
if allocation == "stack":
|
|
raise MemoryError("private stack allocation failure")
|
|
super().__init__()
|
|
self.calls = 0
|
|
|
|
def callback(self, callback, *args, **kwargs):
|
|
self.calls += 1
|
|
if self.calls == 2:
|
|
raise MemoryError("private callback allocation failure")
|
|
return super().callback(callback, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(session, "ExitStack", FailingStack)
|
|
monkeypatch.setattr(session.os, "open", open_fd)
|
|
monkeypatch.setattr(session.os, "close", close_fd)
|
|
monkeypatch.setattr(dist, "all_reduce", lambda *args, **kwargs: None)
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
session._prepare_evidence_directory(tmp_path, SimpleNamespace(rank=0))
|
|
assert failure.value.code == "LMS_LOAD_EVIDENCE_UNAVAILABLE"
|
|
assert "private" not in str(failure.value)
|
|
assert len(opened) == (0 if allocation == "stack" else 2)
|
|
assert closed == list(reversed(opened))
|
|
for fd in opened:
|
|
with pytest.raises(OSError):
|
|
os.fstat(fd)
|
|
|
|
|
|
def _replacement_worker(rank, rendezvous, config, identities, mode, queue):
|
|
original = session._validate_preload_bindings
|
|
calls = 0
|
|
|
|
def replace_after_preload(*args, **kwargs):
|
|
nonlocal calls
|
|
value = original(*args, **kwargs)
|
|
calls += 1
|
|
if rank == 1 and calls == 1:
|
|
root = config.for_rank(rank).staging_path
|
|
target = root if mode == "root" else root / f"{config.run_id}.rank-{rank}"
|
|
target.rename(target.with_name("preserved-original"))
|
|
target.mkdir(mode=0o700)
|
|
(target / "foreign").write_text("preserve")
|
|
return value
|
|
|
|
patches = pytest.MonkeyPatch()
|
|
patches.setattr(session, "_validate_preload_bindings", replace_after_preload)
|
|
try:
|
|
_candidate_worker(rank, rendezvous, config, identities, "success", queue)
|
|
finally:
|
|
patches.undo()
|
|
|
|
|
|
@pytest.mark.gloo
|
|
@pytest.mark.parametrize("mode", ["root", "attempt"])
|
|
def test_post_preflight_replacement_cannot_yield_loaded_session(tmp_path, mode):
|
|
config, rows, exits = run_candidate(tmp_path, mode, worker=_replacement_worker)
|
|
assert exits == [0, 0]
|
|
# Session preparation translates a plain ownership ContractError to this
|
|
# closed public code before exchanging it with the peer.
|
|
assert [row[1:] for row in rows] == [("error", "LMS_MODEL_LOAD_FAILED")] * 2
|
|
root = config.for_rank(1).staging_path
|
|
foreign = root if mode == "root" else root / f"{config.run_id}.rank-1"
|
|
assert (foreign / "foreign").read_text() == "preserve"
|
|
assert (config.staging_path / "canonical-output/prior.txt").read_text() == "prior"
|
|
assert not list(config.staging_path.rglob("distributed-export-manifest.json"))
|